--------------------------------------- class Cliente { public ... main ...{ ... Pila p1, p2; p1 = new Pila(); ... // ... modifiche a p1... p2 = p1; // ... altro codice... } } --------------------------------------- public Object clone() throws CloneNotSupportedException { return super.clone(); } --------------------------------------- try { } catch ( ) { } catch ( ) { } catch ( ) { } finally { } --------------------------------------- class EccPiccolo extends Exception { public EccPiccolo(String s) {super(s);} } class EccGrande extends Exception { public EccGrande(String s) {super(s);} } class EccGrandeGrande extends EccGrande { public EccGrandeGrande(String s) {super(s);} } public class Eccezioni { static void m1() throws EccPiccolo { System.out.println("Entro in m1"); if (Math.random() < 0.4) throw new EccPiccolo("m1"); System.out.println("Esco da m1"); } static void m2() throws EccPiccolo, EccGrande{ System.out.println("Entro in m2"); double x = Math.random(); if (x < 0.4) throw new EccPiccolo("m2"); if (x > 0.6) throw new EccGrande("m2"); System.out.println("Esco da m2"); } static void m3() throws EccGrande, EccGrandeGrande { System.out.println("Entro in m3"); double x = Math.random(); if (x > 0.7) throw new EccGrandeGrande("m3"); if (x > 0.6) throw new EccGrande("m3"); System.out.println("Esco da m3"); } static void m4() throws EccPiccolo { System.out.println("Entro in m4"); m1(); System.out.println("Esco da m4"); } public static void main (String[] args) throws EccGrandeGrande { while (true) { try { m1(); m2(); m3(); m4(); } catch (EccPiccolo e) { System.out.println(" Piccolo: " + e); } catch (EccGrandeGrande e) { System.out.println(" GrandeGrande: " + e); throw new EccGrandeGrande(""); } catch (EccGrande e) { System.out.println(" Grande: " + e); } } } } --------------------------------------- >java Eccezioni Entro in m1 Esco da m1 Entro in m2 Grande: EccGrande: m2 Entro in m1 Esco da m1 Entro in m2 Piccolo: EccPiccolo: m2 Entro in m1 Piccolo: EccPiccolo: m1 Entro in m1 Esco da m1 Entro in m2 Grande: EccGrande: m2 Entro in m1 Esco da m1 Entro in m2 Grande: EccGrande: m2 Entro in m1 Piccolo: EccPiccolo: m1 Entro in m1 Esco da m1 Entro in m2 Grande: EccGrande: m2 Entro in m1 Esco da m1 Entro in m2 Esco da m2 Entro in m3 GrandeGrande: EccGrandeGrande: m3 EccGrandeGrande: at java.lang.Throwable.(Compiled Code) at java.lang.Exception.(Compiled Code) at EccGrande.(Compiled Code) at EccGrandeGrande.(Compiled Code) at Eccezioni.main(Compiled Code) > --------------------------------------- FileInputStream f = new FileInputStream(new File("pippo")); --------------------------------------- FileInputStream f = new FileInputStream("pippo"); --------------------------------------- import java.io.*; public class VisualizzaFile { public static void main(String args[]) { FileInputStream f; int c; if (args.length != 1) System.out.println("Uso: java VisualizzaFile "); else { try { f = new FileInputStream(args[0]); } catch (FileNotFoundException e) { System.out.println("Errore in apertura file"); return; } try { c = f.read(); while (c > 0) { System.out.print((char) c); c = f.read(); } } catch (IOException e) { System.out.println("Errore in lettura file"); } try { f.close(); } catch (IOException e) { System.out.println("Errore in chiusura file"); } } } } --------------------------------------- import java.io.*; public class CopiaFile { public static void main(String args[]) throws FileNotFoundException, IOException { FileInputStream in; FileOutputStream out; int c; if (args.length != 2) System.out.println( "Uso: java CopiaFile "); else { in = new FileInputStream(args[0]); out = new FileOutputStream(args[1]); c = in.read(); while (c > 0) { out.write((char) c); c = in.read(); } in.close(); out.close(); } } } --------------------------------------- import java.io.*; public class DataFile { public static void main (String[] args) throws IOException { DataOutputStream out; DataInputStream in; out = new DataOutputStream(new FileOutputStream("tmp")); out.writeBoolean(false); out.writeDouble(12.34); out.writeChar('q'); out.close(); in = new DataInputStream(new FileInputStream("tmp")); System.out.println(in.readBoolean()); System.out.println(in.readDouble()); System.out.println(in.readChar()); in.close(); } } --------------------------------------- DataOutputStream out = new DataOutputStream("tmp"); --------------------------------------- DataInputStream in = new DataInputStream("tmp"); --------------------------------------- import java.io.*; class FileAccessoCasuale { public static void main (String[] args) throws IOException, EOFException { FileOutputStream out = new FileOutputStream("tmp"); for (char c = 'a'; c <= 'f'; ++c) out.write(c); out.close(); RandomAccessFile f = new RandomAccessFile("tmp", "r"); for (int i = 4; i >= 0; --i) { f.seek(i); System.out.println((char)f.readByte()); } } } --------------------------------------- public final static PrintStream err = ... --------------------------------------- class MyThread extends Thread { public void run() { System.out.println("Thread corrente " + Thread.currentThread()); for (int i = 1; i <= 10; i++) { System.out.println("T " + i); try { Thread.sleep(50); } catch (InterruptedException e) {}; } System.out.println("Fine run"); } } public class ProvaMyThread { public static void main (String args[]) { System.out.println("Thread corrente " + Thread.currentThread()); MyThread t = new MyThread(); t.start(); for (int j = 1; j <= 10; j++) { System.out.println("M " + j); try { Thread.sleep(Math.round(100 * Math.random())); } catch (InterruptedException e) {}; } System.out.println("Fine main"); } } --------------------------------------- >java ProvaMyThread Thread corrente Thread[main,5,main] M 1 Thread corrente Thread[Thread-2,5,main] T 1 T 2 M 2 T 3 T 4 M 3 T 5 M 4 T 6 T 7 M 5 T 8 M 6 T 9 M 7 T 10 M 8 Fine run M 9 M 10 Fine main > --------------------------------------- class MyRun implements Runnable { public void run() { System.out.println("Thread corrente " + Thread.currentThread()); for (int i = 1; i <= 10; i++) { System.out.println("T " + i); try { Thread.sleep(50); } catch (InterruptedException e) {}; } System.out.println("Fine run"); } } public class ProvaMyRun { public static void main (String args[]) { System.out.println("Thread corrente " + Thread.currentThread()); MyRun r = new MyRun(); Thread t = new Thread(r); t.start(); for (int j = 1; j <= 10; j++) { System.out.println("M " + j); try { Thread.sleep(Math.round(100 * Math.random())); } catch (InterruptedException e) {}; } System.out.println("Fine main"); } }