2015-04-23 26 views
-1

我有这个CSV文件。用Java计算CSV文件中的字符数

City,Job,Salary 
Delhi,Doctors,500 
Delhi,Lawyers,400 
Delhi,Plumbers,100 
London,Doctors,800 
London,Lawyers,700 
London,Plumbers,300 
Tokyo,Doctors,900 
Tokyo,Lawyers,800 
Tokyo,Plumbers,400 
Lawyers,Doctors,300 
Lawyers,Lawyers,400 
Lawyers,Plumbers,500 
Hong Kong,Doctors,1800 
Hong Kong,Lawyers,1100 
Hong Kong,Plumbers,1000 
Moscow,Doctors,300 
Moscow,Lawyers,200 
Moscow,Plumbers,100 
Berlin,Doctors,800 
Berlin,Plumbers,900 
Paris,Doctors,900 
Paris,Lawyers,800 
Paris,Plumbers,500 
Paris,Dog catchers,400 

我有代码,它在CSV文件中执行多个操作。现在我想对字符进行计数,并且还想在底部的CSV文件附加一行。

import java.io.*; 

import java.util.Arrays; 

public class Main { 

    public static void main(String args[]) throws IOException 
    { 

    String csv="C:\\Users\\Dipayan\\Desktop\\salaries.csv"; 
    BufferedReader br= new BufferedReader(new FileReader(csv)); 
    String line=""; 
    int count=0; 

    String str[]=new String[200]; 

    int[] a=new int[24]; 

    try { 
     br.readLine(); 

     while((line = br.readLine())!=null) 

     { 
      String[] f=line.split(","); 
      a[count]=Integer.parseInt(f[2]); 
      str[count]=f[1];    
      count++; 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

Arrays.sort(a); 

    for(int i:a) 
    { 
     System.out.println(i); 
    } 
     System.out.println("min="+a[0]); 

     System.out.println("Max="+a[count-1]); 



     // String sorting 
     String min1=str[0]; 

     for(int i=0;i<count;i++) 
     { 
      if(str[i].compareTo(min1)<0) 
       min1=str[i]; 
     } 
    System.out.println(min1); 

    /*String Sort end*/ 

    /*Finding the Median*/ 


    int median; 

    if(a.length%2==0) 
     median = a[a.length/2]+a[(a.length/2)+1]; 
    else 
     median=a[a.length/2]; 

    System.out.println("Median"+median); 

    /*Median*/ 

    System.out.println("Line"+count); 




} 

} 

回答

0

好吧,你快到了。您已经阅读每一行

while((line = br.readLine())!=null) { 
    //bla bla 
} 

你必须声明之前的int characterSum = 0;和每行的长度添加到它。像

int characterSum = 0; 

while((line = br.readLine())!=null) { 
    characterSum += line.length(); 
    //bla bla 
} 

至于追加一行到文件的东西... ...还有已经在这个question

0

您可以尝试使用CSVWriter java包将一行添加到您的CSV文件中。

以下是显示如何在CSV中追加一行的示例的链接。 http://www.csvreader.com/java_csv_samples.php

+0

请建议CSVWriter软件包以外的任何其他软件。我必须为此添加一个单独的jar。我必须使用常见的内置罐来解决它。 –

+0

我不确定你在计算字符数方面寻找什么。在整个文件中,在其中一列中,没有逗号,是什么?你的问题的第二部分,你想要后处理的数据,还是只添加一行到预处理的数据?如果你想要后期操作,你必须打开文件读/写,读取后将文件位置置回零,并写入所有数据(我假设你将它保存在某处*提示*),然后给你写新的记录。如果你想预先操作,你打开文件追加。 – Mike

0

计数字符修改while循环是这样的:

int numChars = 0; 
while((line = br.readLine())!=null) { 
    numChars += line.length(); 
    // leave your other code here 
} 

要追加java.util.FileWriter的线上使用this constructor

try (FileWriter fw = new FileWriter("file.name", true); 
    BufferedWriter bw = new BufferedWriter(fw); 
    PrintWriter pw = new PrintWriter(bw)) { 
    pw.println("the,new,line,to,append"); 
} catch (IOException e) { 
    // handle 
} 

注意, “试图用资源” 的声明这是Java 7 syntax

1

java.io.File.length()让你得到length of the file denoted by the abstract pathname。一个更好的方法,因为@蒂洛的评论中指出我的错误假设是首先将文件读入字符串,并使用它的getChars().length method

关于你的第二个问题,追加行到文件的末尾必要追加模式打开文件,写你的字符串到它,如下面的例子:

FileWriter f = new FileWriter(csvFileObject, true); 
f.write(ourString); 
f.close(); 

希望有所帮助。而且,没有必要的外部罐子。

+0

在许多情况下,文件长度将与文件中的字符数不同。 – Tilo

+0

现在呢,@Tilo呢? – hd1

+0

@ hd1我试过你的第二个解决方案。但它没有附加在新的路线上。 –