简体   繁体   English

替换 Java 中文本文件中的特定字符串

[英]Replacing a specific string from a text file in Java

I've got a text file like this: Image我有一个这样的文本文件:图片

The first column represents the user ID and the last column represents balance.第一列代表用户 ID,最后一列代表余额。 I want to iterate through the elements to find a specific user ID and then update their balance.我想遍历元素以找到特定的用户 ID,然后更新他们的余额。 I've managed to match the user ID and access the balance using the Scanner but I'm not sure how to update the balance in the text file with a new value.我已设法匹配用户 ID 并使用扫描仪访问余额,但我不确定如何使用新值更新文本文件中的余额。 This is the code I've written so far:这是我到目前为止编写的代码:

        File file = new File("UserInfo.txt");
        Scanner sc = new Scanner(file);
        
        while(sc.hasNextLine()){
            String info = sc.nextLine();
            String data[] = info.split(" ");
            //userId is a function argument
            if(String.valueOf(userId).equals(data[0])){
               //I want to update the balance in the text file with a new value here
            }else{
                continue;
            }
        }
        sc.close();

it seems that you want to create a CSV file.您似乎想创建一个 CSV 文件。 My advice is to use ";"我的建议是使用“;” as a separator for the split() command, and if the file is not too big, read the whole file, put it into an arrayList of Strings and then manipulate It, rewriting the whole file at the end from the arrayList.作为 split() 命令的分隔符,如果文件不是太大,则读取整个文件,将其放入 Strings 的 arrayList 中,然后对其进行操作,最后从 arrayList 重写整个文件。 Otherwise you have to use RandomAccess class否则你必须使用 RandomAccess class

Replacing string in file is not so simple.替换文件中的字符串并不是那么简单。 You have to read whole file and rewrite part that you want to leave unchanged.您必须读取整个文件并重写您想要保持不变的部分。 Only when condition of comparing userID and data[0] is met, you would use replace() function to change user balance.只有当比较userIDdata[0]的条件满足时,你才会使用replace() function 来改变用户余额。

Your file is basically CSV file with space as line separator, as already mentioned store file contents in array or BufferedReader , change appropriate line and then export back to CSV.您的文件基本上是 CSV 文件,其中空格作为行分隔符,如前所述,将文件内容存储在数组或BufferedReader中,更改适当的行,然后导出回 CSV。

        File file = new File("UserInfo.txt");
        Scanner sc = new Scanner(file);
        ArrayList<String> items = new ArrayList<String>(); 
        while(sc.hasNextLine()){
            String info = sc.nextLine();
            String data[] = info.split(" ");
            
            if(String.valueOf(currentUser.getAccountNumber()).equals(data[0])){
                data[3] = String.valueOf(currentUser.getBorrowBalance()); 
                //Updating the balance in ArrayList
            }else{
                continue;
            }
            for(int i=0; i<data.length; i++){
                items.add(data[i]);
            }
        }
        PrintWriter pw = new PrintWriter(file);
        for(int j=0; j<items.size(); j++) {
            pw.printf("%s ", items.get(j));
            if(j%3==0) pw.println(); //Going to new line after writing 4 elements to text file
        }
        pw.close();
        sc.close();

So these are the changes I've made so far.所以这些是我到目前为止所做的改变。 I tried to take the elements, modify the balances and put the updated values in an arraylist.我尝试获取元素,修改天平并将更新的值放入 arraylist 中。 Then put them into the text file using PrintWriter.然后使用 PrintWriter 将它们放入文本文件中。 But I keep getting an empty text file when I call this function.但是当我调用这个 function 时,我总是得到一个空的文本文件。

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

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