Tuesday, 27 October 2020

Program to create text file, write some data and read the same using single character method without using string functions

 In this tutorial, you'll learn how to create TEXT file, write some data to file and read the data from text file.

Before going to start you should aware of the following concepts:

import java.io.*;
import java.util.Scanner;

public class TextFileCreateWriteReadData {
	public static void main(String args[]) {
		try {
			File f = new File("demo.txt");
			// Step 1 - Create a TEXT file
			System.out.println("TEXT file created successfully !!!\n");
			// Step 2 - Write to TEXT file
			Scanner scan = new Scanner(System.in);
			System.out.println("Enter some text to write into file ==>");
			String data = scan.nextLine();
			FileWriter fw = new FileWriter("demo.txt");   // Creates a FileWriter object
			// Step 3 - Read data from TEXT file
			FileReader fr = new FileReader("demo.txt");   // Creates a FileReader object
			char x[] = new char[30];
			fr.read(x);		// Reads the contents to array
			System.out.println("\n\nReading data from TEXT file....");
			for(char c : x) {
				System.out.print(c);	// Print character one by one
		catch(IOException ioe) {

C:\>javac TextFileCreateWriteReadData.java
C:\>java TextFileCreateWriteReadData 
TEXT file created successfully !!!

Enter some text to write into file ==>
Hello I am Rahul

Reading data from TEXT file....
Hello I am Rahul

No comments:

Post a comment