/* Tiedosto: Listaa1.java Tekijä: Juha Peltomäki, 8.1998. Aihe: Tällä funktiolla luetaan tiedosto. Listataan, joko Tiedoston sisältö tai hakemiston tiedostojen nimet. */ import java.io.*; class Listaa1 { public static void main (String args[] ) throws Exception { DataInputStream syotto = new DataInputStream(System.in); System.out.print("Anna luettavan tiedoston nimi>"); System.out.flush(); String tied_nimi = syotto.readLine(); // Luodaan uusi tiedosto sille annetulla nimellä File tiedosto = new File(tied_nimi); /* Jos tiedostoa ei ole olemassa tai sitä ei voi lukea */ if (!tiedosto.exists() || !tiedosto.canRead()) { System.out.println("Ei voi avata tai lukea " + tiedosto); } /* Jos tiedosto on hakemisto */ else if (tiedosto.isDirectory()) { String [] tiedostot = tiedosto.list(); System.out.println("Listaan hakemiston sisällön:"); for (int i=0; i< tiedostot.length; i++) System.out.println(tiedostot[i]); } /* Jos tiedosto löytyi ja saatiin avattua */ else try { FileInputStream ts = new FileInputStream(tiedosto); byte [] data = new byte [ts.available()]; ts.read(data); System.out.write(data); } catch (FileNotFoundException e) { System.out.println("Tiedostoa ei löytynyt!" + e.toString()); } // Tämä pysäyttää tulostuksen JBuilderissa. VOI POISTAA MYÖHEMMIN! try { System.out.print("Lopeta>"); int koe = System.in.read(); } catch (java.io.IOException ioe) { } // ************************************************* } }