Sul mio manuale di java si riporta questo esempio:
In base a quello che ho capito il programma sopra utilizza read() per leggere e visualizzare i contenuti di un file contenente testo ASCI. Sul manuale c'é anche scritto che il nome del file deve essere specificato come argomento della riga di comando ma non si spiega fisicamente cosa si deve fare su eclipse e non si dice neppure dove essere salvato questo benedetto .txt. Mi aspettavo che nel codice ci fosse una URL dove java potesse andare a prelevare il file...codice:/* Display a text file. To use this program, specify the name of the file that you want to see. For example, to see a file called TEST.TXT, use the following command line. java ShowFile TEST.TXT */ import java.io.*; class ShowFile { public static void main(String args[]) { int i; FileInputStream fin; // First, confirm that a file name has been specified. if(args.length != 1) { System.out.println("Usage: ShowFile filename"); return; } // Attempt to open the file. try { fin = new FileInputStream(args[0]); } catch(FileNotFoundException e) { System.out.println("Cannot Open File"); return; } // At this point, the file is open and can be read. // The following reads characters until EOF is encountered. try { do { i = fin.read(); if(i != -1) System.out.print((char) i); } while(i != -1); } catch(IOException e) { System.out.println("Error Reading File"); } // Close the file. try { fin.close(); } catch(IOException e) { System.out.println("Error Closing File"); } } }
![]()

Rispondi quotando
non posso andare avanti a leggere! 