Search code examples
javaarraylistwritefile

writeFile() method doesn't write all lines to text file [JAVA]


I am trying to write the contents of an arraylist to a text file. I am partially able to do this with my newbie coding skills, however at the moment it only writes the first line out of 48 lines to the text file.

I would assume that this might be due to the fact that i do not have a loop anywhere in my code however, i am not entirely sure whether id need a while loop, or for loop and where exactly i would need to put it? Could this perhaps be also due to my readFile method using String(readAllBytes(get(filename))) as opposed to reading in line by line?

    public static void main(String... p) throws IOException {

        List<SensorInfo> readings = new ArrayList<>();

        String filedata = readFile("client-temp.txt");

        SensorInfo info = new SensorInfo(filedata);
        readings.add(info);

        String data = createStringFromInfo(readings);
        System.out.println("Lines: " + readings.size());
        writeFile("datastore.txt", data);
    }
}

writeFile

public static void writeFile(String filename, String content)
    {
        try
        {
            Files.write(get(filename), content.getBytes());
        } 
        catch (IOException e)
        {
            System.out.println("Error wiring file: " + e);
        }
    }

createStringFromInfo

public static String createStringFromInfo(List<SensorInfo> infoList)
    {
        String data = "";
        for (SensorInfo info : infoList)
        {
            data += info.asData();
        }
        return data;
    }

SensorInfo http://pastebin.com/9DDDGzwV


Solution

  • MadProgrammer is right.
    If I got it right, the file client-temp.txt has multiple lines; each one representing data from a single SensorInfo object. When you read all of them into filedata and pass it as a parameter for the constructor of SensorInfo, only the first line will be used to instantiate a single SensorInfo object, as you can clearly see from the source in the link you posted. All the remaining lines will be discarded. Keep in mind that a constructor of a class will always instantiate a single object of that class.
    To resolve the issue you should read line by line from the file client-temp.txt and pass each one as a parameter for the creation of a new SensorInfo object that will be added to the list.

    Here is a suggestion for your new main method:

    public static void main(String[] args) {
        List<SensorInfo> readings = new ArrayList<>();
    
        try (BufferedReader br = new BufferedReader(new FileReader("client-temp.txt"))) {
            String line = br.readLine();
    
            while (line != null) {
                readings.add(new SensorInfo(line));
                line = br.readLine();
            }
        } catch (IOException e) {
            System.out.println("Unable to read data file");
            e.printStackTrace();
            System.exit(1);
        }
    
        String data = createStringFromInfo(readings);
        System.out.println("Lines: " + readings.size());
        writeFile("datastore.txt", data);
    }
    

    Regards