使用java程序编辑文本文件中的特定行 [英] to edit a specific line in a textfile using java program

查看:143
本文介绍了使用java程序编辑文本文件中的特定行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我说有一个名为people.txt的文本文件,它包含以下信息:

Ok, say I have a text file called "people.txt", and it contains the following information:

 1 adam 20 M
 2 betty 49 F
 3 charles 9 M
 4 david 22 M
 5 ethan 41 M
 6 faith 23 F
 7 greg 22 M
 8 heidi 63 F

基本上,第一个数字是该人的身份证明,然后是这个人的姓名,年龄和性别。假设我想用不同的值替换第2行或ID号为2的人。现在,我知道我不能使用 RandomAccessFile ,因为名称并不总是相同的字节数,也不是年龄。在搜索随机Java论坛时,我发现 StringBuilder StringBuffer 应该足以满足我的需求,但我不确定如何实现。它们可以用来直接写入文本文件吗?我希望这可以直接从用户输入工作。

Basically, the first number is the ID of the person, then comes the person's name, age and gender. Say I want to replace line 2, or the person with ID number 2 with different values. Now, I know I cant use RandomAccessFile for this because the names are not always the same number of bytes, neither are the ages. While searching random Java forums, I found that StringBuilder or StringBuffer should suffice for my needs, but I'm not sure how to implement either. Can they be used to directly write to the text file? I want this to work directly from user input.

推荐答案

刚为您创建了一个示例

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());
        }
    }

这篇关于使用java程序编辑文本文件中的特定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆