Saturday, 8 June 2013

Read File Character By Character

To read the file character by character ,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package filedemo;

import java.io.*;
/**
 *
 * @author Saeed
 */
public class FileDemo {
    public static void main(String args[]) throws IOException{
       char ch;
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        // To get The Data from the Console
       
        File f=new File("talha.txt"); // File to Create
        try (FileWriter fw = new FileWriter(f)) { // File Writer to Write Data inside File f
            do{
             ch=(char)br.read(); // to read data character by character
             fw.write(ch);//get data from console and write in FileWriter(File f)
             }while(ch!='q');
           
        }
          
           int i;
           FileInputStream fin=new FileInputStream("talha.txt"); // to Open Stream in Read Mode
           do{
           i=fin.read();  // get the bytes and assign to integer
           if (i!=-1) System.out.write((char)i); // if not end of file convert in character and print
           }while(i!=-1);
                     
    }
}

No comments:

Post a Comment