/** <B>Grundlagen der Informatik</B>
 *  <BR>Beispiel Dateizugriff
 *  <BR>Schreiben und Lesen einer Datei 
 */
public class File1 {

    public static void main(String args[]) throws IOException  {

        String text      = new String("Hello World!");
        String dateiName = new String("file1");

        schreibeString(dateiName, text);

        String lesen = leseString(dateiName);

        System.out.println("\n"+lesen+"\n");
    }  // main

    /** Methode schreibt String data in Datei name */
    private static void schreibeString(String name, String data) throws IOException
    {

        FileOutputStream fout     = new FileOutputStream(name);
        BufferedOutputStream bout = new BufferedOutputStream(fout);
        DataOutputStream out      = new DataOutputStream(bout);

        for (int i = 0; i <= data.length()-1; i++) 
            out.writeChar(data.charAt(i));

        out.close();
        bout.close();
        fout.close();

    }   // schreibeString

    /** Methode liest String aus Datei name und gibt ihn zurueck */
    private static String leseString(String name) throws IOException {

        FileInputStream fin     = new FileInputStream(name);
        BufferedInputStream bin = new BufferedInputStream(fin);
        DataInputStream in      = new DataInputStream(bin);

        char gelesen;
        String res = new String("");

        while (in.available() > 0)
            res += in.readChar();

        in.close();
        bin.close();
        fin.close();

        return res;

    }  // leseString

}  // File1
