Reading a text file in java

前端 未结 7 891
遥遥无期
遥遥无期 2020-11-30 11:50

How would I read a .txt file in Java and put every line in an array when every lines contains integers, strings, and doubles? And every line has different amounts of words/n

相关标签:
7条回答
  • 2020-11-30 12:54

    Try the Scanner class which no one knows about but can do almost anything with text.

    To get a reader for a file, use

    File file = new File ("...path...");
    String encoding = "...."; // Encoding of your file
    Reader reader = new BufferedReader (new InputStreamReader (
        new FileInputStream (file), encoding));
    
    ... use reader ...
    
    reader.close ();
    

    You should really specify the encoding or else you will get strange results when you encounter umlauts, Unicode and the like.

    0 讨论(0)
提交回复
热议问题