简体   繁体   中英

Java: How do you read each line of a text file and setting each line as an array element?

I am trying to read questions that are listed in each line of a text file and then adding each line to an array, so that they can be called individually later on. I'm almost positive it is possible to do in Java but I am unsure how to do it.

I did figure out how to read a whole text file and setting it all to a string:

    private static String readFile(String pathname) {
    String line = null;
    try {
        BufferedReader reader = new BufferedReader(new FileReader(pathname));
        while((line = reader.readLine()) != null){
            System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return line;
}

Although it doesnt have much to do with this, as I mentioned this is a file that holds questions. If I get a solution to this problem I will be having another file for all the answers to the questions and I will then do the same thing for that file.

Does anyone know of a not overly complicated way to do this? If it has to be complicated then tell me that. I would like some type of example and not just links to different sites. Im not saying I will only take that though.

Thank you for your time!

PS I have read other questions on this topic but could not find a suitable answer and/or example for what I am trying to do in Java.

I would use an ArrayList... like so...

ArrayList<String> questions = new ArrayList<String>();


/// in your loop...
while((line = reader.readLine()) != null){
  questions.add(line);
}

By the way, as jlordo points out in the comment, the way you've structured your method, the only way out of your loop is for line to be null, so, by the time you get to your return statement you're returning null. What are you actually trying to return here? If it's the entire file of lines you need to be adding them to a String as you go.

Instead of array use ArrayList , which is dynamic and find the concrete code.

private static List<String> readFile(String pathname) {
    String line = null;
    List<String> list = new ArrayList<String>();
    try {
        BufferedReader reader = new BufferedReader(new FileReader(pathname));
        while((line = reader.readLine()) != null){
            list.add(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return list;
}

And for the second file, you should maintain a map<question,answer> means a Map<String,String> .

 private static String readFile(String pathname) {
    List<String> lines = new ArrayList<String> ();
    String line = null;
    try {
        BufferedReader reader = new BufferedReader(new FileReader(pathname));
        while((line = reader.readLine()) != null){
            lines.add(line) ;
            System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

Use an ArrayList<String> like I did, it grows dynamically. Declare it as a field, I just used a local variable in this example. You can iterate through it later like so:

 for(String line : lines)
   {
      System.out.println(line) ;
    }

Can't you just make a string arraylist and add each line read using readLine() into it? Then you can convert the arraylist into a plain array.

listArray.toArray(strArray);

Use Guava Files :

One line code:

System.out.println(Files.readLines(new File("C:\\sample.txt"), Charsets.UTF_8));

这在java 8中非常简单和简洁:

List<String> fileLines = Files.lines(Paths.get("/file/path/")).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM