-7

I want to read a CSV file in Java Swing and then modify it; for instance I want to add some extra columns, a column that contains the ip address of the computer on which I am working and some more columns. Can anyone help me?

I am working on a project which reads the CSV file, modifies it and then loads that CSV file into a MySQL database. I have tried almost all tutorials but couldn't find any solutions.

halfer
  • 19,824
  • 17
  • 99
  • 186
user3599030
  • 11
  • 1
  • 3
  • you have to read the csv file, create another csv file with modified data. delete the old file and rename the new file. – TheLostMind May 05 '14 at 09:16
  • *"I have tried almost all tutorials but couldnt find any solution."* I think the problem is you are looking for too specific a tutorial. What were the 5 closest hits? I would not imagine there would be any single tutorial mentioning both CSV and Swing. – Andrew Thompson May 05 '14 at 09:17
  • its not duplicate i also want to modify the csv file after reading it and then store it in the database. – user3599030 May 05 '14 at 10:23
  • @user3599030 So look up how to write to files and figure it out, instead of having us do it for you. – Nic Jan 05 '16 at 17:18

1 Answers1

3

Reading a CSV is independent from Swing: Swing handle user interfaces but not file input/output. For that, you can use the classes in the java.io package. In particular:

  • the FileReader class to read through your file

  • the BufferedReader class to read one line of your CSV file at a time

  • the StringTokenizer class to parse individual entries in a line (part of java.util)

Then, the easiest way to display this with swing is to store this information inside nested Vector objects (java.util):

  • one top level Vector where each element represents a line and is itself a Vector

  • each Vector representing a line contains the entries for that line

From there, you can create a JTable (javax.swing) to display this information using the constructor

public JTable(Vector rowData, Vector columnNames)

You can see it requires yet another Vector with the names for each column (which must be the same size as the number of entries in a line from your file).

halfer
  • 19,824
  • 17
  • 99
  • 186
schmop
  • 1,440
  • 1
  • 12
  • 21
  • Please use generics when they're available -- i.e. `Vector` instead of `Vector`. In this case, the JTable constructor doesn't take them, but since you can pass a `Vector` where a `Vector` is required, that's how you should be storing them. – Nic Jan 05 '16 at 17:20