【问题标题】:Why i am getting output in a single line?为什么我在一行中得到输出?
【发布时间】:2017-11-09 02:56:53
【问题描述】:

文件名: BufferedReaderExample.txt

hello
amarnath
durga
india
asia

源文件:BufferedReaderExample.java

import java.io.*;
class  BufferedReaderExample
{
public static void main(String[] args) throws IOException 
{
    FileReader fr = new FileReader("BufferedReaderExample.txt");
    BufferedReader br = new BufferedReader(fr);
    String s = br.readLine();
    while(s!=null)
    {
        System.out.print(s);
        s = br.readLine();
    }
    br.close(); 
}
}

为什么我在一行中得到输出?

helloamarnathdurgaindiaasia

【问题讨论】:

  • 你正在阅读为 line,然后打印不带换行符
  • 因为您使用的是System.out.print 而不是System.out.println
  • 你正在使用打印,它是在没有换行的情况下打印它......在询问之前尝试做更多的研究

标签: java bufferedreader


【解决方案1】:

您也可以这样做来添加新行:

System.out.print(s + “\n”)

或者如果你想在每个单词之间留一个空格,那么:

System.out.print(s + “ ”)

【讨论】:

    【解决方案2】:

    你正在使用

    System.out.print(s);
    

    如果你希望每行分开,请改用这个:

    System.out.println(s);
    

    【讨论】:

      猜你喜欢
      • 2017-06-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-30
      • 2020-02-28
      相关资源
      最近更新 更多