reading and writing text file indium coffee
- 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 ) ;
Read more : Preparing for a Hurricane or Tropical Storm
Program:
import java.io.*; 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) System.out.println(st); } }
B. FileReader Class: information technology equal a class for recitation character file .
import java.io.*; public class ReadingFile { public static void main(String[] arg) throws Exception { FileReader fileread= new FileReader("C:\\Users\\test\\Desktop\\test.txt"); int i; while ((i=fileread.read()) != -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.io.File; 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()) System.out.println(sc.nextLine()); } }
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
Read more : How Much Wet Food Should You Feed a Cat?
Program:
import java.io.*; public class NewClass { public static void main(String[] args) { //initializing FileWriter FileWriter filewrite; try { 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 bufferwrite.write(69); bufferwrite.write(49); // Closing BufferWriter to end operation bufferwrite.close(); System.out.println("Written successfully"); } catch (IOException excpt) { excpt.printStackTrace(); } } }
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
Program:
import java.io.FileWriter; public class FileWriterExample { public static void main(String args[]){ try{ FileWriter fw=new FileWriter("D:\\testout.txt"); fw.write("Welcome"); fw.close(); } catch(Exception e) { System.out.println(e); } System.out.println("Success..."); } }