java 下一行怎么弄,Java FileWriter如何写入下一行

I used the code below to write record into a File. The record can be written in the file, but is append in one Line, each time I call this method example:

Hello WorldHello WorldHello WorldHello World

How can I modify the code so the output look like below, so that when read the text I can use line.hasNextLine() to check?

Hello World

Hello World

Hello World

Hello World

// Create file

FileWriter fstream = new FileWriter(fileName, true);

BufferedWriter out = new BufferedWriter(fstream);

out.write(c.toString());

//Close the output stream

out.close();

// Code I used to read record I am using | as a seperator name and id

String fileName = folderPath + "listCatalogue.txt";

String line = "";

Scanner scanner;

String name, id;

scanner = new Scanner(fileName);

System.out.println(scanner.hasNextLine());

while (scanner.hasNextLine()) {

line = scanner.nextLine();

System.out.println(line);

StringTokenizer st = new StringTokenizer(line, "|");

name = st.nextToken();

id = st.nextToken();

catalogues.add(new Catalogue(name, id));

}

解决方案

out.write(c.toString());

out.newLine();

here is a simple solution, I hope it works

EDIT:

I was using "\n" which was obviously not recommended approach, modified answer.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值