How to read and write text, Excel and CSV file in java?.

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);  
     }  
   }  
 }  

14 comments:

  1. Hi,

    How one can read particular data either from row or column from CSV file and manipulate it?

    ReplyDelete
    Replies
    1. If read particular column data, need split by comma(,) separated then enter column positive to get data.
      Example
      String columndata[]=str.split("\\,");
      String desiredcolunmdata=columndata[column_position];

      Delete
  2. You can also use poi jar.You can create excel file using poi jar easily.There is fully explain example
    http://www.javaproficiency.com/2015/03/create-xls-file-in-java.html

    ReplyDelete
  3. can we have read from text file and write in excel file????

    ReplyDelete
  4. can we have read from text file and write in excel file????

    ReplyDelete
    Replies
    1. // Read text from file.
      FileReader 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
      }

      Delete
  5. Hi Hiro Mia,

    How can i write data in particular column in the CSV file

    ReplyDelete
    Replies
    1. CsvWriter csvOutput = new CsvWriter(new FileWriter("Output.csv",true),',');
      // 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();

      Delete
    2. Hi Hiro Mia,

      Thanks for replying,

      On my above question i want to ask, how to write data in particular cell in the csv.


      Delete
  6. to read N number rows from excel & search particular word in it.

    ReplyDelete
  7. Great 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:
    Selenium Training

    ReplyDelete


  8. This blog was very useful for me waiting for more blog.


    SAP FICO Training in Chennai

    ReplyDelete
  9. facing issue osgi.wiring.package=jxl

    ReplyDelete
  10. Thanks 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