Write Java program that converts a file format to another. It should support the following formats: -
Question:
Write Java program that converts a file format to another. It should support the following formats:
- .csv (comma separated values) file: stores tabular data in plain text. Each line of the file represents a table row containing one or more cells separated by commas. If a content of a cell has one or more commas (e.g. 12,345), then that cell's content needs to be enclosed in double-quotations (e.g. "12,345").
- .txt (tab separated values) file: stores tabular data in plain text. Each line of the file represents a table row containing one or more cells separated by tab characters (t). The content of each cell does not have any tab character.
Program Commands:
- convert source.xxx destination.yyy: this command converts source.xxx to destination.yyy where source.xxx is the name and extension of the file that user wants to convert and destination.yyy is the name and extension of the file in which the user wants to store the result of format conversion. Please note that xxx and yyy can either be csv or txt. Also, the file names may or may not include the path to the file in the file system.
- normalize source.xxx: this command reads the content of source.xxx, normalizes the content of each cell, and writes the normalized content back to the same file. Normalizing a cell is an operation that depends on the current content of the cell:
- if cell is empty: writes N/A instead
- if cell contains an integer: normalization explicitly shows the sign (+ for pos- itive and - for negative). Also, if the integer representation is shorter than 10 characters, it adds some leading zeros to make the representation 10 character long.
- if cell contains a float/double: normalization shows two digit after decimal point. Also, it uses scientific notation if the number is greater than 100 or less than 0.01.
- if cell contains a string longer than 13 characters, normalization shows the first 10 characters of the string followed by an ellipsis (three dots like this . . . )
- otherwise, normalization causes no change.
- quit: ends the program.
This is the code I have so far:
import java.util.*; import java.io.*; public class PA5 { private static final String INPUT = System.getProperty("user.dir")+"/input/"; private static final String OUTPUT = System.getProperty("user.dir")+"/output/"; public static void converter(String src, String trg) throws Exception { String srcExt = src.substring(src.length()-3); String trgExt = trg.substring(trg.length()-3); if(!srcExt.equals("txt") && !srcExt.equals("csv")) throw new Exception("bad format"); if(!trgExt.equals("txt") && !trgExt.equals("csv")) throw new Exception("bad format"); if(src.equals(trg)) throw new Exception("same i/o"); Scanner in = new Scanner(new File(src)); PrintWriter out = new PrintWriter(trg); if(srcExt.equals(trgExt)) {//same formats if(srcExt.equals("txt")) {//txt->txt while(in.hasNextLine()) { Scanner lineScanner = new Scanner(in.nextLine()); lineScanner.useDelimiter("t"); while(lineScanner.hasNext()) out.print(lineScanner.next() + (lineScanner.hasNext()?"t":in.hasNextLine()?" ":"")); out.flush(); } }else {//csv->csv } }else { if(srcExt.equals("txt")) {//txt->csv }else {//csv->txt } } out.close(); in.close(); } public static void normalize(String fileName) throws Exception{ ArrayListcontent = new ArrayList(); Scanner in = new Scanner(new File(fileName)); while(in.hasNextLine()) content.add(in.nextLine()); in.close(); PrintWriter out = new PrintWriter(fileName); for(String line: content) { Scanner lineScanner = new Scanner(line); //Your code... use out.printf... } out.close(); } public static void main(String[] args) { // TODO - Auto-generated method stub try { converter(INPUT+"abcd.txt", OUTPUT+"efgh.txt"); }catch(Exception e) {} }
}
Introduction To Java Programming And Data Structures Comprehensive Version
ISBN: 9780136520238
12th Edition
Authors: Y. Daniel Liang