如何读取文本文件并将其打印到控制台窗口? Java的 [英] How do you read a text file and print it to the console window? Java

查看:651
本文介绍了如何读取文本文件并将其打印到控制台窗口? Java的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想读整个文本文件,并将其全部内容存储到一个字符串中。然后我想打印字符串到控制台窗口。我试过这个:

  import java.util.Scanner; 
import java.io. *;

public class WritingTextFiles {

public static void main(String [] args)throws IOException {
FileWriter fw = new FileWriter(testing.txt);
Scanner in =新的扫描仪(System.in);
String testwords = in.nextLine();
fw.write(testwords);
BufferedReader r = new BufferedReader(new FileReader(testing.txt));
System.out.print(r);
fw.close();




$ b $ p $打印到控制台窗口的唯一东西是java.io.BufferedReader@18fb397。

任何人都可以解释这个像我这样的新手吗?我有很少的经验,但我肯定愿意学习。我愿意接受任何和所有的建议。提前致谢!

解决方案

java.io.BufferedReader@18fb397打印到控制台的原因是因为您提供了引用缓冲读取器作为打印的参数,而不是您要打印的字符串。

  BufferedReader r = new BufferedReader(new FileReader(testing.txt)); 
System.out.print(r);

应该是:

  BufferedReader r = new BufferedReader(new FileReader(testing.txt)); 
String s =,line = null; ((line = r.readLine())!= null){
s + = line;
while
}
System.out.print(s);

注意,我们实际上读取了文件的行并将其存储在一个临时变量中,变量s。然后我们打印s,而不是BufferedReader。

最后说明,当你完成时关闭一个文件是明智的,你可以调用fw.close()但是在编写测​​试用例之后你应该直接调用它。这是为了确保FileWriter已经写入了字符串。

I would like to read an entire text file and store its entire contents into a single string. Then I would like to print the string to the console window. I tried this:

import java.util.Scanner;
import java.io.*;

public class WritingTextFiles{

    public static void main (String [] args) throws IOException{
        FileWriter fw= new FileWriter("testing.txt");  
        Scanner in= new Scanner (System.in);
        String testwords=in.nextLine();  
        fw.write(testwords);  
        BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );  
        System.out.print(r);  
        fw.close();  
    }
}

The only thing that is printed to the console window is java.io.BufferedReader@18fb397.

Can anyone explain this to a newbie like me? I have very little experience but I am certainly willing to learn. I am open to any and all suggestions. Thanks in advance!

解决方案

The reason that java.io.BufferedReader@18fb397 is printed to the console is because you give the reference of the buffered reader as an argument to print, and not the string you want to print.

BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );
System.out.print(r);

should be:

BufferedReader r = new BufferedReader( new FileReader( "testing.txt" ) );
String s = "", line = null;
while ((line = r.readLine()) != null) {
    s += line;
}
System.out.print(s);

Notice we actually read the lines of the file and store it in a temporary variable, then we append this variable to s. Then we print s, and not the BufferedReader.

On a final note, it is wise to close a file when your done, you do call fw.close(), but you should have called it directly after writing the testwords. This is to make sure that the FileWriter has actually written the string.

这篇关于如何读取文本文件并将其打印到控制台窗口? Java的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆