Write and Read Text txt file.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class WriteandReadFile {
static File file;
public static void main(String[] a) throws FileNotFoundException, IOException {
try {
file = new File("writetextfile.txt");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// Write text on txt file.
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.write("How to write and read text file in java\n");
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
// Read text from file.
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
}
Write and Read data Excel file.
Download jxl.jar import java.io.File;
import java.io.IOException;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.Number;
public class ReadandWriteExcelFile {
public static void main(String[] args) throws BiffException, IOException, WriteException {
File file = new File("output.xls");
// if file doesnt exists, then create it
if (!file.exists()) {
file.createNewFile();
}
// Write Excel file using jxl api
WritableWorkbook wworkbook;
wworkbook = Workbook.createWorkbook(file);
WritableSheet wsheet = wworkbook.createSheet("First Sheet", 0);
for (int rows = 0; rows < 10; rows++) {
wsheet.addCell(new Label(0, rows, "First Coloum " + rows));
wsheet.addCell(new Number(1, rows, 3.1459 + rows));
wsheet.addCell(new Label(2, rows, "third Coloum " + rows));
wsheet.addCell(new Number(3, rows, 7895 + rows));
}
wworkbook.write();
wworkbook.close();
// Read Excel file using jxl api
Workbook workbook = Workbook.getWorkbook(file);
Sheet sh = workbook.getSheet(0);
for (int rows = 0; rows < sh.getRows(); rows++) {
for (int colm = 0; colm < sh.getColumns(); colm++) {
System.out.print(sh.getCell(colm, rows).getContents() + " ");
}
System.out.println();
}
}
}
Write and Read data CSV file.
Download javacsv.jar import com.csvreader.CsvWriter;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class ReadandWriteCSVFile {
public static void main(String[] args) throws FileNotFoundException, IOException {
boolean alreadyExists = new File("Output.csv").exists();
try {
// use FileWriter constructor that specifies open for appending
CsvWriter csvOutput = new CsvWriter(new FileWriter("Output.csv",true),',');
// if the file didn't already exist then we need to write out the header line
if (!alreadyExists) {
csvOutput.write("Name");
csvOutput.write("Deparment");
csvOutput.write("Year");
csvOutput.endRecord();
}
// write out a few records
for (int i = 0; i < 10; i++) {
csvOutput.write("Studen " + i);
csvOutput.write("Deparment " + i);
csvOutput.write("201" + i);
csvOutput.endRecord();
}
csvOutput.close();
} catch (IOException e) {
e.printStackTrace();
}
// Read data from CSV file.
FileReader fr = new FileReader(new File("Output.csv"));
BufferedReader br = new BufferedReader(fr);
String st;
while ((st = br.readLine()) != null) {
System.out.println(st);
}
}
}