2

I am trying to save my ArrayList into a text file. But the format of how it is saved is wrong. My code:

ArrayList<Vehicle> vehs = new ArrayList<Vehicle>();

vehs.add(new Vehicle("QJT123", "Starlet 99", 35.0, 190000));
vehs.add(new PremiumVehicle("TUX132", "BWM 05  ", 90.0, 12000, 100, 10000, 5000));

Constructor used for these 2:

public PremiumVehicle(String vehicleID, String description, double dailyRate, int odometer, int allowance, int serLength, int lastOdo) //subclass

public Vehicle(String vehicleID, String description, double dailyRate, int odometer) //Superclass

Both of these are stored into the Vehicle ArrayList. Code to save to text file:

private static void saveFile(ArrayList<Vehicle> vehs){
    File fileName = new File("VehicleList.txt");        

    try {
        FileWriter fw = new FileWriter(fileName);
        Writer output = new BufferedWriter(fw);

        for (int i = 0; i < vehs.size(); i++){
            output.write(vehs.get(i).toString() + "\n");
        }

        output.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "I cannot create that file");
    }
}

My output in VehicleList.txt:

vehicle ID = QJT123 Description = Starlet 99    Status = A  Daily Rate = 35.0 Odometer reading = 190000vehicle ID = TUX132  Description = BWM 05    Status = A  Daily Rate = 90.0   Odometer reading = 12000 Mileage allowance = 100    service length = 10000  Last Service = 5000

How do i make it such that each new line is added when writing in a new ArrayList object, e.g:

vehicle ID = QJT123 Description = Starlet 99    Status = A  Daily Rate = 35.0 Odometer reading = 190000
vehicle ID = TUX132 Description = BWM 05    Status = A  Daily Rate = 90.0   Odometer reading = 12000 Mileage allowance = 100    service length = 10000  Last Service = 5000

I've tried using vehs.get(i).toString() + "\n" but it doesn't seem to work.

4
  • not clear for me, your 2 objects are getting written in the same line??? Commented May 24, 2017 at 9:46
  • @ΦXocę웃Пepeúpaツ yes, in my text file, it is all written in one line. A new line is suppose to be added right before vehicle ID Commented May 24, 2017 at 9:49
  • doesn't make any sense. tested with some code and it works perfectly fine Commented May 24, 2017 at 9:49
  • are you running that on something else as windows? Commented May 24, 2017 at 9:55

2 Answers 2

2

Most likely, your platform does not see \n as a newline character - that thing depends on the OS.

One way to obtain the correct newline marker:

String lineSep = System.getProperty("line.separator");

or even simpler:

String lineSep = System.lineSeparator();

and instead of using + "\n" you do + lineSep in your code.

Sign up to request clarification or add additional context in comments.

1 Comment

or even better System.lineSeparator()
1

BufferedWriter has a newLine() method, you may use that if you declare output as a BufferedWriter .

private static void saveFile(ArrayList<Vehicle> vehs){
    File fileName = new File("VehicleList.txt"); 


    try {
        FileWriter fw = new FileWriter(fileName);
        BufferedWriter output = new BufferedWriter(fw);

        for (int i = 0; i < vehs.size(); i++){
            output.write(vehs.get(i).toString());
            output.newLine();
        }

        output.close();

    } catch (Exception e) {
        JOptionPane.showMessageDialog(null, "I cannot create that file");
    }
}

Also for the javadoc of the method :

Writes a line separator. The line separator string is defined by the system property line.separator, and is not necessarily a single newline ('\n') character.

5 Comments

\n should work in his code. at least it does for me
It is platform-dependent .
@XtremeBaumer yeah.. somehow \n isn't working for me.
@Berger with \n\r it should work on every common OS right?
@XtremeBaumer : well indeed with \r\n it should match the common system's new line sequences (\r\n, \r, \n ) .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.