java - Java 将数据写入外部文件
问题描述
我找到了一种写出数据的方法,除了应该写到文件的所有单词中,只有最后一个单词出现在输出文件中。这是代码:
public static void main(String[] args) throws IOException {
for (String s: args) {
//here it gets the file from the doc name in the command line, goes through it, and adds all
//the words to a vector
incorporteVocab();
}
}
public static void incorporteVocab() {
String filename = "C:\\projectTests\\vocabulary.txt"; //file where to write out the vocabulary
for (String w : commonDocumentWords) {
if (!inStopList(w))
addToVocabulary(w, filename);
}
}
public static void addToVocabulary(String word, String filename) {
Vocabulary.add(word);
toDataFile(word, filename);
}
public static void toDataFile(String word, String filename) {
try {
FileWriter myWriter = new FileWriter(filename);
myWriter.write(word);
myWriter.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
请帮忙,谢谢!
解决方案
您可以将 FileWriter 的第二个参数更改为true以使其将新数据附加到文件中:
FileWriter myWriter = new FileWriter(filename, true);
推荐阅读
- saml - SAML 2.0 "Stale Request"
- python - How to reshape my 4D-np.array to a 2D matrix and keep the structure?
- java - 使用 Hibernate JOINED 继承策略时从父实体中查询/选择行
- wpf - ToggleButton 没有 IsDefault 属性 Wpf
- external - 使用STM32cubeProgrammer进行外部FLASH慢速验证
- c# - 如何检查类型是否为记录
- python - 如何在我的外部硬盘驱动器中处理 django 项目
- node.js - 努力在nodejs的for循环中使用异步等待
- node.js - VSCode terminal can't find NodeJs
- flask-sqlalchemy - 升级前 Flask-migrate 更改数据库