-1

I'm currently writing an assignment that takes multiple text files(File objects) with lines, and then combines the lines together and separates them by commas, like:

File1Line1, File2Line1
File1Line2, File2Line2

I guess I'm just confused with how to use the files. How would I get the first(second, third, etc.) line from each file, while also dealing with files having different numbers of lines? Any help just on the concept of this is appreciated.

user1547050
  • 337
  • 2
  • 7
  • 15

3 Answers3

1

As far as reading a file line by line it's easy to do in most languages. Here's an example in java: How to read a large text file line by line using Java?.

Conceptually, you should start with thinking of an algorithm and then write some pseudocode to further explore and understand it.

For this assignment, an option would be to alternate reading each file one line at a time, and immediately write them to the csv. A second option would be to store each line in a data structure, such as an array, and write at the end, but that could be expensive for large files. You can handle different file lengths in many ways, for instance just writing the lines without corresponding lines alone. Here's some pseudocode, based on java:

    FileReader reader1 = FileReader("file1.text")
    FileReader reader2 = FileReader("file2.text")

    while(reader1.hasNextLine() || reader2.hasNextLine()) 
    {
         if(reader1.hasNextLine()) {
         writeToCSV(reader1.nextLine());
         } 
         if(reader2.hasNextLine() {
         writeToCSV(reader2.nextLine());
         }
         writeToCSV("\r\n");
    }

You can find plenty of examples on the actual method calls, but it's important to understand the algorithm first.

Community
  • 1
  • 1
androiddev19
  • 306
  • 2
  • 10
0

If you are sure the lines of the two files are One-to-One mapping, then it is easy.

You can use two BuffererReader to read these two files, and you just need to iterate one of them

some codes like this:

BufferedReader reader1 = new BufferedReader(new FileReader(new File(pathOfFile1)));
BufferedReader reader2 = new BufferedReader(new FileReader(new File(pathOfFile2)));

BufferedWriter writer = new BufferedWriter(new FileWriter(new File(pathOfOutputCsvFile)));

String lineOfFile1 = null;

while((lineOfFile1 = reader1.readLine()) != null){

  String lineOfFile2 = reader2.readLine();

  //here, lineOfFile1 and lineOfFile2 are the same line number 
  //then some codes for combination
  //...

}

//finally don't forget to close the readers and writer.

If you can't be sure the lines in these two files are One-to-One mapping, then you should read them all into the memory and mapping them in memory then output them as a CSV file.

jiacheo
  • 308
  • 1
  • 2
  • 12
0

This code only directly references 1 line from each file in RAM at a time, meaning it should work with huge files without memory exceptions. Behind the scenes more memory may be occupied than what you see, but it will still not crash with huge files.

Code works by reading one line at a time from each of the files till all files are empty. As files run out of lines output an empty string instead.

void assignment(String outputFile, String... filenames){
    PrintWriter writer = new PrintWriter(outputFile, "UTF-8");
    Scanner scanners = new Scanner[filenames.length];
    for(int i=0;i<filenames.length;i++){
        Scanner scanner = new Scanner(new File(filenames[i]));
        scanners[i] = scanner;
    }
    boolean running = true;
    while(running){
        boolean allEmpty = true;
        StringBuilder csvLine = new StringBuilder();
        for(int i=0;i<scanners.lengh;i++){
            if(scanner.hasNextLine()){
                String line = scanner.nextLine();
                csvLine.append(line);
                allEmpty=false;
            }
            if(i!=scanners.length-1) csvLine.append(",");
        }
        if(allEmpty)
            running=false;
        else
        writer.println(csvLine.toString());

    }
    writer.close();
    for(Scanner s : scanners) s.close();
}

Usage:

assignment("output.txt","file1.txt","file2.txt","file3.txt","file4.txt");

Or:

String[] args = new String[]{"helloWorld.txt","fun.bin"};
assignment("output2.txt",args);

This code is untested and doesn't handle exceptions. This code will let you read in lines from files who's lines don't match, and combine them into a single CSV file. As files run out of lines, only empty strings will be shown.

This should give you an idea of how to do precisely what you've asked.

William Morrison
  • 10,953
  • 2
  • 31
  • 48