简体   繁体   English

我在将 ArrayLists 的内容写入文件时遇到问题

[英]I have a problem with writing the content of ArrayLists into a file

So I wrote some code for a vocabulary trainer for my german class and want to write the content of my ArrayLists to a file.因此,我为德语课的词汇训练器编写了一些代码,并希望将 ArrayLists 的内容写入文件。 However it only writes the first of the 3 ArrayLists into the file when saving.但是,它只在保存时将 3 个 ArrayList 中的第一个写入文件。 Does anyone know what causes this, or better yet, how to fix it?有谁知道是什么原因造成的,或者更好的是,如何解决它? Thanks for you help!谢谢你的帮助!

I have already reset all the ArrayLists and re-implemented the file it should write into, but nothing helped.我已经重置了所有的 ArrayLists 并重新实现了它应该写入的文件,但没有任何帮助。

These are all just sequences of Code, not the whole program.这些都只是代码序列,而不是整个程序。 It is over 400 lines Long so I didnt want to paste the whole thing.它超过 400 行,所以我不想粘贴整个内容。 The Code runs flawlessly until I open the file I wrote into.代码运行完美,直到我打开我写入的文件。

static ArrayList<String> vokabel = new ArrayList<String>();
static ArrayList<String> uebersetzung = new ArrayList<String>();
static ArrayList<Integer> kasten = new ArrayList<Integer>(); 

static void beenden() {
  for(int m = 0; m < groesse; m++) {
    String str = vokabel.get(m).toString();
    textWriter.write(str);
    textWriter.write(" ");
  }
  textWriter.close();
  textWriter.println();
  for(int n = 0; n < groesse; n++) {
    String str = uebersetzung.get(n).toString();
    textWriter.write(str);
    textWriter.write(" ");
  }
  textWriter.close();
  textWriter.println();
  for(int o = 0; o < groesse; o++) {
    String str = kasten.get(o).toString();
    textWriter.write(str);
    textWriter.write(" ");
  }
  textWriter.close();
  textWriter.println();

  System.exit(0);

}

I expect it to write the content of all 3 ArrayLists into the file, though it didn't work up until now.我希望它将所有 3 个 ArrayLists 的内容写入文件,尽管它直到现在才起作用。 This is what ends up in the file after entering 3 words with their translations and their corresponding case number.这是在输入 3 个单词及其翻译和相应的案例编号后文件中的最终结果。 Only the words themselves make it into the file:只有单词本身才能进入文件:

Hund Nein Hallo Hund Nein 你好

The reason it only writes the first ArrayList into the file is because you're closing the TextWriter immediately after writing it (and when the TextWriter is closed, it doesn't write stuff).它只将第一个ArrayList写入文件的原因是因为您在写入后立即关闭TextWriter (并且当TextWriter关闭时,它不会写入内容)。 Just remove all the只需删除所有

textWriter.close();

lines, and then put just one right before System.exit(0) , and it should work properly.线,然后把只有一个正确的System.exit(0)它应该正常工作。

You write all 3 lists one after the other.您一个接一个地编写所有 3 个列表。 AND: Inbetween you close the writer!并且:在这之间你关闭了作家! So you are "lucky" not to get an IOException.所以你很“幸运”没有得到 IOException。

I guess you would like the entries of the 3 lists together (vokabel + uebersetzung+ kasten).我猜您想将 3 个列表的条目放在一起(vokabel + uebersetzung + kasten)。 Therefore I suggest you create a class taking 3 fields with the information.因此,我建议您创建一个包含 3 个字段的类,其中包含信息。 Give this class a sensible toString() and simply write those objects one line at a time.给这个类一个合理的 toString() 并且一次只写一行。

Ah and btw: Don't ever call System.exit!啊,顺便说一句:永远不要调用 System.exit! It makes your program unusable in a larger context and prevents proper cleanup of resources.它使您的程序在更大的上下文中无法使用并阻止正确清理资源。

Use Flush.when you have text longer than buffer size it will not write.使用 Flush。当文本长度超过缓冲区大小时,它不会写入。

System.IO.TextWriter writeFile = new StreamWriter("c:\\textwriter.txt");
writeFile.WriteLine("csharp.net-informations.com");
writeFile.Flush();
writeFile.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM