The program needs to be written in Java Code: You are given a ?le containing the
ID: 639950 • Letter: T
Question
The program needs to be written in Java Code:
You are given a ?le containing the names and addresses of company employees from many years ago that your manager has asked you to import into a database. You can use a CSV ?le and your database application to load the ?le, but the ?le your manager gave you was exported from an old, non-standard accounting system. Here is its format:
Freddy|Kruger
1313|Mockingbird|Lane
Houston|Texas
Billy|Thornton
1010|Slingblade|Street
Houston|Texas
Write a program that reads in the ?le and exports it to a standard CSV format. For the records above, the output format would be
Freddy Kruger,1313 Mockingbird Lane, Houston, Texas
Billy Thornton,1010 Slingblade Street, Houston, Texas
Explanation / Answer
Here is the small program that you are looking for...reading and parse csv files
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class SplitterExample
{
public static void main(String[] args)
{
//Input file which needs to be parsed
String fileToParse = "SampleCSVFile.csv";
BufferedReader fileReader = null;
//Delimiter used in CSV file
final String DELIMITER = ",";
try
{
String line = "";
//Create the file reader
fileReader = new BufferedReader(new FileReader(fileToParse));
//Read the file line by line
while ((line = fileReader.readLine()) != null)
{
//Get all tokens available in line
String[] tokens = line.split(DELIMITER);
for(String token : tokens)
{
//Print all tokens
System.out.println(token);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
finally
{
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Output:
1
Lokesh
Gupta
howtodoinjava.com
enabled
2
Rakesh
Gupta
howtodoinjava.com
enabled
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.