import java.io.*;
class record implements Serializable
{
static Scanner input=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int roll;
String name;This Java File IO tutorial helps you understand and use the FileInputStream and FileOutputStream classes for manipulating binary files.In Java, FileInputStream and FileOutputStream are byte streams that read and write data in binary format, exactly 8-bit bytes. They are descended from the abstract classes InputStream and OutputStream which are the super types of all byte streams.The following class diagram helps you understand the API hierarchy of these byte stream classes:You can notice that these classes implement the AutoCloseable interface, which means that we can use the try-with-resources structure to close the streams automatically.
File
or a path name, and/or specify to overwrite or append to an existing file, using the following constructors:
File handling is an important part of any application. Java has several methods for creating, reading, updating, and deleting files.
import java.util.*;class record implements Serializable
{
static Scanner input=new Scanner(System.in);
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int roll;
String name;
void insert()
{
try
{
System.out.println(“n Enter Roll No:: “);
roll=input.nextInt();
System.out.println(“n Enetr Name:: “);
name=br.readLine();
FileOutputStream fout=new FileOutputStream(“Student.txt”,true);
byte buff[]=name.getBytes();
fout.write((char)roll);
fout.write(‘n’);
fout.write(buff);
fout.write(‘n’);
fout.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void delete(int r)
{
try
{
FileInputStream fin = new FileInputStream(“student.txt”);
FileOutputStream fout=new FileOutputStream(“temp.txt”);
{
try
{
System.out.println(“n Enter Roll No:: “);
roll=input.nextInt();
System.out.println(“n Enetr Name:: “);
name=br.readLine();
FileOutputStream fout=new FileOutputStream(“Student.txt”,true);
byte buff[]=name.getBytes();
fout.write((char)roll);
fout.write(‘n’);
fout.write(buff);
fout.write(‘n’);
fout.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void delete(int r)
{
try
{
FileInputStream fin = new FileInputStream(“student.txt”);
FileOutputStream fout=new FileOutputStream(“temp.txt”);
while(true)
{
int a=fin.read();
if(a-1)
break;
if(a!=r)
{
fout.write((byte)a);
a=fin.read();
fout.write(a);
while(true)
{
a=fin.read();
if(a’n’)
break;
fout.write(a);
}
fout.write(a);
}
else
{
a=fin.read();
do{
a=fin.read();
}while(a!=’n’);
}
}
fin.close();
fout.close();
File f=new File(“student.txt”);
File f1=new File(“temp.txt”);
if(f.delete())
f1.renameTo(new File(“student.txt”));
else
System.out.println(“Error”);
{
int a=fin.read();
if(a-1)
break;
if(a!=r)
{
fout.write((byte)a);
a=fin.read();
fout.write(a);
while(true)
{
a=fin.read();
if(a’n’)
break;
fout.write(a);
}
fout.write(a);
}
else
{
a=fin.read();
do{
a=fin.read();
}while(a!=’n’);
}
}
fin.close();
fout.close();
File f=new File(“student.txt”);
File f1=new File(“temp.txt”);
if(f.delete())
f1.renameTo(new File(“student.txt”));
else
System.out.println(“Error”);
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void showdata(int r)
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void showdata(int r)
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
int a=fin.read();
if(a-1)
break;
if(ar)
{
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’n’);
}
else
{
do
{
a=fin.read();
}while(a!=’n’);
}
if(a-1)
break;
if(ar)
{
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’n’);
}
else
{
do
{
a=fin.read();
}while(a!=’n’);
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
void show()
{
try
{
{
try
{
FileInputStream fin=new FileInputStream(“student.txt”);
while(true)
{
int a=fin.read();
if(a-1)
break;
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’n’);
while(true)
{
int a=fin.read();
if(a-1)
break;
System.out.println(“roll no:: “+a);
a=fin.read();
System.out.println(“Name:: “);
do
{
a=fin.read();
System.out.print((char)a);
}while(a!=’n’);
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
public class Student extends record
{
public static void main(String args[])
{
try
{
while(true){
System.out.println(“nEnter your choice : “);
System.out.println(“1:Insert.”);
System.out.println(“2:Delete.”);
System.out.println(“3:View record.”);
System.out.println(“4:View All.n”);
int choice=input.nextInt();
record r=new record();
switch(choice)
{
case 1:
r.insert();
break;
case 2:
System.out.println(“Enter roll no. : “);
int roll=input.nextInt();
r.delete(roll);
break;
case 3:
System.out.println(“Enter roll no. : “);
roll=input.nextInt();
r.showdata(roll);
break;
case 4:
r.show();
break;
default:
System.out.println(“Invalid Entry !!”);
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
{
public static void main(String args[])
{
try
{
while(true){
System.out.println(“nEnter your choice : “);
System.out.println(“1:Insert.”);
System.out.println(“2:Delete.”);
System.out.println(“3:View record.”);
System.out.println(“4:View All.n”);
int choice=input.nextInt();
record r=new record();
switch(choice)
{
case 1:
r.insert();
break;
case 2:
System.out.println(“Enter roll no. : “);
int roll=input.nextInt();
r.delete(roll);
break;
case 3:
System.out.println(“Enter roll no. : “);
roll=input.nextInt();
r.showdata(roll);
break;
case 4:
r.show();
break;
default:
System.out.println(“Invalid Entry !!”);
break;
}
}
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
- Details
- Written by Nam Ha Minh
- Last Updated on 28 July 2019 | Print Email
1. Understanding the FileInputStream Class
The FileInputStream is a byte input stream class that provides methods for reading bytes from a file. We can create an instance of this class by supplying a File or a path name, using these two constructors:- FileInputStream(File file)
- FileInputStream(String name)
- int available(): returns an estimate of the number of remaining bytes that can be read.
- int read(): reads one byte of data, returns the byte as an integer value. Return -1 if the end of the file is reached.
- int read(byte[]): reads a chunk of bytes to the specified byte array, up to the size of the array. This method returns -1 if there’s no more data or the end of the file is reached.
- int read(byte[], int offset, int length): reads up to length bytes of data from the input stream.
- long skip(long n): skips over and discards n bytes of data from the input stream. This method returns the actual number of bytes skipped.
- FileChannel getChannel(): returns the unique FileChannel object associated with this file input stream. The FileChannel can be used for advanced file manipulation (New IO).
- void close(): Closes this file input stream and releases any system resources associated with the stream.
2. Understanding the FileOutputStream Clas
The FileOutputStream is a byte output stream class that provides methods for writing bytes to a file. We can create an instance of this class by supplying a![Simple File Handling Program In Java Simple File Handling Program In Java](https://embed-ssl.wistia.com/deliveries/fa496adfe01d33a6134d2e281e2cc46486275f1a.jpg?image_crop_resized)
![File File](/uploads/1/2/6/3/126383966/273452207.png)
- FileOutputStream(File file)
- FileOutputStream(File file, boolean append): if append is true, then the bytes will be written to the end of an existing file rather than the beginning.
- FileOutputStream(String name)
- FileOutputStream(String name, boolean append)
- void write(int): writes the specified byte to the output stream.
- void write(byte[]): writes the specified array of bytes to the output stream.
- void write(byte[], int offset, int length): writes length bytes from the specified byte array starting at offset to the output stream.
- FileChannel getChannel(): returns the unique FileChannel object associated with this file output stream. The FileChannel can be used for advanced file manipulation (New IO).
- void close(): Closes this file output stream and releases any system resources associated with the stream.