当我尝试java文件的get kincaid值时,我收到此错误。我无法找到我应该做的事情。据我所知,这是一个关闭文件的问题。 我尝试了另外一些关于阅读 - 闭包的选择,但我无法做到。我应该如何进行关闭操作?谢谢。
Exception in thread "main" java.io.IOException: Stream Closed
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Kincaid.calcWordsAux(Kincaid.java:91)
at Kincaid.main(Kincaid.java:27)
`static ArrayList<String> javaFiles = new ArrayList<String>();
public static void main(String[] args) throws IOException {
File folder = new File("C:/Users/blabla/src");
File[] listOfFiles = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
if (isItJavaFile(listOfFiles[i])) {
javaFiles.add(listOfFiles[i].getAbsolutePath());
InputStream targetStream = new FileInputStream(
listOfFiles[i]);
System.out.println(calculateKincaid(
calcSyllablesAux(targetStream),
calcWordsAux(targetStream),
calcSentencesAux(targetStream)));
}
} else if (listOfFiles[i].isDirectory()) {
getTheJavaFiles(listOfFiles[i]);
}
}
for (String s : javaFiles) { System.out.println(s); }
}
public static boolean isItJavaFile(File file) {
if ((file.getName().length() > 5)
&& file.getName().substring(file.getName().length() - 5)
.equals(".java"))
return true;
else
return false;
}
public static void getTheJavaFiles(File file) {
boolean x = false;
while (true) {
File[] listOfFiles = file.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (isItJavaFile(listOfFiles[i])) {
if (javaFiles.contains(listOfFiles[i].getAbsolutePath())) {
x = true;
break;
}
javaFiles.add(listOfFiles[i].getAbsolutePath());
} else {
file = new File(file + "/" + listOfFiles[i].getName());
getTheJavaFiles(file);
}
}
if (x == true) {
break;
}
}
}
public static int calcWords(String line) {
int count = 0;
for (int i = 0; i < line.length() - 1; i++) {
if (line.charAt(i) == ' ' && line.charAt(i + 1) != ' ')
count++;
}
return count;
}
public static int calcWordsAux(InputStream file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int wordCount = 0;
int ssLine = 0;// slash star gorunce 1 yapiyoruz.comment bitince star
// slash gelince 0.
String line = null;
while ((line = br.readLine()) != null) {
if (ssLine == 1 && (!line.contains("*/"))) {
wordCount += calcWords(line.substring(line.indexOf("*")));
}
if (line.contains("*/")
&& (line.indexOf("*") - line.indexOf("/") == -1)
&& ssLine != 0) {
wordCount += calcWords(line.substring(0, line.indexOf("*")));
ssLine = 0;
}
if (line.contains("//")
&& (line.indexOf("/") - line.lastIndexOf("/") == -1)) {
wordCount += calcWords(line.substring(line.lastIndexOf("/")));
}
if (line.contains("/*")) {
ssLine++;
wordCount += calcWords(line.substring(line.indexOf("/")));
}
}
br.close();
return wordCount;
}
public static int calcSentences(String line) {
int count = 0;
line.indexOf('"');
line = line.replaceAll("\\.{3}", "");
String delimiters = "?!.";
for (int i = 0; i < line.length(); i++) {
if (delimiters.indexOf(line.charAt(i)) != -1) {
count++;
}
}
return count;
}
public static int calcSentencesAux(InputStream file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int sentencesCount = 0;
int ssLine = 0;// slash star gorunce 1 yapiyoruz.comment bitince star
// slash gelince 0.
String line = null;
while ((line = br.readLine()) != null) {
if (ssLine == 1 && (!line.contains("*/"))) {
sentencesCount+= calcSentences(line);
}
if (line.contains("*/")
&& (line.indexOf("*") - line.indexOf("/") == -1)
&& ssLine != 0) {
sentencesCount += calcSentences(line.substring(0,
line.indexOf("*")));
ssLine = 0;
}
if (line.contains("//")
&& (line.indexOf("/") - line.lastIndexOf("/") == -1)) {
sentencesCount += calcSentences(line.substring(line
.lastIndexOf("/")));
}
if (line.contains("/*")) {
ssLine++;
sentencesCount += calcSentences(line.substring(line
.indexOf("/")));
}
}
br.close();
return sentencesCount;
}
public static int calcSyllables(String line) {
int syllables = line.length()
- line.toLowerCase().replaceAll("a|e|i|o|u|", "").length();
return syllables;
}
public static int calcSyllablesAux(InputStream file) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(file));
int syllablesCount = 0;
int ssLine = 0;// slash star gorunce 1 yapiyoruz.comment bitince star
// slash gelince 0.
String line = null;
while ((line = br.readLine()) != null) {
if (ssLine == 1 && (!line.contains("*/"))) {
syllablesCount += calcSyllables(line);
}
if (line.contains("*/")
&& (line.indexOf("*") - line.indexOf("/") == -1)
&& ssLine != 0) {
syllablesCount += calcSyllables(line.substring(0,
line.indexOf("*")));
ssLine = 0;
}
if (line.contains("//")
&& (line.indexOf("/") - line.lastIndexOf("/") == -1)) {
syllablesCount += calcSyllables(line.substring(line
.lastIndexOf("/")));
}
if (line.contains("/*")) {
ssLine++;
syllablesCount += calcSyllables(line.substring(line
.indexOf("/")));
}
}
br.close();
return syllablesCount;
}`
答案 0 :(得分:2)
您正在调用三种方法,为所有方法提供相同的InputStream
- 并且每个方法创建一个BufferedReader
包裹InputStream
,然后在结束时关闭它。
关闭BufferedReader
将关闭基础InputStream
,导致您看到的错误。
鉴于您想要三次读取文件,您应该单独打开它三次...或打开一次,将所有行读入列表,然后在该列表上操作。