Master java skills

File IO using FileReader and FileWriter

Reading from a file using FileReader class

Create a file class example.txt like below

package com.javatrainingschool;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class FileReadingExample {
	
	public static void main(String[] args) throws IOException {
		
		//create a file object from the actual file
		File file = new File("C:\\Users\\saras\\Desktop\\website\\example.txt");
		
		//file reader object
		FileReader reader = new FileReader(file);
		
		int i = 0;
		
		while((i=reader.read()) != -1) {
			System.out.print((char)i);
		}
		reader.close();
	}
}
Output :

This is example file
line number 1
line number 2
line number 3

Writing into file using FileWriter

package com.javatrainingschool;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class FileWritingExample {
	
	public static void main(String[] args) throws IOException {
		
		File file = new File("C:\\Users\\saras\\Desktop\\website\\example.txt");
		
		FileWriter writer = new FileWriter(file, true);
		
		writer.append("\nline number 4");
		
		System.out.println("Writing process successfully completed.");
		
		writer.close();
		
	}
}
Output :
Writing process successfully completed.