我正在尝试将英语单词从文本文件转换为新文件,该文件将单词翻译成Pig Latin。当将其简单地打印到控制台时,所有内容都会转换它应有的方式,但是我遇到的问题是,只有初始文件的最后一行出现在新文件上。
public static void newFile(String pigLatin) {
OutputStream os = null;
try {
os = new FileOutputStream(new File("/Users/amie/Documents/inputnewnew.pig.txt"));
os.write(pigLatin.getBytes(), 0, pigLatin.length());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
默认情况下,FileOutputStream
会覆盖现有文件。您需要做的是将另一个构造函数与append
参数
FileOutputStream(String name, boolean append)
like
os = new FileOutputStream(new File("/Users/...", true))