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

Calculator Program using jFrame

Calculator Program using jFrame java swing calculator example java jframe calculator source code java code for simple calculator using gui how to make a calculator in advance java using JFrame simple calculator program in java using frame simple calculator program in java source code write a code to create simple calculator using swing component import javax.swing.*; import java.awt.event.*; public class TextFieldExample extends JFrame implements ActionListener{     JTextField tf1,tf2,tf3;     JButton b1,b2; //declaration of control     TextFieldExample(){         JFrame f= new JFrame("Button click demo"); //         tf1=new JTextField();         tf1.setBounds(50,50,150,20);         tf2=new JTextField();         tf2.setBounds(50,100,150,20);         tf3=new JTextField();       ...

Java Program to delete record using JFrame and Prepaired Statement

Java Program to delete record using JFrame and Prepaired Statement Here is table Design  Create table demo using two column as id  and Dname , id is primary key  Program source code  +import javax.swing.*;   import java.awt.event.*;   import java.sql.*; public class FrameDelete extends JFrame implements ActionListener{       JTextField tf1,tf2,tf3;       JButton b1,b2;   JLabel l1,l2; Connection con;    PreparedStatement psmt;   int result; //declaration of control    FrameDelete(){           JFrame f= new JFrame("InsertDemo");   // l1=new JLabel("Enter RollNo"); l1.setBounds(150,10,150,50);         tf1=new JTextField();           tf1.setBounds(150,50,150,20);   tf3=new JTextField();           tf3.setBounds(1...

java insert using JFrame and Prepaired Statement

Java Program to insert record using JFrame and Prepaired Statement Here is table Design  Create table demo using two column as id  and Dname , id is primary key  Program source code  import javax.swing.*;   import java.awt.event.*;   import java.sql.*; public class FrameInsert extends JFrame implements ActionListener{       JTextField tf1,tf2,tf3;       JButton b1,b2;   JLabel l1,l2; Connection con;    PreparedStatement psmt;   int result; //declaration of control    FrameInsert(){           JFrame f= new JFrame("InsertDemo");   // l1=new JLabel("Enter RollNo"); l1.setBounds(150,10,150,50);         tf1=new JTextField();           tf1.setBounds(150,50,150,20);   l2=new JLabel("Enter Name"); l2.setBounds(150,60,150,5...