gpt4 book ai didi

java - bufferedReader 关闭文件

转载 作者:行者123 更新时间:2023-12-02 03:45:49 25 4
gpt4 key购买 nike

当我尝试获取 java 文件的 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;
}`

最佳答案

您正在调用三个方法,为所有这些方法提供相同的 InputStream - 并且每个方法都会创建一个包装该 InputStreamBufferedReader ,然后在最后关闭它。

关闭 BufferedReader 将关闭底层 InputStream,从而导致您看到的错误。

鉴于您想要读取该文件三遍,您应该单独打开它三遍...或者打开一次,将所有行读入一个列表,然后对该列表进行操作。

关于java - bufferedReader 关闭文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36316145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com