We know how to read CSV file without using any API or external.JAR. Now we try to create CSV file with out using any API or external.JAR. Here we create CSV file using pure JAVA classes, for this we need to follow this steps.
Step 1:
Create OutputStream object for file creation.
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath)
Step 2:
We need to OutputStream to Writer object to get support for non english characterset .
Writer outStreamWriter = new OutputStreamWriter(fileStream,StandardCharsets.UTF_8);
Step 3:
We create a BufferedWriter object to get a better performance.
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter))
Step 4 :
Now we write data using append("Data") method and then flush the buffer and close it.
buffWriter.append("Data1");
buffWriter.append(separator);
buffWriter.append("Data2");
buffWriter.newLine();
buffWriter.flush();
Example Code :
package csvfilehandling;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class CSVWritingWithoutAPI {
public static void main(String[] args) throws IOException{
writeCSV("Our File Path", ",");
}
public static void writeCSV(String filePath, String separator) throws IOException {
/**
* We use try with resource so we don't need to call close
* method or write finally block explicitly it will be called automatically.
*/
try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
buffWriter.append("Data1");
buffWriter.append(separator);
buffWriter.append("Data2");
buffWriter.newLine();
buffWriter.flush();
buffWriter.append("1");
buffWriter.append(separator);
buffWriter.append("2");
buffWriter.newLine();
buffWriter.flush();
buffWriter.append("11");
buffWriter.append(separator);
buffWriter.append("22");
buffWriter.flush();
}
}
}
Step 1:
Create OutputStream object for file creation.
OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath)
Step 2:
We need to OutputStream to Writer object to get support for non english characterset .
Writer outStreamWriter = new OutputStreamWriter(fileStream,StandardCharsets.UTF_8);
Step 3:
We create a BufferedWriter object to get a better performance.
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter))
Step 4 :
Now we write data using append("Data") method and then flush the buffer and close it.
buffWriter.append("Data1");
buffWriter.append(separator);
buffWriter.append("Data2");
buffWriter.newLine();
buffWriter.flush();
Example Code :
package csvfilehandling;
import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
public class CSVWritingWithoutAPI {
public static void main(String[] args) throws IOException{
writeCSV("Our File Path", ",");
}
public static void writeCSV(String filePath, String separator) throws IOException {
/**
* We use try with resource so we don't need to call close
* method or write finally block explicitly it will be called automatically.
*/
try (OutputStream fileStream = new BufferedOutputStream(new FileOutputStream(filePath));
Writer outStreamWriter = new OutputStreamWriter(fileStream, StandardCharsets.UTF_8);
BufferedWriter buffWriter = new BufferedWriter(outStreamWriter)) {
buffWriter.append("Data1");
buffWriter.append(separator);
buffWriter.append("Data2");
buffWriter.newLine();
buffWriter.flush();
buffWriter.append("1");
buffWriter.append(separator);
buffWriter.append("2");
buffWriter.newLine();
buffWriter.flush();
buffWriter.append("11");
buffWriter.append(separator);
buffWriter.append("22");
buffWriter.flush();
}
}
}