2013-03-20 65 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% 
相关问题