2013-03-20 64 views
0

我想創建一個現金備忘錄爲我的項目。閱讀從一個文本文件中的值,並將其保存爲其他文件

現在我有一個小問題。

如何讀取文本文件中的特定值,然後保存在另一個文本文件中的值。

實施例:

data.txt文件有一些值。

Item Name : m-01 

Item Brand : One Man 

Item size : XXL 

Item Price : 1000 

Item vat : 15% 

這些都是保存在data.txt

數據現在我的計劃會要求項目名稱時,我會寫的項目名稱(例如:M-01) 它只是將採取值1000 (價格)和15(含增值稅),然後將它們保存在一個新的txt文件data2.txt

我怎樣才能做到這一點?

請幫助我。

+0

您應該使用這個CSV文件... – 2013-03-20 14:03:54

+0

開始閱讀有關[掃描儀](HTTP://文檔.oracle.com/JavaSE的/ 6 /文檔/ API/JAVA/util的/ Scanner.html)和[FileOutput](http://docs.oracle.com/javase/6/docs/api/java/io/FileOutputStream。 HTML)。代碼並返回。 – PeterMmm 2013-03-20 14:06:37

回答

0
public class WriteOrderNumberFile { 

     private static void copyFile(String sourceFileName, String 
    destinationFileName, String orderNr) { 

      // orderNr = "LEC##0000000073"; 
      BufferedReader br = null; 
      PrintWriter pw = null; 

      try { 
      br = new BufferedReader(new FileReader(sourceFileName)); 
      pw = new PrintWriter(new FileWriter(destinationFileName)); 
      String token; 
      String line; 
      Scanner inFile; 
      while ((line = br.readLine()) != null) { 
       inFile = new Scanner(line); 

       while (inFile.hasNext()) { 
        token = inFile.next(); 
        if (token.equals(orderNr)) { 
         System.out.println(token); 
         pw.println(line); 
         while ((line = br.readLine()) != null) { 
         inFile = new Scanner(line); 
         while (inFile.hasNext()) { 
          token = inFile.next(); 
          if (token.equals("Run")) { 
           br.close(); 
           pw.close(); 
           return; 
          } 
         } 
         pw.println(line); 
         } 

        } 

       } 

      } 

      br.close(); 
      pw.close(); 
      } catch (Exception e) { 
      e.printStackTrace(); 
      } } 

     public static void main(String[] args) { 
      String sourceFileName = "D:\\Test Folder\\source.txt"; 
      String destinationFileName = "D:\\Test Folder\\destination.txt"; 
      String orderNr = "LEC##0000000064"; 
      copyFile(sourceFileName, destinationFileName, orderNr); } 

    } 
0

您只需使用一對夫婦的bash命令的位置:

$ egrep 'Item vat|Item Price' data.txt | cut -f2 -d: 
1000 
15% 
相關問題