Created
November 8, 2022 07:49
-
-
Save ProfAndreaPollini/1371e8333d31408f625ac28c9de8ae5a to your computer and use it in GitHub Desktop.
Lettura e scrittura da file
This file contains hidden or 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
| package fileio; | |
| import java.io.BufferedReader; | |
| import java.io.FileNotFoundException; | |
| import java.io.FileReader; | |
| import java.io.IOException; | |
| public class FileTest { | |
| public static final String FILENAME = "pippo.txt"; | |
| // public static void main(String[] args) throws FileNotFoundException { | |
| // FileReader fileReader = new FileReader(FILENAME); | |
| // } | |
| public static void main(String[] args) { | |
| try { | |
| var fileReader = new BufferedReader(new FileReader(FILENAME)); | |
| //System.out.println(fileReader.readLine()); | |
| String line = fileReader.readLine(); | |
| while( line != null) { | |
| System.out.println(line); | |
| line = fileReader.readLine(); | |
| } ; | |
| } catch (FileNotFoundException e) { | |
| System.out.println("File non trovato"); | |
| } catch (IOException e) { | |
| System.out.println("errore I/O"); | |
| } | |
| } | |
| } |
This file contains hidden or 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
| package fileio; | |
| import java.io.IOException; | |
| import java.nio.file.Files; | |
| import java.nio.file.Path; | |
| public class NewFileTest { | |
| public static final String FILENAME = "pippo.txt"; | |
| public static void main(String[] args) { | |
| var path = Path.of(FILENAME); | |
| try { | |
| var fileReader = Files.newBufferedReader(path); | |
| System.out.println(path); | |
| String line = fileReader.readLine(); | |
| while( line != null) { | |
| System.out.println(line); | |
| line = fileReader.readLine(); | |
| } ; | |
| } catch (IOException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment