2016-03-06 48 views
-3

我試過這個代碼,我在互聯網編輯線

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

發現,但我的不會這樣的代碼輸出就可以了。這段代碼的輸出是這樣的:

了unedit:

jojo moyes 
kim possible 
dexter laboratory 

編輯:當我進入 「瑪麗」 編輯 「金」

jojo moyes 
mary possible 
dexter laboratoty 

但我的會是這樣

jojo moyes 
kim possible 
dexter laboratoy 
mary possible 

我的壽命在編輯前已經註冊。並且在寄存器中也有時間將它存儲在文本文件中。而且,如果用戶想要的信息進行編輯的東西,他進入(你的圖片)

EDITED而來的編輯功能:這裏是我的代碼

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

所以,你有一些代碼顯示一些輸出,並且輸出是錯誤的。但我們不知道: - 你的代碼是什麼, - 它應該做什麼, - 你的輸入是什麼。那麼我們如何幫助? –

+1

您是否嘗試過使用writer.write(newtext)呢?追加會在已有文本的末尾添加新文本,而寫入將覆蓋以前的文本。這似乎不是問題,但值得一試。 –

+0

@JBNizet我已經發布了代碼hehehe :) – bruh

回答

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

當第一次打開提示我寫的「金」,並在那裏更換,我寫的「嫁」像這樣的輸出。我認爲你的代碼很好,除了不使用FileWriter的append()。您應該爲FileWriter使用write()方法。

enter image description here

編輯: 使用不同的文件名(我不知道,如果讀取和寫入操作發生的同一文件)FileWriter和初始化,您可以使用

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

讓我知道問題是否解決。

+0

我編輯了我的問題,我發佈我的代碼他們請檢查它,因爲當我試圖使用'write()'它仍然是一樣的。你試圖運行的是我在一個完美的網站上運行的代碼。但是當我在我的代碼中實現它時,它將不再起作用 – bruh

+0

已編碼的代碼。讓我知道它是否解決。 –

+0

我應該寫另一個'FileWriter'或者只是改變文件名? – bruh