Created
September 2, 2015 07:10
-
-
Save maderaka/2f80dd2997c3b0cf9ca4 to your computer and use it in GitHub Desktop.
Convert text file (.txt) into CSV file using Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
public class Convert { | |
public static void main(String[] args) { | |
System.out.println("Initialize ..."); | |
Read read = new Read("soal01.txt"); | |
Print print = new Print(); | |
for (int i = 1; i <= 3; i++) { | |
try{ | |
print.setFileName("jawaban"+i+".csv"); | |
read.setPrint(print); | |
read.exec(i); | |
} catch(IOException ex){ | |
ex.printStackTrace(); | |
} | |
} | |
System.out.println("Finished"); | |
} | |
} | |
class Print { | |
protected String fileName; | |
protected FileWriter writer; | |
public Print() {} | |
public Print(String fileName) throws IOException { | |
this.fileName = fileName; | |
this.writer = new FileWriter(this.fileName); | |
} | |
public String getFileName() { | |
return this.fileName; | |
} | |
public void setFileName(String fileName) throws IOException { | |
this.fileName = fileName; | |
this.writer = new FileWriter(this.fileName); | |
} | |
public void close() throws IOException { | |
this.writer.flush(); | |
this.writer.close(); | |
} | |
public void addRow(String[] c) throws IOException { | |
int l = c.length; | |
for(int i = 0; i < l; i++) { | |
this.writer.append(c[i]); | |
if(i != (l - 1)) { | |
this.writer.append(","); | |
} | |
} | |
this.writer.append('\n'); | |
} | |
} | |
class Read { | |
protected String fileName; | |
protected BufferedReader bufferedReader; | |
protected Print print; | |
public Read(String fileName) { | |
this.fileName = fileName; | |
} | |
public Read(Print print, String fileName) { | |
this.print = print; | |
this.fileName = fileName; | |
} | |
public void setPrint(Print print) { | |
this.print = print; | |
} | |
public Print getPrint() { | |
return this.print; | |
} | |
public void exec(Integer type) { | |
String sCurrentLine = ""; | |
try{ | |
this.bufferedReader = new BufferedReader( | |
new FileReader(this.fileName)); | |
while((sCurrentLine = this.bufferedReader.readLine()) != null) { | |
String[] columns = sCurrentLine.split("\t"); | |
int length = columns.length; | |
if(type == 1) { | |
this.print.addRow(columns); | |
} else if(length >= 2){ | |
double col1 = Double.parseDouble(columns[1]); | |
if(col1 > 20){ | |
if(type == 2){ | |
this.print.addRow(columns); | |
} else if(type == 3 && length >= 3 && columns[2] != null){ | |
this.print.addRow(columns); | |
} | |
} | |
} | |
} | |
this.print.close(); | |
} catch(IOException ex) { | |
ex.printStackTrace(); | |
} finally { | |
try{ | |
if(this.bufferedReader != null) { | |
this.bufferedReader.close(); | |
} | |
} catch(IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
alskdfjlksadjf 5 | |
asdfjklasfjlkasdf 8 lll | |
sajdfklsadjflksaf 15 asdf | |
sajdkflsadfjsa 4 kadsjf | |
fsjkdlfjslakdf 90 2341asdfjk | |
sadjfklasdjflsa 77 jaksdjf | |
dfsjakdlfjsaldkf 23 kasjdf | |
asdfjklasjdflsad 99 | |
fsadklfjsalkdf 29 kasljdf | |
asdfjklsadfjslak 9 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Initialize ...
Exception in thread "main" java.lang.NumberFormatException: For input string: "lll"
at java.base/jdk.internal.math.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2054)
at java.base/jdk.internal.math.FloatingDecimal.parseDouble(FloatingDecimal.java:110)
at java.base/java.lang.Double.parseDouble(Double.java:549)
at Read.exec(Convert.java:103)
at Convert.main(Convert.java:17)