Classes InputStream et OutputStream.
Les méthodes principales des
classes InputStream et OutputStream
sont:
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("Thе file "+fns+" does not exist"); } finally{ if(fi != null)fi.close(); if(fo != null)fo.close(); } } } |
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(); } } } |
import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; public class FcharCopy { 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+"\n"); FileReader fi = null; FileWriter fo = null; try { fi = new FileReader(fns); fo = new FileWriter(fnd); while((bt = fi.read())!=-1){ fo.write(bt); System.out.print((char)bt); } System.out.println("\n\nfile "+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(); } } } |
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; public class FlineCopy { public static void main(String arg[]) throws IOException{ String line = null; String fns=null, fnd=null; BufferedReader sin=null; BufferedReader fi = null; PrintWriter fo = null; try { sin= new BufferedReader(new InputStreamReader(System.in)); System.out.print("input the name of the source file:"); fns = sin.readLine(); System.out.print("input the name of the destination file:"); fnd = sin.readLine(); System.out.println("copy the file "+fns+" in the file "+fnd+"\n"); fi = new BufferedReader(new FileReader(fns)); fo = new PrintWriter(new FileWriter(fnd)); while((line = fi.readLine())!= null){ System.out.println(line); fo.println(line); } System.out.println("\n\nfile "+fns+" is copied as "+fnd); } catch(FileNotFoundException ex){ System.out.println("The file "+fns+" does not exist"); System.out.println("or the file "+fnd+" can not be created"); } finally{ if(fi != null)fi.close(); if(fo != null)fo.close(); } } } |
import java.io.Serializable; public class PersonSr implements Serializable{ private static final long serialVersionUID = 1L; static int num=0; String name; String id; PersonSr(){ name = "student "+ ++num; id = (int)(Math.random()*3999)+1111+""; } } public class StudentSr extends PersonSr{ private static final long serialVersionUID = 1L; int notes[]; StudentSr(){ notes = new int [5]; for(int i =0;i<notes.length;i++){ notes[i]=(int)(Math.random()*5)+2; } } public String toString(){ String rez; rez = "name:"+name+"\tid:"+id+"\tnotes:"; for(int i=0;i<notes.length;i++){ rez += "\t"+notes[i]; } return rez; } } |
import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectOutputStream; public class StudWr { public static void main(String arg[]) throws IOException{ ObjectOutputStream oos = null; StudentSr s; try{ oos = new ObjectOutputStream ( new FileOutputStream ("save.ser")); for(int i=0;i<9;i++){ s =new StudentSr(); System.out.print(""+s); oos.writeObject(s); System.out.println(" # is serialized"); } } finally{ oos.close(); } } } |
import java.io.EOFException; import java.io.FileInputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectInputStream; public class StudRd { public static void main(String[] arg)throws IOException,ClassNotFoundException{ ObjectInputStream ois = null; StudentSr s; int n=0, nf=0; try{ ois = new ObjectInputStream ( new FileInputStream ("save.ser")); for(;;){ s= (StudentSr)ois.readObject(); n++; if(average(s)>=4){ System.out.print(""+s); System.out.println("\thas average "+ average(s)); nf++; } } } catch(EOFException ex){ System.out.println("printed "+nf+" students, found total "+n+" students"); } finally{ ois.close(); } } public static double average(StudentSr s){ double sum; int i; for(sum=i=0;i<s.notes.length;i++){ sum+=s.notes[i]; } return sum/s.notes.length; } } |
class
MyClass implements Serializable { transient Thread thread; //Skip serialization of the transient field transient String fieldIdontwantSerialization; // Serialize the rest of the fields int data; String x; // More code } |
Exercice
Modifiez le programme pour utiliser deux fichiers texte - names.txt (contient les noms des étudiants) et numbers.txt (contient leurs numéros correspondants). Arrêtez le programme en cas d'une faute ou bien la fin d'un des deux fichiers.import
java.io.*; public class FileLst{ public static void main(String args[]) { try{ File path = new File(args[0]); String[] list = path.list(); for(int i = 0; i < list.length; i++) System.out.println(list[i]); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("there is no command line parameter"); } catch (NullPointerException ex) { System.err.println("The directory does not exist"); } } } |
import
java.io.*; import java.util.*; public class FileInf{ public static void main(String args[]) { File f; try{ f = new File(args[0]); } catch (ArrayIndexOutOfBoundsException e) { System.err.println("there is no command line parameter"); return; } if(!f.exists()){ System.err.println("The "+f+" does not exist"); return; } System.out.println( " Abs path: " + f.getAbsolutePath() + "\n can write: " + f.canWrite() + "\n can read: " + f.canRead() + "\n Name: " + f.getName() + "\n Parent " + f.getParent() + "\n Path: " + f.getPath() + "\n Modif: " + new Date(f.lastModified())); if(f.isFile()) System.out.println("This is a file with length "+ f.length() + " bytes"); else if(f.isDirectory()) System.out.println("This is a directory"); } } |
import java.io.*; public class FileManip{ public static void main(String args[]){ File newF = new File("D:\\tst\\Test.txt"); File chng = new File("D:\\tst\\Test_change.txt"); File dir1 = new File("D:\\tst\\Text.txt"); if(dir1.mkdirs()){ System.out.println("Directory "+dir1+" created"); } else { System.out.println("Directory "+dir1+" exist yet"); } try{ if(newF.createNewFile()){ System.out.println("File "+newF+" created"); } else{ System.out.println("File "+newF+" exist yet"); } }catch (IOException e) { System.out.println("the path to file "+ newF + "does not exist" ); } if(newF.renameTo(chng)){ System.out.println("File "+newF+" renamed to" + chng); } else { System.out.println("File "+newF+" does not exist"); } if (chng.delete()){ System.out.println("File "+ chng + " deleted"); } else { System.out.println("File "+chng + "can not be deleted"); } if(dir1.delete()){ System.out.println("Directory " + dir1 + " deleted"); } else { System.out.println("Directory " + dir1 + " can not be deleted"); } } } |