java - 覆盖java txt文件中的一行

标签 java arrays file io

所以我创建了这个方法,它最后显示整行,因为我在转换和编辑数组后显示数组。所以我的问题是我怎么知道将数组覆盖到我从中获取它的同一行。提前致谢,这是我的代码。

       public void getData(String path, String accountNumber) {
    try {
        FileInputStream fis = new FileInputStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        System.out.println("Please Enter the Deposit amount That you would like to add.");

        Scanner sn = new Scanner (System.in);

         int add = sn.nextInt();

        String str;

        while ((str = br.readLine()) != null) {
            if (str.contains(accountNumber)) {
                String[] array = str.split(" ");
                int old = Integer.parseInt(array[3]);
                int Sum= old + add;
                String Sumf = Integer.toString(Sum);
                array[3] = Sumf;

                for (int i = 0; i < array.length; i++) {
                System.out.println(array[i]);}

                break;
            }
        }
    } catch (Exception ex) {
        System.out.println(ex);
    }


}

我正在使用字符串 accountNumber 来获取我需要的特定行。获取该行后,我将其更改为数组,同时使用 str.split(""); 拆分索引; 。之后我知道我需要编辑索引号[3]。所以我这样做,然后将其放回数组中。我需要做的最后一件事就是立即恢复阵列。

最佳答案

您可以跟踪正在读取的文件中的输入,并使用修改后的版本将其写回。

public void getData(String path, String accountNumber) {
    try {
        FileInputStream fis = new FileInputStream(path);
        BufferedReader br = new BufferedReader(new InputStreamReader(fis));

        System.out.println("Please Enter the Deposit amount That you would like to add.");

        Scanner sn = new Scanner (System.in);

        int add = sn.nextInt();

        String line; // current line
        String input = ""; // overall input

        while ((line = br.readLine()) != null) {
            if (line.contains(accountNumber)) {
                String[] array = line.split(" ");
                int old = Integer.parseInt(array[3]);
                int Sum= old + add;
                String Sumf = Integer.toString(Sum);
                array[3] = Sumf;

                // rebuild the 'line' string with the modified value
                line = "";
                for (int i = 0; i < array.length; i++)
                    line+=array[i]+" ";
                line = line.substring(0,line.length()-1); // remove the final space
            }

            // add the 'line' string to the overall input
            input+=line+"\n";
        }

        // write the 'input' String with the replaced line OVER the same file
        FileOutputStream fileOut = new FileOutputStream(path);
        fileOut.write(input.getBytes());
        fileOut.close();
    } catch (Exception ex) {
        System.out.println(ex);
    }
}

关于java - 覆盖java txt文件中的一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34562770/

相关文章:

java - Android 需要深入的 Java 知识吗?

java - 如何在没有 Opengl ES 的情况下为对象制作动画?

java - 自动缩进 Java 和 HTML 文件的脚本或程序

javascript - 为什么这些数组按值传递而不是按引用传递?

arrays - 以循环方式移动序列的最佳实践

c - 通过从堆中删除值并向堆中插入值来同步文件和堆中的数据

python - 从 Python 中打开的 Excel 文件中读取

java - 如何比较具有不同顺序元素的 Json 数组

php - 从数组中的文件名中提取基名

android - 无法使用 OkHttp3 在文件管理器中选择文件