关于使用 FileInputStream 的问题, 如何实现打开文件一次之后所有 method 都无须再打开文件就能用。
新手求助要求只能打开文件1次。我不理解的是为什么如果我不在每个method里打开一次文件,那个method就不能正常运行?
假设我在getVowelCount()那个method里不再次打开文件,这个method就不能正确运作?
相反,在这个method里打开一次文件,就能正确执行命令。
求解释和解决方法。
万分感谢!🙏
程序代码:
import java.util.Scanner;
import import public class TextFileAnalyzer {
private String file = null;
private int charCount = 0;
private int lnCount = 0;
private int stnCount = 0;
private int volCount = 0;
private int i = 0;
private char ctr;
private int wrdCount = 0;
private String wrd = null;
private FileInputStream input;
private Scanner Input;
public TextFileAnalyzer(String filePath) throws Exception {
this.file = filePath;
input = new FileInputStream(file);
Scanner Input = new Scanner(input);
}
public int getCharCount() throws IOException {
for (int c = input.read(); c != -1; c = input.read()){
charCount++;
}
return charCount;
}
public int getVowelCount() throws IOException {
input = new FileInputStream(file);
byte[] b = new byte[input.available()];
input.read(b);
for (i = 0; i < b.length; i++) {
if(b[i] == 'A' || b[i] == 'a' || b[i] == 'E' ||
b[i] == 'e' || b[i] == 'I' || b[i] == 'i' ||
b[i] == 'O' || b[i] == 'o' || b[i] == 'U' ||
b[i] == 'u') {
volCount ++;
}
}
return volCount;
}
public int getWordCount() throws IOException {
input = new FileInputStream(file);
Scanner Input = new Scanner(input);
while (Input.hasNextLine()) {
wrd = Input.next();
wrdCount++;
}
Input.close();
return wrdCount;
}
public int getLineCount() throws IOException {
input = new FileInputStream(file);
byte[] b = new byte[input.available()];
input.read(b);
for (i = 0; i < b.length; i++) {
if(b[i] == '\n') {
lnCount++;
}
}
return lnCount;
}
public int getSentenceCount() throws IOException {
input = new FileInputStream(file);
byte[] b = new byte[input.available()];
input.read(b);
for (i = 0; i < b.length; i++) {
if(b[i] == '.' && b[i] != b[ i - 1]) {
stnCount++;
}
}
return stnCount;
}
public String getFilePath() {
return file;
}
public String toString() {
return file + ": " + charCount + "c " + volCount + "v "
+ wrdCount + "w " + lnCount + "l " + stnCount + "s ";
}
public static void main(String[] args) throws Exception {
String path = "Desktop/TextFile.txt";
TextFileAnalyzer analysis = new TextFileAnalyzer(path);
if (analysis.getCharCount() == 523099) {
System.out.println("getCharCount seems OK.");
} else {
System.out.println("getCharCount has problems.");
}
if (analysis.getVowelCount() == 159206) {
System.out.println("getVowelCount seems OK.");
} else {
System.out.println("getVowelCount has problems.");
}
if (analysis.getWordCount() == 88147) {
System.out.println("getWordCount seems OK.");
} else {
System.out.println("getWordCount has problems.");
}
if (analysis.getLineCount() == 8425) {
System.out.println("getLineCount seems OK.");
} else {
System.out.println("getLineCount has problems.");
}
if (analysis.getSentenceCount() == 3677) {
System.out.println("getSentenceCount seems OK.");
} else {
System.out.println("getSentenceCount has problems.");
}
if (analysis.getFilePath().equals(path)) {
System.out.println("getFilePath seems OK.");
} else {
System.out.println("getFilePath has problems.");
}
System.out.println(analysis);
}
}[ 本帖最后由 sarahman 于 2014-11-14 11:14 编辑 ]








