2013-05-05 49 views
3

中含有例如我有一個名爲file.txt包含個人信息(ID,姓名,工資)文件:如何更新.txt文件的Java

A123 Anna 3000 
A234 Alex 4000 
A986 Jame 5000 

我如何寫Java代碼,允許用戶輸入一個人的ID並補充工資? 最終輸出如下:

輸入ID:A123

輸入補充工資:2000中運行該程序後

file.txt的:

A123 Anna 5000 
A234 Alex 4000 
A986 Jame 5000 

這是我做了什麼到目前爲止,但它沒有工作:

public static void addDalary() throws IOException { 
     String ID, Nanme; 
     double salary; 


      Scanner console = new Scanner(System.in); 
      System.out.print("Enter ID : "); 
      String pID = console.nextLine(); 
      System.out.print("Enter replenish salary: "); 
      replenish = console.nextInt(); 

      Scanner input = new Scanner(new File("file.txt")); 
      while (input.hasNext()) { 
       ID = input.next(); 
       Name = input.next(); 
       salary = input.nextDouble(); 

       if (ID == pID) {  
        salary = salary + replenish; 
       } 
      } 
      input.close(); 
     } 

進出口新的這些東西。任何幫助將非常感激。

+0

http://docs.oracle.com/javase/6/docs/api/java/io/RandomAccessFile.html – NINCOMPOOP 2013-05-05 10:30:25

+0

參見[所以,你需要寫一個程序但不知道如何開始](http://www.patriciashanahan.com/beginner.html)並回到我們,當你有一個*特定*問題。 – 2013-05-05 10:30:35

+0

最好的辦法是使用RandonAccessFile類。 可能的重複:http://stackoverflow.com/questions/199847/random-access-file-in-java – 2013-05-05 10:33:02

回答

1

您必須使用從原始文件逐行讀取它的方法。用新號碼搜索並替換工資。將內容寫入新的臨時文件。刪除原始文件。最後將臨時文件替換爲原始文件名。下面是代碼這樣做:

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileReader; 
import java.io.FileWriter; 
import java.io.IOException; 
import java.io.PrintWriter; 
import java.util.Scanner; 

public class FileContentUpdater { 

    public static void main(String args[]) throws IOException { 
     String ID, Name; 
     double salary; 
     int replenish; 

     Scanner console = new Scanner(System.in); 
     System.out.print("Enter ID : "); 
     String pID = console.nextLine(); 
     System.out.print("Enter replenish salary: "); 
     replenish = console.nextInt(); 

     File originalFile = new File("file.txt"); 
     BufferedReader br = new BufferedReader(new FileReader(originalFile)); 

     // Construct the new file that will later be renamed to the original 
     // filename. 
     File tempFile = new File("tempfile.txt"); 
     PrintWriter pw = new PrintWriter(new FileWriter(tempFile)); 

     String line = null; 
     // Read from the original file and write to the new 
     // unless content matches data to be removed. 
     while ((line = br.readLine()) != null) { 

      if (line.contains(pID)) { 
       String strCurrentSalary = line.substring(line.lastIndexOf(" "), line.length()); 
       if (strCurrentSalary != null || !strCurrentSalary.trim().isEmpty()) { 
        int replenishedSalary = Integer.parseInt(strCurrentSalary.trim()) + replenish; 
        System.out.println("replenishedSalary : " + replenishedSalary); 
        line = line.substring(0,line.lastIndexOf(" ")) + replenishedSalary; 
       } 

      } 
      pw.println(line); 
      pw.flush(); 
     } 
     pw.close(); 
     br.close(); 

     // Delete the original file 
     if (!originalFile.delete()) { 
      System.out.println("Could not delete file"); 
      return; 
     } 

     // Rename the new file to the filename the original file had. 
     if (!tempFile.renameTo(originalFile)) 
      System.out.println("Could not rename file"); 

    } 
} 
+0

耶,終於.....,非常感謝你 – surisava 2013-05-05 12:10:33

+0

@Juned請問pw.fluch();的含義是什麼? – kbluue 2016-02-15 19:48:41

+0

什麼是將臨時文件中的所有數據存儲並寫入更多數據,然後將其傳輸到基本文件的含義。不想要的解決方案 – Krishna 2016-04-01 14:29:06

2

一種可能的方式可以是這樣的

  1. 從文件中讀取每一行
  2. 分割線

  3. 準備與更新的數據另一條線,並寫入臨時文件

  4. 最後刪除舊文件並將臨時文件重命名爲舊文件。

  5. 現在你已經完成了。 嘗試上述算法。