Home > Mobile >  Reading a text file in Java and creating two seprate lists from it
Reading a text file in Java and creating two seprate lists from it

Time:10-14

So I need to read a text file into java for my project. But I have 103 elements and I need the first 100 in a list and the last three in another. However, I'm not sure how to have two different functions return the different elements as it keeps causing errors in my JUnit tests. I was wondering if there was an efficient way to separate the two lists.

CodePudding user response:

You can use Java 8 Stream API to read the file, split it into two parts and collect the result into two different lists:

List<String> firstList = Files.lines(Paths.get("file.txt"))
                              .limit(100)
                              .collect(Collectors.toList());
List<String> secondList = Files.lines(Paths.get("file.txt"))
                               .skip(100)
                               .collect(Collectors.toList());

CodePudding user response:

There is not a way to have a function return two raw objects. You should either make two different functions or create a new class with member variables that are arrays, then return that object from the function.

Another approach would be to just set the array values as member values in the function itself. Could you post the code or at least your structure that would be helpful.

CodePudding user response:

Using Files API Java 8:

You can use the Files API methods to solve your problem as below:

You can explore the Files API from official doc here.

Approach Used:

  • Do Replace "path_of_the_file" with the actual path of your file.
  • I have used Files.readAllLines(), it will read the whole file and create a List of String.
  • From the total data list, you can create two subList with startIndex and endIndex range as applicable in your case.

Please Note:

  1. As an example, I have 10 elements in the file starting from A to J and for sublist i have used startIndex and endIndex according to my main data. As a result, List1 is having first 7 elements and list2 is having last 3 elements.

  2. You can use your startIndex and endIndex for the sublists as per your requirement. As per your problem, you have 103 elements in the file so you can achieve your two lists by using the below range of indices.

As per the problem given:

List<String> list1 = fullDataList.subList(0,100);
    
List<String> list2 = fullDataList.subList(100,103);

Code:

public class Test {
    public static void main(String[] args) throws IOException {
        List<String> fullDataList = Files.readAllLines(Path.of("path_of_the_file"));
        System.out.println("full data from file:: "   fullDataList);
        List<String> list1 = fullDataList.subList(0,7);
        List<String> list2 = fullDataList.subList(7,10);
        System.out.println("list1:: "   list1);
        System.out.println("list2:: "   list2);
    }
}

Output::

full data from file:: [A, B, C, D, E, F, G, H, I, J]
list1:: [A, B, C, D, E, F, G]
list2:: [H, I, J]
  • Related