Created
April 27, 2017 15:33
-
-
Save mattiaferigutti/4a940c16d45f02bedbcbb7668a702dd7 to your computer and use it in GitHub Desktop.
Lettura del file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment