'Create a new ArrayList based on input files, and print the first word of each element

I try to combine information spread across different files, generating a new ArrayList with information somehow based on the original files, and output a new txt which contains the first word of each element of that ArrayList. The problem is my code doesn't end for some reason, and I don't know why. Here is my code:

import java.util.Scanner;
import java.util.ArrayList;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.IOException;

public class FileReadVaryingAmount {
   public static void main(String[] args) throws IOException {  
      ArrayList<FileInputStream> files = getStreams("letters.txt");
      writeFirstWords(files, "myoutput.txt");
   }

   public static ArrayList<FileInputStream> getStreams(String filenames) {
      FileInputStream fis;
      Scanner sc;
      ArrayList<FileInputStream> inFS = new ArrayList<>();

      try {
         fis = new FileInputStream(filenames);
         sc = new Scanner(fis);
      }
      catch (Exception e) {
         System.out.println("File open error");
         return inFS;
      }

      Scanner scnr = new Scanner(System.in);

      try {
           while(scnr.hasNextLine()){
           String name = scnr.next();
           try {
               FileInputStream temp = new FileInputStream(name);
               Scanner scanner = new Scanner(temp);
               while (scanner.hasNext()){
               inFS.add(new FileInputStream(scanner.next()));
               }
           }
           catch (Exception e) {
               System.out.println("Error.");
           }
      }
      }
      catch(Exception e){
      System.out.println("Error.");
      }

      return inFS;
      }
   
   public static void writeFirstWords(ArrayList<FileInputStream> list, String outfileName) {
      FileOutputStream fileByteStream = null;
      PrintWriter outFS = null;

      try {
          fileByteStream = new FileOutputStream(outfileName);
          outFS = new PrintWriter(fileByteStream);
      }
      catch (Exception e) {
         System.out.println("File open error");
      }

      for (int i = 0; i < list.size(); i++){
      outFS.println(list.get(i));
      }
      outFS.flush();

      try {
         fileByteStream.close();
         outFS.close();
      }
      catch (Exception e) {
      System.out.println("File close error");
      }
   }
}

and here is the content of the files:

letters.txt

myfile.txt
myfile2.txt
myfile3.txt

myfile.txt

101 102

myfile2.txt

5 101 102 103 104 105

myfile3.txt

1 2 3 4 5


Solution 1:[1]

I revised your code and now it works perfectly.

public static void main(String[] args) throws IOException
{
    final ArrayList<String> firstWords = getFirstWords("letters.txt");
    for (String word : firstWords)
    {
        System.out.println(word);
    }
}

public static ArrayList<String> getFirstWords(String path) throws IOException
{
    final ArrayList<String> firstWords = new ArrayList<>();
    final BufferedReader input = new BufferedReader(new FileReader(path));
    // loop through all lines of the file
    String line;
    while ((line = input.readLine()) != null)
    {
        // the line corresponds to another file path
        // first line = myfile.txt
        // second line = myfile2.txt
        // third line = myfile3.txt
        firstWords.add(getFirstWordInFile(line));
    }
    input.close();
    return firstWords;
}

public static String getFirstWordInFile(String path) throws IOException
{
    final BufferedReader input = new BufferedReader(new FileReader(path));
    // read the first line of the file
    final String firstLine = input.readLine();
    // split this line into separate words
    final String[] words = firstLine.split(" ");
    final String firstWord = words[0];
    input.close();
    return firstWord;
}

The output is:

101
5
1

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1