Skip to main content

java read file Character by Character


welcome java file handling tutorial .

in this tutorial we are going to learn how to read text file using  java here i have used FileReader to  read file Character by Character. use following steps to read text file using java  program
1. create one folder on d drive as f that is name of folder is f
2. create t1.txt file write some text on it
3. create java program use following code or follow steps from video save java program in same folder import java.io.* package properly in java program
4. compile program and run
5. to understand program read what is FileReader read program explanation given below.











save


RfileCh.java

// program by Prakash

import java.io.*;

class RfileCh
{
 public static void main(String s[])
 {
  try{
       FileReader r=new FileReader("t1.txt");
  // used to open a file
  int ch;
 
  while((ch=r.read())!=-1)
  // here -1 is end of file
  {
     System.out.print((char)ch);// type casting int to char
 
  }
 
  // thaks for watching
 
 
  }
  catch(Exception e){}

  }

}



File Reading and Writing in java

In this tutorial, we show you how to read character)   from files using classes available in the java.io package

Reader, InputStreamReader, FileReader and BufferedReader

Reader is the abstract class for reading character streams. It implements the following fundamental methods:
·         read(): reads a single character.
·         read(char[]): reads an array of characters.
·         skip(long): skips some characters.

·         close(): closes the stream.




Comments

Labels

Show more

Popular posts from this blog

jbutton Click Event example Advance Java

Jbutton Click Event example Advance Java  Change frame Color On Button click Advance Java  import javax.swing.*; import java.awt.*; import java.awt.event.*;      class FrameDemo extends JFrame implements ActionListener {       JButton btn; Container c;          FrameDemo() {               c=this.getContentPane();        JButton btn= new JButton("Click"); btn.setBounds(100,50,100,50);         c.add(btn);         c.setLayout(null);        btn.addActionListener(this); }                    public void actionPerformed(ActionEvent a) {                     c.setBackground(Color.RED); } }        // cre...

java project using CMD Source Code Download

java project using CMD Source Code Download import java.sql.*; import java.*; import java.io.*; class whileInfinite { public static void main(String s[]) {  try{ while(true) { System.out.println("Select Your Choice :"); System.out.println("________________________________________"); System.out.println("1. Insert record :"); System.out.println("2. disply all records :"); System.out.println("3. display Specific record :"); System.out.println("4. update record:"); System.out.println("5. delete Record:"); System.out.println("6. Exit:"); BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));           System.out.println("Enter your choice"); int choice = Integer.parseInt(reader.readLine()); System.out.println("yo entered"+ choice); switch(choice) { case 1:whileInfinite obj=new whileInfin...

Nested Classes sample program

Nested Classes In Java, just like methods, variables of a class too can have another class as its member. Writing a class within another is allowed in Java. The class written within is called the nested class , and the class that holds the inner class is called the outer class . class Outer {  private class Inner  {    void dispaly()    {        System.out.println("this is inner class");       }   }  void call()  {    Inner obj=new Inner();    obj.dispaly();  } } class InnerDemo { public static void main(String s[]) {  Outer ob=new Outer();  ob.call(); } } Example solve watch video for understanding  // inner class By Prakash Sonar class outer {  private class Inner  {    void display()   {      System.out.println...