Java Programming

Character File

Java programming example for write and read file using character stream

10/5/2021
0 views
character-file.javaJava
/* Character stream */
import java.io.*;

class charfile {
	public static void main(String args[ ]) throws IOException {

		/* Declare and create input file */
		FileWriter fwrite = new FileWriter("charfile.txt");
		fwrite.write("This is a character file!");
		System.out.println("File Created!\n");
		fwrite.close();

		/* Read till end of the file */
		FileReader freader = new FileReader("charfile.txt");
		int rf;
		System.out.println("Reading File -");
		while((rf = freader.read()) != -1)
			System.out.print((char) rf);
		System.out.println();
		freader.close();
	}
}


/* Output */
File Created!

Reading File -
This is a character file!
Java TutorialCharacter Stream FileRead FileWrite File

Related Examples