【问题标题】:edit line in a text file编辑文本文件中的行
【发布时间】:2016-03-06 08:31:08
【问题描述】:

我已经尝试过我在互联网上找到的这段代码

File file = new File("file.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = "", oldtext = "";
        while((line = reader.readLine()) != null)
            {
            oldtext += line + "\n";

        }
        reader.close();
        // replace a word in a file
        //String newtext = oldtext.replaceAll("drink", "Love");

        //To replace a line in a file
        String replace = JOptionPane.showInputDialog("Enter what to replace: ");
        String toreplace = JOptionPane.showInputDialog("Enter where to replace: ");
        String newtext = oldtext.replaceAll(replace, toreplace);

        FileWriter writer = new FileWriter("file.txt");
        writer.append(newtext);writer.close();

但我的输出不会像这段代码那样。这段代码的输出是这样的:

取消编辑:

jojo moyes
kim possible
dexter laboratory

edited:当我输入“mary”来编辑“kim”时

jojo moyes
mary possible
dexter laboratoty

但我的会是这样的

jojo moyes
kim possible
dexter laboratoy
mary possible

我在编辑之前已经注册了。并且在寄存器中也有时间将某些内容存储在文本文件中。如果用户想在他输入的信息中编辑某些东西(你得到图片),那么编辑功能就会出现

已编辑:这是我的代码

public void Register_Edit_Info() throws IOException{
        FileWriter writeFile=new FileWriter("voters.txt", true);
        BufferedWriter outFile=new BufferedWriter(writeFile);
        File readFile=new File("voters.txt");   
        BufferedReader read=new BufferedReader(new FileReader(readFile));

        String choice2;
        String [] secondMenu = {"Register", "Edit", "Delete", "Back"};

        do{
            choice2=(String)JOptionPane.showInputDialog(null, "Please choose:", "Election 2765", 1, null, secondMenu, secondMenu[0]);
            switch(choice2){
            case "Register":
                String [] menuGender={"Male", "Female"};
                String [] menuStatus={"Single", "Married", "Widow(er)", "Legally separated"};

                do{
                age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
                while(age<18){
                    JOptionPane.showMessageDialog(null, "Voter should be 18 or above");
                    age=Integer.parseInt(JOptionPane.showInputDialog("Age: "));
                }
                name=JOptionPane.showInputDialog("Full Name: ");
                gender=(String)JOptionPane.showInputDialog(null, "Gender:", "Election 2765", 1, null, menuGender, menuGender[0]);
                if(gender=="Male"){
                    gender="Male";
                }
                else{
                    gender="Female";
                }
                dBirth=JOptionPane.showInputDialog("Date of Birth: ");
                pBirth=JOptionPane.showInputDialog("Place of Birth: ");
                address=JOptionPane.showInputDialog("Address\n(Province, City/Municipality, Barangay, House No./Street: ");
                status=(String)JOptionPane.showInputDialog(null, "Civil Status:", "Election 2765", 1, null, menuStatus, menuStatus[0]);
                if(status=="Single"){
                    status="Single";
                }
                else if(status=="Married"){
                    spouse=JOptionPane.showInputDialog("Spouse Name: ");
                    status="Married(Spouse: "+spouse+")";
                }
                else if(status=="Widow(er)"){
                    status="Widow(er)";
                }
                else{
                    status="Legally Separated";
                }
                citizenship=JOptionPane.showInputDialog("Citizenship:");
                job=JOptionPane.showInputDialog("Profession/Occupation: ");
                tin=JOptionPane.showInputDialog("Tin Number: ");
                father=JOptionPane.showInputDialog("Father's Full Name: ");
                mother=JOptionPane.showInputDialog("Mother's Full Name: ");
                votersNumber++; 

                vNumber=Integer.toString(votersNumber);

                outFile.append(vNumber+"/"+name+"/"+age+"/"+gender+"/"+dBirth+"/"+pBirth+"/"+address+"/"+status+"/"+citizenship+"/"+job+"/"+father+"/"+mother);
                outFile.newLine();

                selectYN=JOptionPane.showInputDialog("You are now registered. Do you want to register more?\n[1]Yes [2]No");
                }while(!"2".equals(selectYN));

                break;
            case "Edit":
                vNumForEdit=JOptionPane.showInputDialog("Enter voters number: ");
                String line=null, oldtext="";

                while((line=read.readLine())!=null){
                    oldtext+=line+"\n";

                    String [] info=line.split("/");
                    if(info[0].matches(vNumForEdit)){
                        String [] forEditMenu={"Name", "Age", "Gender", "Date of Birth", "Place of Birth", "Address", "Civil Status", "Citizenship", "Profession/Occupation", "Father's Name", "Mother's Name"};
                        forEdit=(String)JOptionPane.showInputDialog(null, line+"\n\nPlease select what you want to edit", "National Election 2765", 1, null, forEditMenu, forEditMenu[0]);
                        switch(forEdit){
                        case "Name":
                            oldName=JOptionPane.showInputDialog("Enter old name: ");
                            newName=JOptionPane.showInputDialog("Enter new name: ");

                            String newText = oldtext.replaceAll(oldName, newName);
                            outFile.append(newText);                            
                            break;
                        }
                    }
                }
            case "Delete":
                break;
            }
        }while(choice2!="Back");
        read.close();
        outFile.close();
    }

【问题讨论】:

  • 所以,你有一些显示一些输出的代码,而输出是错误的。但我们不知道: - 你的代码是什么, - 它应该做什么, - 你的输入是什么。那么我们能提供什么帮助呢?
  • 你试过用 writer.write(newtext) 代替吗?追加将您的新文本添加到现有文本的末尾,而写入将覆盖以前的文本。这似乎不是问题,但仍然值得一试。
  • @JBNizet 我已经发布了代码嘿嘿嘿 :)
  • @chalarangelo 我现在试过了,但还是一样

标签: java file-handling


【解决方案1】:

此答案适用于您问题的第一部分。(编辑前)。

public static void main(String[] args) throws FileNotFoundException,IOException {
        File file = new File("file.txt");
        BufferedReader reader = new BufferedReader(new FileReader(file));
        String line = "", oldtext = "";
        while((line = reader.readLine()) != null)
            {
            oldtext += line + "\n";

        }
        reader.close();
        System.out.println(oldtext);
        // replace a word in a file
        //String newtext = oldtext.replaceAll("drink", "Love");

        //To replace a line in a file
        String replace = JOptionPane.showInputDialog("Enter what to replace: ");
        String toreplace = JOptionPane.showInputDialog("Enter where to replace: ");
        String newtext = oldtext.replaceAll(replace, toreplace);

        System.out.println(newtext);
        java.io.FileWriter writer = new java.io.FileWriter("file1.txt");
        writer.write(newtext);
        writer.close();
    }

当第一次提示打开时,我写“kim”和在哪里替换,我写“marry”和这样的输出。我认为您的代码很好,除了不使用 append() 用于 FileWriter。您应该为 FileWriter 使用write() 方法。

编辑:FileWriter 使用不同的文件名(我不知道是否对同一个文件进行读写操作。)和初始化你可以使用

FileWriter writeFile=new FileWriter("voters1.txt");

如果问题解决了,请告诉我。

【讨论】:

  • 我编辑了我的问题并发布了我的代码,请他们检查一下,因为当我尝试使用 write() 时,它仍然是一样的。您尝试运行的代码是我在一个完美运行的站点中的代码。但是当我在我的代码中实现它时,它就不再起作用了
  • 我应该再写一个FileWriter 还是只是更改文件名?
  • 改个名字就行了。
  • 哦,什么也没发生 :(
猜你喜欢
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 2012-01-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多