我在使用BufferedReader
时遇到问题我想打印文本文件的6行:
public class Reader {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
while (br.readLine() != null) {
System.out.println(br.readLine());
}
in.close();
}
现在,每当我调用readLine()方法时,我都可以收集它,它会自动前进到下一行。
所以我不能使用条件br.readLine()!= null,因为它已经将它推进一行并获得输出:
Line 2
Line 4
Line 6
我使用什么条件来检查文本字段中是否还有新行。
提前致谢!
答案 0 :(得分:69)
这是问题所在:
while (br.readLine() != null) {
System.out.println(br.readLine());
}
你有两次调用readLine
- 第一个仅检查是否有一行(但是读取并抛弃它),第二次读取 next < / em>行。你想要:
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
现在我们只在每次循环迭代时调用readLine()
一次,并使用我们为“我们完成了吗?”读取的行。和“打印出线”部分。
答案 1 :(得分:4)
您通过line
循环读取while
并通过循环读取下一行,因此只需在循环中读取
String s;
while ((s=br.readLine()) != null) {
System.out.println(s);
}
答案 2 :(得分:3)
您可以将br.readLine()
的结果分配给变量,并将其用于处理和检查,如下所示:
String line = br.readLine();
while (line != null) { // You might also want to check for empty?
System.out.println(line);
line = br.readLine();
}
答案 3 :(得分:1)
private void readFile() throws Exception {
AsynchronousFileChannel input=AsynchronousFileChannel.open(Paths.get("E:/dicom_server_storage/abc.txt"),StandardOpenOption.READ);
ByteBuffer buffer=ByteBuffer.allocate(1024);
input.read(buffer,0,null,new CompletionHandler<Integer,Void>(){
@Override public void completed( Integer result, Void attachment){
System.out.println("Done reading the file.");
}
@Override public void failed( Throwable exc, Void attachment){
System.err.println("An error occured:" + exc.getMessage());
}
}
);
System.out.println("This thread keeps on running");
Thread.sleep(100);
}
答案 4 :(得分:0)
也许你的意思是:
public class Reader {
public static void main(String[]args) throws IOException{
FileReader in = new FileReader("C:/test.txt");
BufferedReader br = new BufferedReader(in);
String line = br.readLine();
while (line!=null) {
System.out.println(line);
line = br.readLine();
}
in.close();
}
答案 5 :(得分:0)
尝试:
String text= br.readLine();
while (text != null)
{
System.out.println(text);
text=br.readLine();
}
in.close();
答案 6 :(得分:0)
或
public String getFileStream(final String inputFile) {
String result = "";
Scanner s = null;
try {
s = new Scanner(new BufferedReader(new FileReader(inputFile)));
while (s.hasNext()) {
result = result + s.nextLine();
}
} catch (final IOException ex) {
ex.printStackTrace();
} finally {
if (s != null) {
s.close();
}
}
return result;
}
这也是第一行。
答案 7 :(得分:0)
您可以将其存储在数组中,然后使用您想要的任何一行。 这是我用来从文件中读取行并将其存储在字符串数组中的代码片段,希望这对您有用:)
public class user {
public static void main(String x[]) throws IOException{
BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
String[] user=new String[30];
String line="";
while ((line = b.readLine()) != null) {
user[i]=line;
System.out.println(user[1]);
i++;
}
}
}
答案 8 :(得分:0)
尝试使用资源。这将自动关闭资源。
try (BufferedReader br = new BufferedReader(new FileReader("C:/test.txt"))) {
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
} catch (Exception e) {
}