-1

I have this CSV File.

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

I have the code, which does multiple operations in the CSV File. Now I want to count the characters and also want to append a line to CSV File at the bottom.

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);




}

}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
dipayan
  • 128
  • 5
  • 18

4 Answers4

1

java.io.File.length() gets you the length of the file denoted by the abstract pathname. A better approach, as my erroneous assumption was pointed out in @Tilo's comment would be to first read the file into a String, and use it's getChars().length method.

As to your second question, appending a line to the end of a file necessitates opening the file in append mode and writing your string to it, as in the following example:

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

Hope that helps... And, there are no external jars necessary.

hd1
  • 33,938
  • 5
  • 80
  • 91
0

You can try CSVWriter java package to add a line to your CSV file.

Here is the link that shows an example of how to append a line in CSV. http://www.csvreader.com/java_csv_samples.php

  • Please suggest anything other than CSVWriter package. I have to add a separate jar for that. I have to solve it using common in-build jar. – dipayan Apr 23 '15 at 18:04
  • I'm not sure what your looking for in terms of count the number of characters. In the whole file, in one of the columns, without commas, what? Part two of your question, do you want the post-manipulated data, or just add a line to the pre-manipulated data? If you want post-manipulated, you either have to open the file read/write, set the file position back to zero after you've read it, and write all the data (I assume you saved it somewhere *hint*), then write you new record. If you want pre-manipulated, you open the file for append. – Mike Apr 23 '15 at 19:36
0

To count the characters modify your while loop like this:

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

To append a line use this constructor of the java.util.FileWriter:

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
}

Note that the "try with resources" statement this is Java 7 syntax.

Tilo
  • 3,255
  • 26
  • 31
0

Well, you're almost there. You're already reading each line

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

You'll have to declare an int characterSum = 0; before that and add the length of each line to it. Something like

int characterSum = 0;

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

As for appending a line to your file... there's already a question on this.

Community
  • 1
  • 1
async
  • 1,537
  • 11
  • 28