to edit a specific line in a textfile using java program

强颜欢笑 提交于 2019-12-04 12:47:21
Rahul Agrawal

Just created an example for you

public static void main(String args[]) {
        try {
            // Open the file that is the first
            // command line parameter
            FileInputStream fstream = new FileInputStream("d:/new6.txt");
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
            String strLine;
            StringBuilder fileContent = new StringBuilder();
            //Read File Line By Line
            while ((strLine = br.readLine()) != null) {
                // Print the content on the console
                System.out.println(strLine);
                String tokens[] = strLine.split(" ");
                if (tokens.length > 0) {
                    // Here tokens[0] will have value of ID
                    if (tokens[0].equals("2")) {
                        tokens[1] = "betty-updated";
                        tokens[2] = "499";
                        String newLine = tokens[0] + " " + tokens[1] + " " + tokens[2] + " " + tokens[3];
                        fileContent.append(newLine);
                        fileContent.append("\n");
                    } else {
                        // update content as it is
                        fileContent.append(strLine);
                        fileContent.append("\n");
                    }
                }
            }
            // Now fileContent will have updated content , which you can override into file
            FileWriter fstreamWrite = new FileWriter("d:/new6.txt");
            BufferedWriter out = new BufferedWriter(fstreamWrite);
            out.write(fileContent.toString());
            out.close();
            //Close the input stream
            in.close();
        } catch (Exception e) {//Catch exception if any
            System.err.println("Error: " + e.getMessage());
        }
    }

One solution could be to read in the file, line-by-line, manipulate the lines you need (performing some parsing/tokenization to get the ID/name/etc.), then write all the lines into the file (overwriting its current content). This solution depends on the size of the file you are working with: too large a file will consume a lot of memory as you are holding all its contents in memory at once

Another approach (to cut down on memory requirements) is to process the file lin-by-line, but instead of holding all lines in memory, you write the current line to a temporary file after processing of each line, then move the temporary file to the location of the input file (overwriting that file).

The classes FileReader and FileWriter should help you with reading/writing to the file. You might want to wrap them in a BufferedReader/BufferedWriter to improve performance.

Also, don't forget to close the reader (also, the writer) when done reading (writing) the file, so consequent accesses to the file are not blocked due to the file still being open

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!