Results 1 to 9 of 9
Thread: Writing to existing CSV file
- 03-03-2012, 03:24 PM #1
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Writing to existing CSV file
Hello !
I have many CSV files like this:
Dist H G
12.5 6.67 8.76
3.54 8.32 7.43
I want to make a simple application in Java for:
-choose a CSV file(with File Chooser Dialog)
-create a new column("Sum") and put the result of ("H" + "G") there
The result CSV:
Dist H G Sum
12.5 6.67 8.76 15.43
3.54 8.32 7.43 15.75
Please help me to write the code.
Thank you very much in advance
- 03-03-2012, 08:32 PM #2
Re: Writing to existing CSV file
Break the task up into a list of simple steps and do one step at a time
Start with a simple program that reads the file and parses the columns on each line into an array.
Then add the extra column to the array
Then write the contents of the array to a disk file.
- 03-09-2012, 10:32 PM #3
Re: Writing to existing CSV file
Yeah, Norm outlined a good process. Are you having trouble with a specific portion of your code?
- 03-10-2012, 12:23 PM #4
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: Writing to existing CSV file
Till now, my code is:
public static void main(String[] args) {
try {
Scanner myfile = new Scanner(new FileReader("test1.csv"));
while (myfile.hasNextLine()) {
String[] cols = myfile.nextLine().split(";", 2);
System.out.println("col1: " + cols[0] + ", col2:" + cols[1]);
}
myfile.close();
} catch (Exception e) {
e.printStackTrace();
}
}
How ?Then add the extra column to the array
Thank very much
- 03-10-2012, 12:36 PM #5
Re: Writing to existing CSV file
See the Arrays class for utility method to copy old array to new array.
- 03-11-2012, 10:43 AM #6
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: Writing to existing CSV file
How do I add new column to my array ?
With System.arraycopy ?!
- 03-11-2012, 12:54 PM #7
Re: Writing to existing CSV file
Did you try the Arrays class method? What happened?
- 03-17-2012, 10:27 AM #8
Member
- Join Date
- Feb 2012
- Posts
- 4
- Rep Power
- 0
Re: Writing to existing CSV file
Hello Sir !
I don't know how to add a new column.
So, please help me.
Thank you very much in advance
- 03-17-2012, 12:54 PM #9
Similar Threads
-
solaris machine /tmp folder, File.exists() cant see the existing file.
By aragorn1905 in forum Advanced JavaReplies: 0Last Post: 12-21-2011, 09:15 AM -
how to put existing file in ftp
By trkece in forum NetworkingReplies: 0Last Post: 02-11-2011, 05:42 AM -
How tor write into Existing File
By agajantorayev in forum New To JavaReplies: 5Last Post: 08-17-2010, 01:03 PM -
Im writing to a file and i want to skip lines while writing to a text file.
By Broden_McDonald in forum New To JavaReplies: 1Last Post: 02-27-2010, 01:29 AM -
Writing to excel file erasing existing formatting
By jmHoekst in forum New To JavaReplies: 1Last Post: 09-16-2008, 05:58 PM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks