Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.34 KB | None | 0 0
  1. import java.io.*;
  2. import java.net.*;
  3. /**
  4. * Created by Mattia on 27/04/2017.
  5. */
  6.  
  7. // definiamo la classe principale
  8. public class LetturaFile
  9. {
  10. public static void main(String[] args)
  11. {
  12. // definiamo il percorso al file da leggere
  13. File doc=new File("C:\\Users\\Mattia\\Desktop\\esempio_testo.txt");
  14. URL path=null;
  15.  
  16. // creaiamo un blocco try-catch per intercettare le eccezioni
  17. try
  18. {
  19. // mostriamo il percorso al file
  20. path=doc.toURL();
  21. System.out.println("Il doc si trova nel percorso" + path);
  22.  
  23. doc=new File(path.getFile());
  24. System.out.println("Nome del file " + doc);
  25. int i;
  26.  
  27. // apriamo lo stream di input...
  28. InputStream is=path.openStream();
  29. BufferedReader br=new BufferedReader(new InputStreamReader(is));
  30.  
  31. // ...e avviamo la lettura del file con un ciclo
  32. do
  33. {
  34. i=br.read();
  35. System.out.println((char)i);
  36. }
  37. while (i!=-1);
  38. is.close();
  39. }
  40.  
  41. // intercettiamo eventuali eccezioni
  42. catch (MalformedURLException e)
  43. {
  44. System.out.println("Attenzione:" + e);
  45. }
  46. catch (IOException e)
  47. {
  48. System.out.println(e.getMessage());
  49. }
  50. }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement