Get Job-ready: Java Course with 45+ Real-time Projects! - Learn Java
Program 1
package dataflair;
import java.io.*;
public class TestRead1
{
public static void main(String[] args) throws IOException
{
File F=new File("d://dataflair/student.txt");
char ch;
int n;
if(F.isFile())
{
FileInputStream fis=null;
try
{
fis=new FileInputStream(F);
while((n=fis.read())!=-1)
{
ch=(char)(n);
System.out.print(ch);
}
}
finally
{
fis.close();
}
}
else
System.out.println("File not Found......");
}
}
Program 2
package dataflair;
import java.io.*;
public class TestRead2
{
public static void main(String[] args) throws IOException
{
File F=new File("d://dataflair/student.txt");
char ch;
if(F.isFile())
{
FileInputStream fis=null;
try
{
fis=new FileInputStream(F);
int n;
n=(int)(F.length());
byte b[];
b=new byte[n];
fis.read(b);
String S=new String(b);
System.out.println(S);
}
finally
{
fis.close();
}
}
}
}