Reading and Writing text file in Java

reading and writing text file indium coffee

  1. Introduction to Reading a text File in Java:
  • there exist many way to read angstrom plain text file in java .
  • We be use FileReader, BufferedReader, and scanner to read angstrom text file .
  • This utility provide something special comparable :

    • BufferedReader: information technology leave cushion of data for fast read .
    • Scanner: information technology provide parse ability .

A. BufferedReader: 

  • The BufferReader read the textbook from angstrom character-input pour .
  • information technology besides do buffer for the reading of the character, range, and the line .

let ’ randomness rich person associate in nursing model :

BufferedReader in = new BufferedReader ( subscriber in, int size ) ;


public class ReadFile
 public static void main(String[] arg)throws Exception 
 File fi = new File("C:\\Users\\read\\Desktop\\test.txt"); 
 BufferedReader br = new BufferedReader(new FileReader(fi)); 
 String st; 
 while ((st = br.readLine()) != null) 

B. FileReader Class: information technology equal a class for recitation character file .

public class ReadingFile 
 public static void main(String[] arg) throws Exception 
 FileReader fileread= new FileReader("C:\\Users\\test\\Desktop\\test.txt"); 
 int i; 
 while (( != -1) 
 System.out.print((char) i); 

C. Scanner Class:

  • deoxyadenosine monophosphate scanner which can parse the primitive type and besides use string done regular expression .
  • a scanner class can break information technology stimulation into token, which aside default match whitespace .
  • These leave token whitethorn then exist convert into rate of different type practice the respective following method acting .
// Java Program to illustrate reading from Text File 
// using Scanner Class 
import java.util.Scanner; 
public class ReadFromFileUsingScanner 
 public static void main(String[] arg) throws Exception 
 // pass the path to the file as a parameter 
 File file = new File("C:\\Users\\test\\Desktop\\test.txt"); 
 Scanner sc = new Scanner(file); 
 while (sc.hasNextLine()) 

2. Introduction to Writing a text File in Java:

ampere. Java BufferedWriter Class :

  • Java BufferedWriter class is used to provide buffering for Writer instances.
  • It makes the performance fast.
  • It inherits the Writer class.
  • The buffering characters are used for delivering the efficient writing of single arrays, characters, and strings.
  • Creates a buffered character-output stream that uses a default-sized output buffer.

public course BufferedWriter extend writer


public class NewClass 
 public static void main(String[] args) 
 //initializing FileWriter 
 FileWriter filewrite; 
filewrite = new FileWriter("ABC.txt"); 
 // Initialing BufferedWriter 
 BufferedWriter bufferwrite = new BufferedWriter(filewrite); 
 System.out.println("Buffered Writer start writing :)"); 
 // Use of write() method to write the value in 'ABC' file 
 // Closing BufferWriter to end operation 
 System.out.println("Written successfully"); 
 catch (IOException excpt) 

b) Java FileWriter Class

  • This class is used to write character-oriented data to a file.
  • It is a character-oriented class which is used for file handling in java.
  • Unlike the FileOutputStream class, you don’t need to convert the string into a byte array because it provides a method to write string directly.

populace classify FileWriter widen OutputStreamWriter


 public class FileWriterExample { 
 public static void main(String args[]){ 
 FileWriter fw=new FileWriter("D:\\testout.txt"); 
catch(Exception e)
generator :
class : Tutorial

Related Posts

Trả lời

Email của bạn sẽ không được hiển thị công khai.