2. Streams



Schreiben in eine Datei:
[weiter]

    import java.io.*;
    
    class FileOutputDemo {
    
        static public void main(String args[]) throws IOException {
            DataOutputStream stream;
    
            try {
                stream = new DataOutputStream(new FileOutputStream("myfile"));    
    
                stream.writeInt(args.length);
                for(int i = 0; i < args.length; i++)
                    stream.writeUTF(args[i]);
            }
            finally {
                stream.close();
            }
        }
    }

Lesen aus einer Datei:

    import java.io.*;
    
    class FileInputDemo {
    
        static public void main(String args[]) throws IOException {
            Strimg arg = "";
            int num;
            DataInputStream stream;
    
            try {
                stream = new DataInputStream(new FileInputStream("myfile"));      
    
                num = stream.readInt();
                while (num > 0) {
                    arg = stream.readUTF();
                    System.out.println(arg);
                    num--;
                }
            }
            finally {
                stream.close();
            }
        }
    }


Zurück zur Übersicht; Doku; Beispiele: Class Hierarchy Index