Skip to content

Instantly share code, notes, and snippets.

@zuch zuch/csvgen.java
Created Aug 15, 2012

Embed
What would you like to do?
java - csv file generator
//reads in file with strings seperated by whitespaces
//outputs a csv file with results
import java.io.*;
class csvgen
{
private static String file_name = "test.csv";
public static void main(String args[])
{
try
{
FileInputStream ifstream = new FileInputStream("test.txt");
FileWriter ofstream = new FileWriter(file_name);
BufferedWriter out = new BufferedWriter(ofstream);
DataInputStream in = new DataInputStream(ifstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null)
{
// Print the content on the console
String line = "";
System.out.println(strLine);
String[] words = strLine.split(" ");
for(int i =0; i < words.length; i++)
{
if(i==0)
{
line = words[i] ;
}
else
{
line += ", " + words[i];
}
}
line += "\n";
out.write(line);
}
System.out.println("File created successfully.");
out.close();
in.close();
}
catch (Exception e)
{//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
}
r r
d d
f f
g g
r r
d d
f f
g g
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.