CodesJava

Easy learning with example program codes

FileReader and FileWriter in java


FileReader:

FileReader class is used for reading streams of characters from a file.

Commonly used constructors of FileReader:

1. FileReader(File file)

Creates a new FileReader, given the File to read from.

2. FileReader(String fileName)

Creates a new FileReader, given the name of the file to read from.

Example:

FileReaderExample.java

import java.io.FileReader;
 
/**
 * This program is used to read a file using FileReader.
 * @author codesjava
 */
class IOTest{
	public void readFile(){
		try {
			//creating FileReader object.
			FileReader fr = 
                         new FileReader("F:\\New folder\\data1.txt");
 
			int i;
			//read file.
			while((i=fr.read())!=-1){
				System.out.print((char)i);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class FileReaderExample {
	public static void main(String args[]){
		//creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call.
		obj.readFile();
	}
}

Output:

Hello.
This is a text file.

Download this example.

FileWriter:

FileWriter  class is used for streams of characters to a file.

Commonly used constructors of FileWriter:

1. FileWriter(File file)

Creates a FileWriter object given a File object.

2. FileWriter(String fileName)

Creates a FileWriter object given a file name.

Example:

FileWriterExample.java

import java.io.FileWriter;
 
/**
 * This program is used to write data into 
 * a file using FileOutputStream.
 * @author codesjava
 */
class IOTest{
	String str = "Hello www.codesjava.com";
 
	public void writeFile(){
		try {
		  //Creating FileWriter object.
		  //It will create a new file before writing if not exist.
		  FileWriter fw = 
                       new FileWriter("F:\\New folder\\data6.txt");
		   fw.write(str); 
		   fw.flush();
		   //Close file after write operation.
		   fw.close();  
 
		   System.out.println("Contents written successfully.");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
 
public class FileWriterExample {
	public static void main(String args[]){
		//Creating IOTest object.
		IOTest obj = new IOTest();
 
		//method call
		obj.writeFile();
	}
}

Output:

Contents written successfully.

Download this example.
 
Next Topic: How To Check If A File Exists In Java.
Previous Topic: BufferedInputStream and BufferedOutputStream in java with example.

Related Topics:

Input output (I/O) in java.
FileInputStream and FileOutputStream in java with example.
Byte Streams in java with example.
DataInputStream and DataOutputStream in java with example.
BufferedInputStream and BufferedOutputStream in java with example.
FileReader and FileWriter in java with example.
How To Check If A File Exists In Java.
Serialization in java with example.
Transient in java with example.

 

Core Java Tutorial

Programming language overview.
Overview of Java.
Java features
JVM architecture details.
JVM, JRE and JDK.
Java Coding Guidelines.
Some important definitions.
Variable and data types.
Hello world java program.
Core java examples programs.
Important Java Programs.
How to set permanent path in java?
OOPs Basics.
Object and Class in Java.
OOPs Principles/Concepts.
Abstraction in java.
Encapsulation in java.
Polymorphism in java.
Method overloading in java.
Method overriding in java.
Dynamic method dispatch.
Runtime polymorphism.
Association in java.
Inheritance in java.
Aggregation in java.
Command line arguments in java.
Command line argument program in eclipse.
Read input from command line using Scanner.
Java array programs
Java star pattern programs
Java number pattern programs
final in java.
Abstract class in java.
Interface in java.
Custom marker Interface in java.
Constructor in java.
Package in java.
Access modifier in java.
Static import in java.
Package class in java.
this in java.
Instance initialize block.
Anonymous block.
super in java.
Static in java.
final in java.
Java cloning deep and shallow
Shallow vs Clone copy
String handling in java.
String handling programs.
StringBuffer in java.
StringBuilder in java.
Exception Handling Tutorial.
Multithreading Tutoial.
Java input output stream tutorial.
Collections framework in java tutorial.
Collections framework programs.
Java Random class
Java annotations.
Java design principles.
Java 7 features
Java 8 features
Java networking tutorial
Java Reflection tutorial


Copyright © 2018 CodesJava DMCA.com Protection Status SiteMap