Reading and displaying data from a .txt file

后端 未结 10 950
北海茫月
北海茫月 2020-11-27 15:41

How do you read and display data from .txt files?

10条回答
  •  庸人自扰
    2020-11-27 15:51

    You most likely will want to use the FileInputStream class:

    int character;
    StringBuffer buffer = new StringBuffer("");
    FileInputStream inputStream = new FileInputStream(new File("/home/jessy/file.txt"));
    
    while( (character = inputStream.read()) != -1)
            buffer.append((char) character);
    
    inputStream.close();
    System.out.println(buffer);
    

    You will also want to catch some of the exceptions thrown by the read() method and FileInputStream constructor, but those are implementation details specific to your project.

提交回复
热议问题