Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- /**
- * Created by Mattia on 27/04/2017.
- */
- // definiamo la classe principale
- public class LetturaFile
- {
- public static void main(String[] args)
- {
- // definiamo il percorso al file da leggere
- File doc=new File("C:\\Users\\Mattia\\Desktop\\esempio_testo.txt");
- URL path=null;
- // creaiamo un blocco try-catch per intercettare le eccezioni
- try
- {
- // mostriamo il percorso al file
- path=doc.toURL();
- System.out.println("Il doc si trova nel percorso" + path);
- doc=new File(path.getFile());
- System.out.println("Nome del file " + doc);
- int i;
- // apriamo lo stream di input...
- InputStream is=path.openStream();
- BufferedReader br=new BufferedReader(new InputStreamReader(is));
- // ...e avviamo la lettura del file con un ciclo
- do
- {
- i=br.read();
- System.out.println((char)i);
- }
- while (i!=-1);
- is.close();
- }
- // intercettiamo eventuali eccezioni
- catch (MalformedURLException e)
- {
- System.out.println("Attenzione:" + e);
- }
- catch (IOException e)
- {
- System.out.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement