Skip to content

Instantly share code, notes, and snippets.

@ProfAndreaPollini
Created November 8, 2022 07:49
Show Gist options
  • Select an option

  • Save ProfAndreaPollini/1371e8333d31408f625ac28c9de8ae5a to your computer and use it in GitHub Desktop.

Select an option

Save ProfAndreaPollini/1371e8333d31408f625ac28c9de8ae5a to your computer and use it in GitHub Desktop.
Lettura e scrittura da file
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");
}
}
}
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