Output is Displayed in console and not in the File

时间:2015-06-25 10:02:27

标签: java

 try {
      BufferedReader sc = new BufferedReader(new FileReader("/home/aravind/Desktop/India.txt"));

       ArrayList<String> name = new ArrayList<>();
       ArrayList<String> Location = new ArrayList<>();
       ArrayList<String> Id = new ArrayList<>();
       ArrayList<String> Details = new ArrayList<>();
       String line = " ";
       while ((line = sc.readLine()) != null) {

            if (!line.trim().equals("")) {
                    System.out.println(line);
                    if (line.toLowerCase().contains("name")) {
                        name.add(line.split(":")[1].trim());
                    }
                    if (line.toLowerCase().contains("Location")) {
                        Location.add(line.split(":")[1].trim());
                    }
                    if (line.toLowerCase().contains("Id")) {
                        Id.add(line.split(":")[1].trim());
                    }
                    if (line.toLowerCase().contains("Details")) {
                        Details.add(line.split(":")[1].trim());
                    }
             }
        }

        for (int i = 0; i < name.size(); i++) {

            PrintWriter out = new PrintWriter(newFileWriter("output.csv"));

            out.println("name;Location;Id;Details;");

            out.println(name.get(i) + ";" 
               + Location.get(i) + ";" 
               + Id.get(i) + ";" 
               + Details.get(i) + ";");
            out.close();
        }
        sc.close();            
 } catch (Exception e) {

 }

and my input file looks like

name = abc

id = 123

Place = xyz

Details = some texts with two line

name = aaa

id = 54657

Place = dfd

Details = some texts with some lines

What could be the problem why it is not printing in csv file instead prints o/p in console..Kindly help me

1 个答案:

答案 0 :(得分:0)

在您的文件中,标题和值始终用&#34; =&#34;分隔,而在运行时,您可以按&#34;:&#34;来修剪字符串。你应该替换&#34;:&#34; by&#34; =&#34;,因此你的修剪结果在索引1处不会为空。:

name.add(line.split("=")[1].trim());