用Java计算CSV文件中的字符数

时间:2015-04-23 17:57:33

标签: java csv append

我有这个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);




}

}

4 个答案:

答案 0 :(得分:1)

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

关于第二个问题,在文件末尾附加一行需要以附加模式打开文件并将字符串写入文件,如下例所示:

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

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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

要计算字符数,请修改你的while循环:

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

要使用java.util.FileWriter的<{3}}附加一行:

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
}

请注意,“尝试使用资源”语句为this constructor

答案 3 :(得分: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