Java 薛定谔&x27;s bug,BufferedWriter不';除非手动检查,否则不能写入TXT
我是一个新手程序员,遇到了这个我找不到答案的问题。Java 薛定谔&x27;s bug,BufferedWriter不';除非手动检查,否则不能写入TXT,java,eclipse,lwjgl,slick,bufferedwriter,Java,Eclipse,Lwjgl,Slick,Bufferedwriter,我是一个新手程序员,遇到了这个我找不到答案的问题。 我使用Eclipse,对于程序,我使用slick和lwjgl-2.9.3 以下代码处于公共void更新(…)中的状态 这部分代码有问题: (file.txt存在并且名称中没有大写字母,giveToFile是一个字符串)(没有引发异常) (编辑: 产生相同的错误) 我在try块的末尾放置了一个System.out.print,它正常运行,并且只运行一次。我还使用了g.drawString,并且giveToFile总是给出预期的字符串。我做了以下两
我使用
Eclipse
,对于程序,我使用slick
和lwjgl-2.9.3
以下代码处于公共void更新(…)中的状态 这部分代码有问题:
(file.txt存在并且名称中没有大写字母,giveToFile是一个字符串)(没有引发异常) (编辑: 产生相同的错误) 我在try块的末尾放置了一个System.out.print,它正常运行,并且只运行一次。我还使用了g.drawString,并且
giveToFile
总是给出预期的字符串。我做了以下两个实验。(该程序是一个类似于游戏的东西,你在最后根据你的表现得到一个分数,它将其放入高分,然后重写TXT文件。)(我建议在之前阅读TLDR。)
实验1(file.txt:“0”)(成功):
-从txt加载的字符串:“0” -giveToFile(字符串):“15 0 0”
-从文本“15 0 0”开始的字符串 -giveToFile(字符串):“30150”
-从txt加载的字符串:“30 15 0 0” -giveToFile(字符串):“30150”
-从txt加载的字符串:“0” -giveToFile(字符串):“15 0 0”
-从文本“15 0 0”开始的字符串 -giveToFile(字符串):“30150”
-从txt开始的字符串:“15 0 0”
-giveToFile(字符串):“15 0 0”
如果((xpos>=200&&xpos=100&&ypos您需要关闭块中的BufferedWriter
)
或者,您可以在完成写入后try
块中flush
您的BufferedWriter
,尽管close
操作将首先对其进行刷新
下面是一个重温的示例,Java 6-styled:
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("src/file.txt"));
bw.write(giveToFile);
// bw.flush(); // if needed
}
catch(IOException e){
e.printStackTrace();
}
finally {
if (bw != null) {
try {
bw.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
…和Java 7样式(“试用”和AutoClosable
s):
Java区分文件和资源。资源位于类路径上,可能位于jar中。因此,资源应被视为只读的。它也可能被缓存
在您的情况下,使用资源(在阅读时)是不可取的
我会做如下的事情
String userDir = System.getProperty("user.home");
Path txtPath = Paths.get(userDir, "file.txt");
List<String> content = Files.readAllLines(txtPath, StandardCharsets.UTF_8);
Files.write(txtPath, content, StandardCharsets.UTF_8);
giveToFile = content.get(0);
content = Collections.singletonList(giveToFile);
stringuserdir=System.getProperty(“user.home”);
Path txtPath=Path.get(userDir,“file.txt”);
列表内容=Files.readAllLines(txtPath,StandardCharsets.UTF_8);
write(txtPath、content、StandardCharsets.UTF_8);
giveToFile=content.get(0);
content=Collections.singletonList(giveToFile);
这使文件不在类路径中,因此可以在jar中创建应用程序
您可以维护一个初始只读文件.txt作为资源,用作初始模板,并将其复制到用户目录
我在这里使用了UTF-8编码,因此文件格式是可移植的。耶!!!在尝试了所有您所说的内容之后,深入研究这个主题,并花近2天的时间研究这个问题,我找到了解决方案
该文件仍然位于相同的src
中,因此其路径为src/file.txt
,但在bin文件夹中自动创建了一个同名文件,删除一个会导致两个文件都被删除。简言之,这两个文件在某种程度上是相同的
对于我用来读取文件的代码:
try{
InputStream is = state1.class.getResourceAsStream("/file.txt");
Scanner fileIn = new Scanner(is);
for(int i=0; i<SCOREMAX; i++){
scoreInt[i] = fileIn.nextInt();
}
fileIn.close();
}catch (Exception e) {
e.printStackTrace();
}
再次注意,我也没有在这里使用src/file.txt
,而是将它指向bin
文件夹中的文件
成功了
最后,我在/*…*/标记中得到了约70行代码,标记为“//几乎有效”,并且至少是我已经删除的代码的10倍。但最后它还是有效的
谢谢大家在这里发表评论和回答,没有你们,我将无法找到解决方案。再次感谢大家!在bw.flush();
刚刚bw.close();
@BrettWalkerclose
调用flush
之前,你累了吗您在eclipse上打开的副本配置为在程序执行后自动同步到文件系统。这将反转磁盘上的文件副本
try{
InputStream is = getClass().getResourceAsStream("/file.txt");
Scanner fileIn = new Scanner(is);
for(int i=0; i<SCOREMAX; i++){
scoreInt[i] = fileIn.nextInt();
}
fileIn.close();
}catch (Exception e) {
e.printStackTrace();
}
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("src/file.txt"));
bw.write(giveToFile);
// bw.flush(); // if needed
}
catch(IOException e){
e.printStackTrace();
}
finally {
if (bw != null) {
try {
bw.close();
}
catch (Throwable t) {
t.printStackTrace();
}
}
}
try (BufferedWriter bw = new BufferedWriter(new FileWriter("src/file.txt"))) {
bw.write(giveToFile);
bw.flush();
}
catch(IOException e){
e.printStackTrace();
}
String userDir = System.getProperty("user.home");
Path txtPath = Paths.get(userDir, "file.txt");
List<String> content = Files.readAllLines(txtPath, StandardCharsets.UTF_8);
Files.write(txtPath, content, StandardCharsets.UTF_8);
giveToFile = content.get(0);
content = Collections.singletonList(giveToFile);
try{
InputStream is = state1.class.getResourceAsStream("/file.txt");
Scanner fileIn = new Scanner(is);
for(int i=0; i<SCOREMAX; i++){
scoreInt[i] = fileIn.nextInt();
}
fileIn.close();
}catch (Exception e) {
e.printStackTrace();
}
FileOutputStream fs = null;
try{
fs = new FileOutputStream("bin/file.txt");
OutputStreamWriter ow = new OutputStreamWriter(fs);
BufferedWriter bw = new BufferedWriter(ow);
bw.write(giveToFile);
bw.flush();
bw.close();
}catch (IOException e){
e.printStackTrace();
}finally{
try{fs.close();} catch(IOException e){e.printStackTrace();}
}