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);
}
}
}
Hi,
ReplyDeleteHow one can read particular data either from row or column from CSV file and manipulate it?
If read particular column data, need split by comma(,) separated then enter column positive to get data.
DeleteExample
String columndata[]=str.split("\\,");
String desiredcolunmdata=columndata[column_position];
You can also use poi jar.You can create excel file using poi jar easily.There is fully explain example
ReplyDeletehttp://www.javaproficiency.com/2015/03/create-xls-file-in-java.html
can we have read from text file and write in excel file????
ReplyDeletecan we have read from text file and write in excel file????
ReplyDelete// Read text from file.
DeleteFileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String st;
while ((st = br.readLine()) != null) {
// seperate text value
// follow below url instraction to write excel
http://hiromia.blogspot.com/2016/01/how-to-write-and-append-data-to-excel.html
}
Hi Hiro Mia,
ReplyDeleteHow can i write data in particular column in the CSV file
CsvWriter csvOutput = new CsvWriter(new FileWriter("Output.csv",true),',');
Delete// write data in desired column in the CSV file and other column value should empty string
csvOutput.write("");
csvOutput.write("Deparment");
csvOutput.write("");
csvOutput.endRecord();
csvOutput.close();
Hi Hiro Mia,
DeleteThanks for replying,
On my above question i want to ask, how to write data in particular cell in the csv.
to read N number rows from excel & search particular word in it.
ReplyDeleteGreat information and the real live code was very much helpful for better understand. Thank you so much buddy. Keep rocking. If your audience is also interested in Selenium Testing, they can take a look here:
ReplyDeleteSelenium Training
ReplyDeleteThis blog was very useful for me waiting for more blog.
SAP FICO Training in Chennai
facing issue osgi.wiring.package=jxl
ReplyDeleteThanks for sharing nice article . It's very helpful . You can also use poi jar.You can create excel file using poi jar easily.There is fully explain example Create excel file
ReplyDelete