Java File IO & BufferedReader

  1. Read a Character read() from User
  2. Read a String readLine() from User
  3. Create a Directory in Java
  4. Create a File in Java
  5. Write into a file in Java
  6. Read from a file in Java
  7. Update (Read + Write) a file in Java

Note
Here I am using,
OS : Linux (Ubuntu 12.04)
IDE : Eclipse Tool
Eclipse : Juno (Version 4.2.0)
Package : Default package

A. Read a Character read() from User


Fileio_Ex1.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Fileio_Ex1 {
	
	void readIO() {
		
		char c = 0;
		BufferedReader reader = new BufferedReader
				(new InputStreamReader(System.in));
		System.out.println("Enter your Characters Here : 'q' to Quit..\n");
		
		do {
			
			try {
				c = (char) reader.read();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println(c);
		} while(c != 'q');
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Fileio_Ex1 obj = new Fileio_Ex1();
		obj.readIO();
	}	
}

Sample Output

Enter your Characters Here : 'q' to Quit..

Java q
J
a
v
a
 
q




B. Read a String readLine() from User


Fileio_Ex2.java

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Fileio_Ex2 {
	
	void readIO() {
		
		String str = null;
		BufferedReader reader = new BufferedReader
				(new InputStreamReader(System.in));
		System.out.println("Enter your Sentence Here : 'quit' to Quit..\n");
		
		do {
			
			try {
				str = (String) reader.readLine();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			System.out.println(str);
		} while(!str.equalsIgnoreCase("quit"));
	}
}
 
class MainClass {
	
	public static void main(String args[]) {
		
		Fileio_Ex2 obj = new Fileio_Ex2();
		obj.readIO();
	}	
}

Sample Output

Enter your Sentence Here : 'quit' to Quit..

Java
Java
Android
Android
SampleCodez
SampleCodez
quit
quit





C. Create a Directory in Java


CreateDirectory.java

import java.io.File;
 
class CreateDirectory 
{
	public static void main(String args[]) {
	
	   try {
    	  
		   String one ="/home/balaji/samplecodez";   	    // one directory
		
		   String many="/home/balaji/dir1/dir2/dir3";	    // multiple directories
		   
		   // Create one directory
		   
		   File file = new File(one);
		   
		   if(!file.exists()) {
			   if(file.mkdir()) {
				   System.out.println("Directory : " + one + " created");
			   }
		   }
 
		   // Create multiple directories
		   
		   File files = new File(many);
		   
		   if(!files.exists()) {
			   if(files.mkdirs()) {
				   System.out.println("Directories : " + many + " created");
			   }
		   }		  
	   } 
	   catch (Exception e) {
		   System.err.println("Error : " + e.getMessage());
	   }
	}
}

Sample Output

Directory : /home/balaji/samplecodez created
Directories : /home/balaji/dir1/dir2/dir3 created




D. Create a File in Java


CreateFile.java

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
 
public class CreateFile {
	
	public static void main(String[] args) {
		
		String fileName = "/home/balaji/samplecodez/success.txt";
		File file = new File(fileName);
 
		// If the file doesn't exist
		if (!file.isFile()) {
			// Check the parent directory...
			file = file.getAbsoluteFile();
			File parentDir = new File(file.getParent());
			if (!parentDir.exists()) {   // ... and create it if necessary
				parentDir.mkdirs();
				System.out.println(parentDir + " parent dir is created..");
			} 
		} 
		
		// Place to store the stream reference
		FileOutputStream outputFile = null; 
		try {
 
			// Create the stream opened to append data
			outputFile = new FileOutputStream(file, true);
			System.out.println(file + " file is created..");
			
		} catch (FileNotFoundException e) {
			e.printStackTrace(System.err);
		} 
		System.exit(0);
	} 
}

Sample Output

/home/balaji/samplecodez/success.txt file is created..






E. Write into a file in Java


Write.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Write {
		
	public static void main(String[] args) {
						
		try {
						
			String content = null;
			String OldContent = "";
 
			File file = new File("/home/balaji/samplecodez/success.txt");
 
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			
			BufferedReader reader = new BufferedReader
					(new InputStreamReader(System.in));
			System.out.println("'end' to quit and save your content..\n");
			
			do {
				
				try {
					content = (String) reader.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				OldContent = OldContent.concat(content).concat("\n");
				
			} while(!content.equals("end"));
			
			bw.write(OldContent);
			bw.close();
 
			System.out.println("\nOur content is successfully inserted into\n");
			System.out.println(file);
 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Sample Output

'end' to quit and save your content..

C C++ Java PHP Android
samplecodez.com
Balaji G
end

Our content is successfully inserted into
/home/balaji/samplecodez/success.txt




F. Read from a file in Java


Read.java

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
 
public class Read {
 
	public static void main(String[] args) {
 
		BufferedReader reader = null;
 
		try {
 
			String CurrentLine;
			
			File file = new File("/home/balaji/samplecodez/success.txt");
			FileReader fileReader = new FileReader(file);
 
			reader = new BufferedReader(fileReader);
 
			while ((CurrentLine = reader.readLine()) != null) {
				System.out.println(CurrentLine);
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)reader.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}

Sample Output

C C++ Java PHP Android
samplecodez.com
Balaji G
end




G. Update (Read + Write) a file in Java


Update.java

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Update {
 
	public static void main(String[] args) {
 
		BufferedReader reader = null;
		String OldContent = "";
		
		File file = new File("/home/balaji/samplecodez/success.txt");
 
		try {
 
			String Content;
			
			FileReader fileReader = new FileReader(file);
			reader = new BufferedReader(fileReader);
 
			while ((Content = reader.readLine()) != null) {
				
				OldContent = OldContent.concat(Content).concat("\n");
			}
 
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (reader != null)reader.close();
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
		
		System.out.println("Your Old Content : \n"+OldContent);
		System.out.println("Do you need to Udate your Content ? Y/N \n");
		reader = new BufferedReader(new InputStreamReader(System.in));
		char c = 'y';
		
		try {
			c = (char) reader.read();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		switch(c) {
		
			case 'y' :  
				write(file);
				break;
			case 'n' :
				break;
			case 'Y' :
				write(file);
				break;
			case 'N' :
				break;
			default :
				System.out.println("Press 'Y' or 'N' to Continue..");
		}		
	}
 
	private static void write(File file) {
		// TODO Auto-generated method stub
		
		try {
			
			String content = null;
			String NewContent = "";
 
			if (!file.exists()) {
				file.createNewFile();
			}
 
			FileWriter fw = new FileWriter(file.getAbsoluteFile());
			BufferedWriter bw = new BufferedWriter(fw);
			
			BufferedReader reader = new BufferedReader
				(new InputStreamReader(System.in));
			System.out.println("'end' to quit and save your content..\n");
			
			do {
				
				try {
					content = (String) reader.readLine();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				NewContent = NewContent.concat(content).concat("\n");
				
			} while(!content.equals("end"));
			
			bw.write(NewContent);
			bw.close();
			System.out.println("\nOur content is successfully updated into\n");
			System.out.println(file);
 
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

Sample Output

Your Old Content : 
C C++ Java PHP Android
samplecodez.com
Balaji G
end

Do you need to Udate your Content ? Y/N 

y
'end' to quit and save your content..

Java Android
samplecodez.com
Balaji G
Others will come soon...
end

Our content is successfully updated into
/home/balaji/samplecodez/success.txt




SHARE THIS PAGE



product 2

product 3

Feedbacks : balaji.scz@gmail.com