Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: CSV File Easy Read/Write

I'm working on a program that requires quick access to a CSV comma-delimited spreadsheet file. So far I've been able to read from it easily using a BufferedReader. However, now I want to be able to edit the data it reads, then export it BACK to the CSV.

The spreadsheet contains names, phone numbers, email addresses, etc. And the program lists everyone's data, and when you click on them it brings up a page with more detailed information, also pulled from the CSV. On that page you can edit the data, and I want to be able to click a "Save Changes" button, then export the data back to its appropriate line in the CSV--or delete the old one, and append the new.

I'm not very familiar with using a BufferedWriter, or whatever it is I should be using.

What I started to do is create a custom class called FileIO. It contains both a BufferedReader and a BufferedWriter. So far it has a method that returns bufferedReader.readLine(), called read(). Now I want a function called write(String line).

public static class FileIO {
    BufferedReader read;
    BufferedWriter write;

    public FileIO (String file) throws MalformedURLException, IOException {
        read = new BufferedReader(new InputStreamReader (getUrl(file).openStream()));
        write = new BufferedWriter (new FileWriter (file));
    }

    public static URL getUrl (String file) throws IOException {
        return //new URL (fileServer + file).openStream()));
                FileIO.class.getResource(file);
    }

    public String read () throws IOException {
        return read.readLine();
    }

    public void write (String line) {
        String [] data = line.split("\\|");
        String firstName = data[0];

        // int lineNum = findLineThatStartsWith(firstName);
        // write.writeLine(lineNum, line);
    }
};

I'm hoping somebody has an idea as to how I can do this?

like image 208
Rich Young Avatar asked Jan 09 '13 01:01

Rich Young


People also ask

How read and write CSV file in Java?

You can Download OpenCSV Jar and include in your project class path. CSVReader – This class provides the operations to read the CSV file as a list of String array. CSVWriter – This class allows us to write the data to a CSV file.

How create and write CSV file in Java?

Writing a CSV file is as simple as reading. Create an instance of CSVWriter by passing FileWriter object as parameter and start writing data to CSV file using methods of CSVWriter Class. After writing data we need to close CSVWriter connection by calling close() method of CSVWriter class.

How do I read a CSV file in Java by line?

We can read a CSV file line by line using the readLine() method of BufferedReader class. Split each line on comma character to get the words of the line into an array. Now we can easily print the contents of the array by iterating over it or by using an appropriate index.


1 Answers

Rather than reinventing the wheel you could have a look at OpenCSV which supports reading and writing of CSV files. Here are examples of reading & writing

like image 135
Reimeus Avatar answered Oct 05 '22 06:10

Reimeus