Java PrintWriter写入将数据设置为.txt

时间:2014-07-11 16:00:06

标签: java iterator set printwriter

问题:如何使用Java PrintWriter

增加集合并写入.TXT

摘要:扫描指定文本文件并处理文本。然后将结果导出到报告.txt。

这方面的所有方面似乎都有效,因为我们应该尝试使用PrintWriter。 然后事情就变得疯狂了。如果每个东西都保持在代码中,并且我打印到终端就可以工作。如果我使用PrintWriter,它将创建文件,然后只打印tType" text2"的第二次迭代。我在这里发布了一些不同的例子,但它们似乎都没有完全打印,根本没有打印或错误。

文本文件输入示例:

123456  text1   175.00  001
234567  text2   195.00  001
345678  text1   175.00  007
456789  text3   160.00  005
987654  text4   90.00   006
876543  text3   160.00  007
765432  text2   195.00  011

需要的输出示例:

text1
text2
text3
text4

目前正在使用PrintWriter

获取.txt
text2

代码:

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;


public class test {

  public static void main(String[] arg){  

     Set<String> tSet= new TreeSet<>();    
    Map<String, Double> idList = new TreeMap<String, Double >();

        //Get input File.
        Scanner console = new Scanner(System.in);
        System.out.println("Enter the full path to your file: ");

        //Error handling in try catch for file not found
        try {
            String inputFileName = console.next();

            //Scanner creation and varaibles
            File inputFile= new File(inputFileName);
            Scanner input = new Scanner(inputFile);




            //Input contents  number , type, price ,ID
            //Loop


            while (input.hasNext()) 
            {
                String Num = input.next(); 
                String tType = input.next();
                tType=tType.toUpperCase();
                tSet.add(tType);
                Double tPrice=Double.parseDouble(input.next());
                String tAgent = input.next();
        //add if record does not exist, else append to existing record
                if (idList.containsKey(tAgent)) {
                    idList.put(tAgent, idList.get(tAgent) + tPrice);
                } 
                else 
                {
                    idList.put(tAgent, tPrice);
                }

            }

            input.close();

        //catch for incorrect file name or no file found
        } catch (FileNotFoundException exception) {
            System.out.println("File not found!");
            System.exit(1);
        }



  //Create Set iterator
  Iterator iterator;
  iterator = tSet.iterator();

  while(iterator.hasNext()){

      try {
      PrintWriter report= new PrintWriter("txtx.txt");
      report.println(iterator.next()+ " ");
     // System.out.println(iterator.next()+ " ");
      report.flush();
// report.close();
      }



    catch (IOException e) {
        System.out.println("Error could not write to location");
    }   


  }


}
}

1 个答案:

答案 0 :(得分:3)

输出循环的每次迭代都会重新创建(覆盖)文件。来自Oracle documentation

  

fileName - 用作此编写者目标的文件的名称。 如果该文件存在,那么它将被截断为零大小;否则,将创建一个新文件。

不是在循环中创建PrintWriter,而是在循环之前创建它。

//Create Set iterator
Iterator iterator;
iterator = tSet.iterator();

try
{
    PrintWriter report= new PrintWriter("txtx.txt");

    while(iterator.hasNext()){
        report.println(iterator.next()+ " ");
        report.flush();
    }

    report.close();
}
catch (IOException e) {
    System.out.println("Error could not write to location");
}