Skip to main content

Reading and Writing text File in Java

var regex = /^([a-zA-Z0-9])+([\w_\.\-]([a-zA-Z0-9])*[a-zA-Z0-9])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

Simple code to demonstrate to Read and Write text file in Java.




package email;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class ReadAndWriteFile {

public static void main(String[] args) {
  try {
    
   String content = "This is the content to write into file";
     
   File writeFile = new File("D:/Sample.txt");

   // if file doesnt exists, then create it
   if (!writeFile.exists()) {
    writeFile.createNewFile();
   }
/**
  * When we pass true as parameter to FilwWriter
  * then it appends the content in file instead of overwriting the content
  */
   //FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
   FileWriter fw = new FileWriter(writeFile.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);
   bw.write(content);
   bw.close();   
   fw.close();
  
   /**
    * File reading logic
    */
   File readFile = new File("D:/Sample.txt");
   BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));
  
   String line = "";
  
   while((line = bufferedReader.readLine()) != null)
   {
  System.out.println("Read from file===>"+line+"\n");
   }
   bufferedReader.close();
  } catch (IOException e) {
   e.printStackTrace();
  }
}
}

Comments

Popular posts from this blog

Uploading data on Remote Server in Java

This blog demonstrate how to upload data from file to remote server/database. Follow the steps as shown. 1. Create new Dynamic Web Project. 2. Now create all the packages as show in the image below. 3. Create controller with name ImportContact.java 4. Create pojo with name Contact.java 5. Create database layer code with name ImportContactBroker.java 6. Create connectivity with name DBConnection.java 7. Create db connectivity setting file as connectionSetting.java 8. Create jsp page with importcontact.jsp 9. Create supporting files like Constant.java and CsvReader.java 10. You can download the war file and import in your workspace. 11. MultipartRequest in Servlet and inside jsp helps to upload file on the remote server. 12. Download sample csv file from here . 13. Create Table with name Contact in Database as given in configuration file. 14.Just compile and run the code. ImportContact.java package com.uploadfile.controller; import java.io.BufferedReader; impo...

Creating Pdf in multiple Languages in Java

Creating Pdf in multiple Languages in Java This code demonstrate how to create pdf in multiple languages in java by using ITEXT pdf creator. To use this sample code download itext.jar To display different locale on pdf you need to download font known as Arialuni.ttf and added to the project Most important thing is the encoding part set encoding to "Identity-H" (String encoding = "Identity-H";) as well as create font for the same to take effect in pdf create font (Font fontNormal = FontFactory.getFont(("c:/windows/fonts/arialuni.ttf"), encoding,BaseFont.EMBEDDED, 8, Font.NORMAL);) You can download the source code from here import java.io.FileOutputStream; import com.lowagie.text.Chunk; import com.lowagie.text.Document; import com.lowagie.text.Font; import com.lowagie.text.FontFactory; import com.lowagie.text.HeaderFooter; import com.lowagie.text.Paragraph; import com.lowagie.text.Phrase; import com.lowagie.text.pdf....

Download Report in Excel in JSP

Report in Excel Format in JSP FLIGHT CODE FROM STA ETA Notes T3 4264 ISLE OF MAN 11:40 11:42 LANDED AT 11:43 BA 4081 PARIS-CDG 11:45 11:57 LANDED AT 11:58 BE 843 BELFAST CITY 11:45 11:40 LANDED AT 11:41 Call the above jsp by using any of the click functionality from another .jsp