简体   繁体   中英

How to append empty line in text files using java?

I have a simple problem, I wrote this method to append records in a text file using java but the problem is when I append new record, it appends in the same line of the last record and not in a new line.

How can I append each record in a separate line??

Here is my code:

public void addProduct(int id, String name, String type, String brand, int quantity, int day1, int month1, int year1, int day2, int month2, int year2, String company, String name2, String address, int phone_no)

{
Formatter x = null;
try{
FileWriter f = new FileWriter("C:\\Users\\فاطمة\\Downloads\\products.txt", true);
x = new Formatter(f);
}
catch(Exception e)
{
//System.out.println("NO Database");
}
x.format("%d %s %s %s %d %d %d %d %d %d %d %s %s %s %d\n\n\n",id,name,type,brand,quantity,day1,month1,year1,day2,month2,year2,company,name2,address,phone_no);
x.close();
}

You can use the %n format to append a newline within your format.

This may work better than the only \\n character, which may not appear as a new line in some systems.

See the Conversions section of the documentation :

'n' line separator The result is the platform-specific line separator

Mena descripes a pretty good solution for you. For the completness. In case you are using Java < 1.5 you can also use System.getProperty("line.separator"); to get the linebreak of the operating system.

%n is available in Java > 1.5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM