【问题标题】:File Becomes Empty After Overwriting It文件覆盖后变为空
【发布时间】:2019-06-13 07:57:52
【问题描述】:

所以我在我的项目中编写了一个代码,我在一个单独的文件中存储了一些行中的投票。然后我读取该文件并将行中的投票提取到一维数组中,然后我必须像用户输入 1 一样增加它们,第一行添加 1 个投票,如果用户输入 2,则增加一个投票到2行以此类推。之后,我必须将该递增的数组与添加的投票存储到最初提取它们的同一文件(覆盖)中。

我面临两个问题,首先,当用户输入 1 时,实际上执行了 7 次增量,因为循环运行了 7 次,因为数组长度为 7。第二个问题是,文件被覆盖后,它显示结果在输出中增加了投票,但文件变为空,当它再次运行时,它显示 0,0,0,0 .... 即使在最后使用 output.close() 之后也是如此。

请建议,我明天要提交项目,还有很多要写的,如果有人可以请借2.3小时帮助也很棒。

public static int[] voteCasting(String resultFile) {

     String[] votesArray = new String[7];
     int[] votesConverted = new int[votesArray.length];

     try {
         String partyVotes = "";
         int castedVotes;

         int i = 0;

         Scanner uI = new Scanner(System.in);
         Scanner rF = new Scanner (new File(resultFile));


         int userInput = uI.nextInt();

         while (rF.hasNext()) {
             partyVotes = rF.next();
             votesArray[i]= partyVotes;
             votesConverted[i] = Integer.parseInt(votesArray[i]);
             i++;
             if (userInput == 1) {
                 votesConverted[0] = ++votesConverted[0];

             }
             else if (userInput == 2) {
                 votesConverted[1] = votesConverted[1]++;
             }
             else if (userInput == 3) {
                 votesConverted[2] = votesConverted[2]++;
             }
             else if (userInput == 4) {
                 votesConverted[3] = votesConverted[3]++;
             }
             else if (userInput == 5) {
                 votesConverted[4] = votesConverted[5]++;
             }
             else if (userInput == 6) {
                 votesConverted[6] = votesConverted[6]++;
             }
             else if (userInput == 7) {
                 votesConverted[7] = votesConverted[7]++;
             }

         }

         PrintWriter wF = new PrintWriter(resultFile);

         while ( rF.hasNext()) {
             wF.write(votesConverted[i]);
             i++;
         }
         wF.flush();
         wF.close();


     } catch (IOException ex) {
          Logger.getLogger(ProjectTesting.class.getName()).log(Level.SEVERE, null, ex);
     }
     return votesConverted;
 }

【问题讨论】:

  • 为什么你认为第二个while ( rF.hasNext()) 会循环播放?你已经用尽了rf的所有token,所以在rF.hasNext()返回false并结束第一个循环之后,你为什么相信它会因为你再次调用它而突然返回true?
  • 我该怎么办? ....

标签: java arrays file-handling


【解决方案1】:

这样的覆盖是行不通的——当文件被打开写入时,它实际上被清空了。打开文件进行追加时不会发生这种情况,但这似乎不是您想要的。您需要写入不同的文件,然后在最后删除原始文件并将新文件移动到其位置。

【讨论】:

  • 我该怎么做?
  • 追加也是我能做的,我只需要用+1增量更新我在前一个文件中的投票,无论是覆盖还是追加。
【解决方案2】:

你的问题在于这个循环:

while ( rF.hasNext()) {
   wF.write(votesConverted[i]);
   i++;
 }

在之前的循环中rF.hasNext() 已经是假的了。

...
while (rF.hasNext()) {
   partyVotes = rF.next();
   votesArray[i]= partyVotes;
....

所以没有什么可写的。相反,您可以尝试:

i=0;
while ( i<7) {
   wF.write(votesConverted[i]);
   i++;
}

【讨论】:

  • 我试过你的代码,它将这些符号存储在文件“#”中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-29
  • 1970-01-01
  • 1970-01-01
  • 2020-07-03
  • 2014-04-30
  • 1970-01-01
相关资源
最近更新 更多