I'm using CsvReader/CsvWriter to read and write my data to csv files. And I have a question about appending lines of data to other certain lines.

Lets say I have some data already saved, now I open my other program which has more info that I would like to add to that same line.

This program has textfields that add to a table, then when I click save it takes the information from the table and saves it to a separate csv file. I'm planning on making it save that info in the first csv file so that I can read all the information at one time.

So in short, is there a way to make my program append (add on) my new data to the old csv file in a specific lines?

When I read the info from file1.csv, I split it into columns like this:

String[] values = viewObject.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

Perhaps by throwing something like the following in?

if (dataAtextField == dataA) {
   // make csv writer append to that line somehow
}

Any ideas or helpful hints?

Create a FileOutputStream in append mode, then create your csvwriter using that output stream

Can you elaborate a bit? I'm not quite sure I follow you.

I took a look into FileOutputStream, I took note on a couple of things:

  • FileOutputStream is meant for writing bytes of raw data such as images, FileWriter is preferred for streams of characters
  • Bytes will be written to the end of the file

I figure the first listed object isn't a big deal. And the second listed object is more than likely solved with the way you explained it. I don't quite follow, if you don't mind explaining it a little I would appreciate it.


Further Info About My Program (may or may not be useful) :

My original data is saved by an external (third party) program. My secondary data will be coming from a class I have created (basically taking information that I have on a table)

So currently file1.csv looks something like:

Col = Column

[Col 1] [Col 2] [Col 3]

dataA, dataB, dataC,
dataD, dataE, dataF,

and the data from my class (from the table) saves to a separate file2.csv file like so:

[Col 1] [Col 2] [Col 3] [Col 4] [Col 5] [Col 6] [Col 7] [Col 8]

data1, data 2, data 3, data4, data5, data6, data7, data8,
data9, data10 , data11 , data12, data13, data14, data 15, data16,

If [Col 1] (from file1.csv) is equal to [Col 1] in file2.csv I want the information added to that line in file1.csv

However I would like to completely eliminate file2.csv so perhaps getting the value in my first column on the jTable and comparing it to [Col 1]

Anyways those are just thoughts as I still have a lot of java to learn.

You create your csvwriter using a file name. That's OK, but there's no option to set "append" mode. There's an alternative constructor for csvwriter that takes an OutputStream as a parameter. FileOutputStream is a kind of OutputStream, which can be opened in append mode. So you can:
1. create a FileOutputStream using the file name and append mode.
2. create a csvwriter using the FileOutputStream from (1)
3. anything you write to the csvwriter will be appended to the file.

Alright that makes a bit more sense and cleared some things up, however before I put it all into action, will it be able to put that data at the end of certain lines? Or just the end of the file?

I assume using a simple if statement should allow me to do the comparison but not quite sure how it would add to that line exactly

End of the file only.
To append to each line you'll have to read in the old file and replace it by writing a complete new file

So then appending wouldn't be practical anymore since I need to add to the end of certain lines.

Instead something like this could work? (typed it out on a whim) :

BufferedReader reader = new BufferedReader(new FileReader ("C:\\myFile.csv"));

        try {
            boolean bHeadersDone = false;
            while (reader.ready()) {
                String beanInfo = reader.readLine();
                
                if (!bHeadersDone) {
                    if (beanInfo.contains("Ping")) {
                        bHeadersDone = true;
                    }
                } 
                else {
                    String[] values = beanInfo.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

                  // textField.getText() is data1 
                  // add whatever is in column 2 to my program GUI via textfield
                  // add whatever is in column 3 to my program GUI via textfield

		    if (dataA.equals(textField.getText())) {  		
			textField1.getText(dataB);			
			textField2.getText(dataC);			

javax.swing.table.DefaultTableModel tableModel = (javax.swing.table.DefaultTableModel)jTable1.getModel();

tableModel.addRow(new Object[]{textField1.getText(), textField2.getText(),textField3.getText(),textField4.getText(),textField5.getText(),textField6.getText(),textField7.getText(),textField8.getText()});

			com.csvreader.CsvWriter writer = new com.csvreader.CsvWriter("C:\\myFile.csv");

			writer.writeRecord(new String[]{"dataB", "dataC", "data2", "data3", "data4", "data5", "data6", "data7", "data7"});
			}
                  
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

Sorry for how bad the code got squished, however its due to the forum :/

Okay I'm going to bump this as I'm attempting to take a slightly different approach.

Lets say I read file one in which i also split the data into columns and then save it to an arraylist like so:

ArrayList<String> arrayList1 = new ArrayList<String>();

String[] values = viewObject1.split("," , 3);
                    dataA = values[0];
                    dataB = values[1];
                    dataC = values[2];

                    if (!dataC.contains("[n/a]")) {

                        arrayList1.add(dataA);
                        arrayList1.add(dataB);

and then I read my second file and do the same thing:

ArrayList<String> arrayList2 = new ArrayList<String>();

String[] values = viewObject2.split("," , 4);
                    itemA = info[0];
                    itemB = info[1];
                    itemC = info[2];
                    itemD = info[3];


                    if (!itemD.contains("[n/a]")) {

                        arrayList2.add(itemA);
                        arrayList2.add(itemB);
                        arrayList2.add(itemC);

And the next thing I want to do is see if dataA and itemA are the same thing, but thats where I am confused because as far as I know it would compare the lines of each arraylist instead of columns correct?

I can't write these to separate arraylists for each column as the info in each file has to line up.

The ultimate goal is that if dataA and itemA match up, I will write each line that goes with each file to a new file. Thus combining them and saving it.

Any ideas or help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.