2013-05-05 66 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. 现在你已经完成了。 尝试上述算法。