Example - writing objects in a file (persons with name and age read and written in the file save.dat)

import java.io.Serializable;
public class Pers implements Serializable{
    String name;
    int age;
    Pers(String name,int age){
        this.name = name;
        this.age = age;
    }
}


import java.io.*;
public class ArrPers implements Serializable{
    int size;
    Pers arr[];
    ArrPers(){
        arr = new Pers[5];
        size = 0;
    }
    void add(Pers p){
        if(size >= arr.length-1){
            Pers temp[] = new Pers[2*arr.length];
            System.arraycopy(arr, 0, temp, 0, arr.length);
            arr=temp;
        }
        arr[size++]=p;
    }
    void prt(){
        System.out.println ("Arrpers: "+size+" objets");
        for(int i=0;i<this.size;i++){
            System.out.println("name: "+arr[i].name+"\tage:"+ arr[i].age);
        }
    }
    void remove (Pers p){
        for(int i=0;i<size;i++){
            if (p==arr[i]){
                arr[i]=arr[--size];
                break;
            }
        }
    }
     void save(){
        try{
            FileOutputStream fos = new FileOutputStream ("save.dat");
            ObjectOutputStream oos = new ObjectOutputStream (fos);
            oos.writeObject(this);        
        }
        catch(Exception ex){}
    }
    static ArrPers load(){
        ArrPers p= null;
        try{
            FileInputStream fis = new FileInputStream ("save.dat");
            ObjectInputStream ois = new ObjectInputStream (fis);
            p= (ArrPers)ois.readObject();           
        }
        catch(Exception ex){}
        return p;
    }
}

public class Test {
    public static void main(String arg[]){
        ArrPers p =null;
        p= ArrPers.load();
        if(p == null) p = new ArrPers();
        p.prt();
        for(int i=0;i<3;i++){
            String name ="client"+(int)(Math.random()*1000);
            int age = (int)(Math.random()*80);
            p.add(new Pers(name,age));
        }
        p.prt();
        p.save();
    }
}



Exemple - read and write a text file (copy a text file - The input file and the outpute file, are given as  arguments )

import java.io.*;
public class Copy {
   public static void main(String arg[]) {
      String f1="",f2="";
      try{
          f1=arg[0];
          f2=arg[1];
      }catch (IndexOutOfBoundsException e){
          System.out.println("Usage: java Copy file_1 file_2");
          System.exit(1);
      }
      try { 
         BufferedReader in = new BufferedReader(
                              new FileReader(f1));
         BufferedWriter ot = new BufferedWriter(
                 new FileWriter(f2));
        
         System.out.println("Copy "+arg[0]+" as "+arg[1]);
      
         String tampon="" ;
         while(( tampon=in.readLine()) != null) {
             ot.write(tampon) ;
             ot.newLine();    
         }
         in.close();
         ot.close();
         System.out.println("done");
      }
      catch(FileNotFoundException ef){
          System.out.print(arg[0]+" does not exist");
          System.out.println(" or "+arg[1]+ " can not be created");
          System.exit(3);
      }
      catch(IOException e) {
          e.printStackTrace();
          System.exit(4);
      }

    }
}

Exemple - read and write a file binary

To test download in the project directory one small file -  eg   icon.png  from https://ff.tu-sofia.bg/JTech/go-back-icon.png
and one larger file - eg test.mp4  from https://jsoncompare.org/LearningContainer/SampleFiles/Video/MP4/Sample-MP4-Video-File-Download.mp4

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FCopy {
public static void main(String arg[]) throws IOException{
int bt;
if(arg.length!=2){
System.out.println("Usage: FCopy fileSource fileDestination");
return;
}
String fns = arg[0], fnd = arg[1];
System.out.println("copy the file "+fns+" in the file "+fnd);
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(fns);
fo = new FileOutputStream(fnd);
while((bt = fi.read())!=-1)fo.write(bt);
System.out.println("file "+fns+" is copied as "+fnd);
}
catch(FileNotFoundException ex){
System.out.println("The file "+fns+" does not exist");
}
finally{
if(fi != null)fi.close();
if(fo != null)fo.close();
}
}
}

Using a buffer

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FCopyArr {
public static void main(String arg[]) throws IOException{
byte bt[] = new byte[2048];
int len;
if(arg.length!=2){
System.out.println("Usage: FCopy fileSource fileDestination");
return;
}
String fns = arg[0], fnd = arg[1];
System.out.println("copy the file "+fns+" in the file "+fnd);
FileInputStream fi = null;
FileOutputStream fo = null;
try {
fi = new FileInputStream(fns);
fo = new FileOutputStream(fnd);
while((len = fi.read(bt))!=-1){
fo.write(bt,0,len);
}
System.out.println("file "+fns+" is copied as "+fnd);
}
catch(FileNotFoundException ex){
System.out.println("The file "+fns+" does not exist");
}
finally{
if(fi != null)fi.close();
if(fo != null)fo.close();
}
}
}