I get this error when I trying the get kincaid value of a java file. I couldn't find what i should do.As I understand it's a problem about closing a file. I have tried another options about reading-closin but i could not do. How should i do the closin operation?Thanks.
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;
}`
You're calling three methods, providing the same InputStream
to all of them - and each of them creates a BufferedReader
wrapping that InputStream
, and then closes it at the end.
Closing the BufferedReader
will close the underlying InputStream
, causing the bug you're seeing.
Given that you want to read the file three times, you should be opening it separately three times... or opening it once, reading all the lines into a list, and then operating on that list.
See more on this question at Stackoverflow