Using BufferedReader to read Text File

前端 未结 9 1519
你的背包
你的背包 2020-11-29 04:29

I\'m having problems with using the BufferedReader

I want to print the 6 lines of a text file:

public class Reader {

public static void main(String[         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-11-29 05:24

    or

    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;
    }
    

    This gets first line as well.

提交回复
热议问题