java 无法读取文件

编程入门 行业动态 更新时间:2024-10-14 12:28:36

java 无法读取<a href=https://www.elefans.com/category/jswz/34/1771438.html style=文件"/>

java 无法读取文件

我在使用Java读取文件时遇到了一些麻烦。

文件的样子:

Answer 1:

1. This is an apple

2. Something

Answer 2:

1. This is a cool website

2. I love banana

3. This is a table

4. Programming is fun

Answer 3.

1. Hello World

....

我想要做的是将它们分成两个部分:

一个是答案号码;另一个是答案清单。

假设我有一个名为Answer的对象类:

答案编号的字符串

答案清单。

到目前为止,这是我在将代码放入对象类之前调试代码所做的工作。但我无法得到正确的结果

public void reader(String file) throws FileNotFoundException, IOException {

FileReader fR = new FileReader(file);

BufferedReader bR = new BufferedReader(fR);

String line = null;

int count = 0 ;

String blockNum = "";

String printState = "" ;

while ((line = bR.readLine()) != null) {

if(line.contains("Answer")){

//System.out.println("Contain Answer statement: " + line);

count++;

blockNum = line;

printState = "";

}

else{

//System.out.println("No Answer Statement: " + line);

printState += line + " / " ;

}

System.out.println( count + " " + blockNum + " " + printState );

}

// Close the input stream

bR.close();

fR.close();

}

我很确定在编码时我做了些蠢事。我不太清楚如何阅读它以便将它分开。

现在输出如下:

1 Answer 1:

1 Answer 1: 1. This is an apple /

1 Answer 1: 1. This is an apple / 2. Something /

2 Answer 2:

2 Answer 2: 1. This is a cool website /

2 Answer 2: 1. This is a cool website / 2. I love banana /

2 Answer 2: 1. This is a cool website / 2. I love banana / 3. This is a table /

2 Answer 2: 1. This is a cool website / 2. I love banana / 3. This is a table / 4. Programming is fun /

3 Answer 3.

3 Answer 3. 1. Hello World /

但我希望输出是这样的:

1 Answer 1: 1. This is an apple / 2. Something /

2 Answer 2: 1. This is a cool website / 2. I love banana / 3. This is a table / 4. Programming is fun /

3 Answer 3. 1. Hello World /

更多推荐

java 无法读取文件

本文发布于:2024-03-23 18:28:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1741400.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!