我有这个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);
}
}
请建议CSVWriter软件包以外的任何其他软件。我必须为此添加一个单独的jar。我必须使用常见的内置罐来解决它。 –
我不确定你在计算字符数方面寻找什么。在整个文件中,在其中一列中,没有逗号,是什么?你的问题的第二部分,你想要后处理的数据,还是只添加一行到预处理的数据?如果你想要后期操作,你必须打开文件读/写,读取后将文件位置置回零,并写入所有数据(我假设你将它保存在某处*提示*),然后给你写新的记录。如果你想预先操作,你打开文件追加。 – Mike