Reading a text file in java

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

    This is a nice way to work with Streams and Collectors.

    List<String> myList;
    try(BufferedReader reader = new BufferedReader(new FileReader("yourpath"))){
        myList = reader.lines() // This will return a Stream<String>
                     .collect(Collectors.toList());
    }catch(Exception e){
        e.printStackTrace();
    }
    

    When working with Streams you have also multiple methods to filter, manipulate or reduce your input.

    0 讨论(0)
  • 2020-11-30 12:40

    Easiest option is to simply use the Apache Commons IO JAR and import the org.apache.commons.io.FileUtils class. There are many possibilities when using this class, but the most obvious would be as follows;

    List<String> lines = FileUtils.readLines(new File("untitled.txt"));
    

    It's that easy.

    "Don't reinvent the wheel."

    0 讨论(0)
  • 2020-11-30 12:41

    Common used:

        String line = null;
        File file = new File( "readme.txt" );
    
        FileReader fr = null;
        try
        {
            fr = new FileReader( file );
        } 
        catch (FileNotFoundException e) 
        {  
            System.out.println( "File doesn't exists" );
            e.printStackTrace();
        }
        BufferedReader br = new BufferedReader( fr );
    
        try
        {
            while( (line = br.readLine()) != null )
        {
            System.out.println( line );
        }
    
    0 讨论(0)
  • 2020-11-30 12:41

    @user248921 first of all, you can store anything in string array , so you can make string array and store a line in array and use value in code whenever you want. you can use the below code to store heterogeneous(containing string, int, boolean,etc) lines in array.

    public class user {
     public static void main(String x[]) throws IOException{
      BufferedReader b=new BufferedReader(new FileReader("<path to file>"));
      String[] user=new String[500];
      String line="";
      while ((line = b.readLine()) != null) {
       user[i]=line; 
       System.out.println(user[1]);
       i++;  
       }
    
     }
    }
    
    0 讨论(0)
  • Your question is not very clear, so I'll only answer for the "read" part :

    List<String> lines = new ArrayList<String>();
    BufferedReader br = new BufferedReader(new FileReader("fileName"));
    String line = br.readLine();
    while (line != null)
    {
        lines.add(line);
        line = br.readLine();
    }
    
    0 讨论(0)
  • 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.

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