基于off标志替换.txt文件中的行

mitkmikd  于 2021-07-03  发布在  Java
关注(0)|答案(2)|浏览(322)

这个问题不同于通常的“我需要替换一行代码”的问题,至少我希望如此。
我想在一个名为accounts.txt的文本文件中编辑一行代码,而不是使用该行作为替换的标志,我需要使用它上面的行,因为文件的进程是“account number,balance”。感谢您的帮助!这是我目前掌握的情况。

public boolean modifyBalance(int accountNum, int newBalance) {
    try {
      FileReader fileReader = new FileReader("accounts.txt");
      BufferedReader file = new BufferedReader(fileReader);
      String line;
      String input = "";
      String flag;
      boolean foundFlag = false;
      Integer intInstance = accountNum;
      flag = intInstance.toString();

      while ((line = file.readLine()) != null) {
        input += line;
        if (line.equals(flag)) {
          file.readLine();
          input += "\n" + newBalance;
          foundFlag = true;
        }//end if
      }//end while
      return foundFlag;
    } //end try
    catch (IOException e) {
       System.out.println("Input Failure in Modify Balance of Account"       
                           + " Repository.");
       System.exit(0);
       return false;
     }
       // look up inObj in the text file and change the associated 
      // balance to newBalance
   }
bgibtngc

bgibtngc1#

这里有一个方法。
过程:
-将文件的所有行写入 ArrayList -如果你找到了旗子,那就标记行号
-如果您的行号不是-1,则您找到了帐户,然后对 ArrayList 把所有的行写回文件。

public boolean modifyBalance(int accountNum, int newBalance)
{
    int lineNumberOfAccount = -1;
    boolean foundFlag = false;
    BufferedReader file = null;

    List<String> fileLines = new ArrayList<String>();
    try
    {
        FileReader fileReader = new FileReader("accounts.txt");
        file = new BufferedReader(fileReader);
        String line;
        String input = "";
        String flag;

        Integer intInstance = accountNum;
        flag = intInstance.toString();

        int lineNumber = 0;

        while ((line = file.readLine()) != null)
        {
            fileLines.add(line);

            System.out.println(lineNumber + "] " + line);
            // input += line;
            if (line.equals(flag))
            {
                lineNumberOfAccount = lineNumber;
                foundFlag = true;
            } // end if

            lineNumber++;

        } // end while
    } // end try
    catch (IOException e)
    {
        System.out.println("Input Failure in Modify Balance of Account" + " Repository.");
        // don't exit here, you are returning false
        // System.exit(0);
        return false;
    }
    // Close the file handle here
    finally
    {
        if (file != null)
        {
            try
            {
                file.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
    // look up inObj in the text file and change the associated
    // balance to newBalance

    System.out.println("lineNumberOfAccount: " + lineNumberOfAccount);

    // found the account
    if (lineNumberOfAccount != -1)
    {
        int nextLine = lineNumberOfAccount + 1;

        // remove the old balance
        fileLines.remove(nextLine);

        // add the new balance
        fileLines.add(nextLine, String.valueOf(newBalance));

        System.out.println(fileLines);

        // write all the lines back to the file
        File fout = new File("accounts.txt");
        FileOutputStream fos = null;
        BufferedWriter bw = null;
        try
        {
            fos = new FileOutputStream(fout);

            bw = new BufferedWriter(new OutputStreamWriter(fos));

            for (int i = 0; i < fileLines.size(); i++)
            {
                bw.write(fileLines.get(i));
                bw.newLine();
            }
        }
        catch (IOException e)
        {
            System.out.println("Could not write to file");
            return false;
        }
        // Close the file handle here
        finally
        {
            if (bw != null)
            {
                try
                {
                    bw.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

    return foundFlag;
}

笔记:
您需要确保正在关闭文件句柄。
理想情况下,应该将此代码分解为至少两个方法。一种方法用于查找行号,另一种方法用于在找到帐户时将文件写回。
使用时要小心 System.exit() 我在我的代码中对此进行了注解,因为如果您得到一个 IOException . 也可以抛出异常或将其 Package 到 RuntimeException 让调用代码来处理。
你可以考虑 newBalance 变量必须是 double 而不是 int

tzcvj98z

tzcvj98z2#

这里有一些事情要考虑。
如果文件很小,您可以将整个内容读入一个字符串数组(查看Java7文件类的javadocs)。然后你可以前后移动数组来进行更改。然后把修改过的文件写出来。
如果文件很大,您可以从输入中读取数据,然后一次向临时文件写入一行数据(但要将输出延迟一行,这样您就可以触发输入标志)。然后删除旧的输入文件并重命名临时文件。

相关问题