- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我正在尝试创建一个将新数据写入保存文件的程序。该文件具有三个“插槽”,即三个由分隔符分隔的字符串。主程序以slot为参数调用saver程序,saver程序打开文件,将每个slot中已有的String读入局部变量,用新的String替换给定slot对应的,并覆盖文件与新插槽。这应该会导致仅更新给定的插槽,而其他两个与以前相同。
主程序连续三次调用saver,每个slot一次。这应该导致保存文件如下所示(其中 # 是分隔符):
在第一次调用之前:#EMPTY#EMPTY#EMPTY
第一次调用后:#NewString#EMPTY#EMPTY
第二次调用后:#NewString#NewString#EMPTY
第三次调用后:#NewString#NewString#NewString
.
取而代之的是:
在第一次调用之前:#EMPTY#EMPTY#EMPTY
第一次调用后:#NewString#EMPTY#EMPTY
第二次调用后:#EMPTY#NewString#EMPTY
第三次调用后:#EMPTY#EMPTY#NewString
printwriter (PrintWriter saver = new PrintWriter(new FileWriter(fileName))) 在保护文件中打开,而不是在主文件中打开,因此每次调用都会打开一个新的 PrintWriter。我 .flush() 和 .close() 在 saver 方法的末尾(这是一个 void 方法)。
为什么在下一次调用该方法之前文件似乎没有保存? =S 我是否必须强制执行某种 wait-until-file-isn't-open-anymore 命令,如果是这样,我该怎么做?
public static void main(String[] args) throws IOException {
SaveGame.saveState("adventure/save/s1.save", new Adventure(), 0);
SaveGame.saveState("adventure/save/s2.save", new Adventure(), 1);
SaveGame.saveState("adventure/save/s3.save", new Adventure(), 2);
}
然后:
public class SaveGame {
public static void saveState(String fileName, Adventure a, int slot) throws IOException {
//UPDATE MASTER SAVE FILE save.txt
String[] save = new String[3];
try {
Scanner openSave = new Scanner(new FileReader("/adventure/save/save.txt"));
openSave.useDelimiter("#");
save[0] = openSave.next();
save[1] = openSave.next();
save[2] = openSave.next();
openSave.close();
}
catch (FileNotFoundException e) {
save[0] = "EMPTY";
save[1] = "EMPTY";
save[2] = "EMPTY";
}
save[slot] = "newString"; //change the CURRENT save in the given slot to the new
PrintWriter updater = new PrintWriter(new FileWriter("adventure/save/save.txt"));
updater.println("#" + save[0] + "#" + save[1] + "#" + save[2]);
updater.flush();
updater.close();
最佳答案
读者读取文件/adventure/save/save.txt
,而作者写入adventure/save/save.txt
。除非您从文件系统的根目录 (/
) 运行该程序,否则这些文件不是相同的文件。
应用 DRY 原则(不要重复自己)。创建一个包含文件路径的常量,并在您使用该路径的任何地方使用该常量。这将避免此类错误。
此外,在 finally block 中关闭读取器和写入器,或使用 Java 7 try-with-resources 结构。
关于java - PrintWriter:读取和写入同一文件 - 文件在再次打开之前似乎未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032624/
在这个程序中,第三个字符串永远不会被打印出来。为什么? (此 Java 程序在 Ubuntu 10.10 上的 Eclipse Indigo 上运行。) import java.io.PrintWri
我已经使用 Java 执行任务和支持工具有一段时间了,但我从未弄清楚如何在一个类中使用 PrintWriter ,然后我可以从另一个类中使用它,例如,我我有几个要解析的 XML 文件,然后从单个输入源
Java PrintWriter 方法printf 和format 之间有什么区别吗? 文档说 printf 是一种方便的方法,但如果它的行为与 format 完全一致,所以我不明白它有什么方便之处。
我正在处理一个服务器-客户端程序集,但现在我遇到了协议(protocol)问题,因为 println 语句末尾的新行混淆了元数据。 换句话说,我需要打印到 PrintWriter,末尾不带新行。我尝试
我想知道有没有可能做 new PrintWriter(new BufferedWriter(new PrintWriter(s.getOutputStream, true))) 在 Java 中,s
PrintWriter(OutputStream out) 此方法不会自动刷新行。 PrintWriter(OutputStream out, boolean autoFlush) 此方法执行自动行冲
我只是想将二维数组“拼图”写入文件。我有一个双 for 循环,它读取数组中的每个“char”值,并将它们写入文件。我似乎无法在我的代码中找到错误。该文件说它在我运行程序时已被修改,但它仍然是空白的。谢
我正在尝试从数据库写入一个大文件文本,大约 200 万行。我观察到,当 PrintWriter 打印 (println()) 行时,文件大小始终为 0 字节。流关闭后文件具有大小数据。它导致所有数据首
public PrintWriter(OutputStream out, boolean autoFlush) : out - An output stream autoFlush - A boole
我的PrintWriter对象编写器的print()方法不起作用,我导入了所有适当的类,但CSV文件一直显示为空白。谁能告诉我为什么? public class StateStats { st
我在这里遇到一个问题:我让这个程序打印 0 到 1000 之间的所有偶数,但是在 friend 做了一些更改之后,它现在只打印“0”。我已经使用它一段时间了,无法弄清楚这是循环问题还是打印机问题。我从
try{ pw = new PrintWriter("C:\\Users\\SDSAD\\Desktop\\java\\file.txt"); }catch(Exception exc){
与我的问题相关的代码功能的主要摘要:该代码采用国会成员数组并以两种不同的方法对它们进行排序;一种按名字排序,一种按平均支持率排序。然后,代码将两个新排序的数组输出到 txt 文件中。 我正在处理的问题
我正在尝试解析一些链接,然后将信息存储在文本文件中,我拥有应在列表中解析的所有链接,但是在解析和存储有关 100 个链接的信息后,我收到了我真的无法理解的错误为什么会发生这种情况,这是我的代码: fo
我真的不明白问题出在哪里。我想将字符打印到文本文件中,并且我使用 printWriter 来执行相同的操作。如果文件有一个“;”,我想用一个新行替换它,这就是我正在做的, public static
我的问题非常简单,如果我有一个长字符串,其中有很多“\n”换行符,即: strings = "Hey\nThere\nFriend\n" 并使用 Java 中的 PrintWriter 执行以下操作:
我有一个将信息存储到二叉树中的程序。 我正在尝试使用 PrintWriter 将所有信息打印到文本文件中,这在以前的情况下对我有用,但这次我没有运气。 必须通过对象在节点中调用信息。我已经调试并确定树
This question already has answers here: PrintWriter writes only partial text (5 个回答) 7 个月前关闭。 我在某个网站
您好,正在开发一个将数据写入文件的小程序。我使用 if else 语句进行验证,因此我需要在单独的方法中使用 PrintWriter 和 FileWriter 类/声明。然后我使用主类的构造函数调用此
我需要打印 100 多行数据和统计数据。我正在使用: PrintWriter p = new PrintWriter(fileName); 之所以大约有 100 行,是因为我需要打印到系统和文件(每个
我是一名优秀的程序员,十分优秀!