I am still fairly new to Java and trying to learn. I am trying to create a program, in Java, that will be able to read a .CSV file that I have in my C:\ Drive and once it is read I want it to separate the data into a newer .CSV file when they are between the comments of the CSV.
For example we have the original CSV data file as follows:
*File A
*Name, Address, City, Country, Zip
"John","123 Main Street","NY","USA","12345"
"Jane","456 Main Street","NY","USA","12345"
"Smith","789 Main Street","NY","USA","12345"
*File B
*Name, Address, City, Country, Zip
"Jose","123 Main Street","NY","USA","12345"
"Brandon","456 Main Street","NY","USA","12345"
"Mike","789 Main Street","NY","USA","12345"
*File C
*Name, Address, City, Country, Zip
"Kathy","123 Main Street","NY","USA","12345"
"Jai","456 Main Street","NY","USA","12345"
"Michael","789 Main Street","NY","USA","12345"
So basically I am trying to have something that will be able to read the CSV file, look for the prefix of *File, in the CSV file, and then will be able to create new files which will have everything in between. For example:
CSV1:
*Name, Address, City, Country, Zip
"John","123 Main Street","NY","USA","12345"
"Jane","456 Main Street","NY","USA","12345"
"Smith","789 Main Street","NY","USA","12345"
CSV2:
*Name, Address, City, Country, Zip
"Jose","123 Main Street","NY","USA","12345"
"Brandon","456 Main Street","NY","USA","12345"
"Mike","789 Main Street","NY","USA","12345"
CSV3:
*Name, Address, City, Country, Zip
"Kathy","123 Main Street","NY","USA","12345"
"Jai","456 Main Street","NY","USA","12345"
"Michael","789 Main Street","NY","USA","12345"
I was able to find something and able to read the CSV file but just don’t know how to divide it yet. The following is the code for reading the file:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class csvReader
{
public static void main(String[] args) throws FileNotFoundException
{
//Get scanner instance
Scanner scan = new Scanner(new File("C:\\Personal_Info.csv"));
//Set the delimiter used in file
scanner.useDelimiter(",");
//Get all tokens and store them in some data structure
//I am just printing them on the screen
while (scan.hasNext())
{
System.out.print(scan.next() + ",");
}
System.out.println("\n*************Process Complete*************");
//Close the scanner
scan.close();
}//End Main
}//End Class
Solution:
You can grab line by line looking for particular keywords in the lines to determine what you need to do. For your current example you can use the keyword “*File” to know when to create a new file to write data to. Just remember you want to close the previous BufferedWriter if it’s currently writing to another file. Try this
public static void main(String[] args) throws FileNotFoundException, IOException
{
Scanner scan = new Scanner(new File("C:\\Personal_Info.csv"));
int fileNumber = 0;
String csv = "CSV";
BufferedWriter writer = null;
while (scan.hasNextLine())
{
String line = scan.nextLine();
if(line.contains("*File"))
{
fileNumber++;
//check and make sure we close our previous writer
if(writer != null)
writer.close();
System.out.println("Creating a new file called: " + csv + fileNumber);
writer = new BufferedWriter(new FileWriter("C:\\" + csv + fileNumber));
}
else if(writer != null)
{
writer.write(line + "\n");
}
}
System.out.println("\n*************Process Complete*************");
//Close the scanner
scan.close();
if(writer != null)
writer.close();
}