Reading a text file in java

前端 未结 7 1069
遥遥无期
遥遥无期 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:45

    The best approach to read a file in Java is to open in, read line by line and process it and close the strea

    // Open the file
    FileInputStream fstream = new FileInputStream("textfile.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
    
    String strLine;
    
    //Read File Line By Line
    while ((strLine = br.readLine()) != null)   {
      // Print the content on the console - do what you want to do
      System.out.println (strLine);
    }
    
    //Close the input stream
    fstream.close();
    

    To learn more about how to read file in Java, check out the article.

提交回复
热议问题