要将文本(而不是原始字节)写入文件,应考虑使用FileWriter。您还应该将其包装在BufferedWriter中,这将为您提供newline方法。
要将每个单词写在新行上,请使用String.split将您的文本分成单词数组。
因此,这是对您的要求的简单测试:
public static void main(String[] args) throws Exception { String nodevalue = "i am mostafa"; // you want to output to file // BufferedWriter writer = new BufferedWriter(new FileWriter(file3, true)); // but let's print to console while debugging BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out)); String[] words = nodevalue.split(" "); for (String word: words) { writer.write(word); writer.newline(); } writer.close();}
输出为:
iammostafa
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)