Java I/O 01. The File class - checking file properties and attributes

Опубликовано: 05 Май 2026
на канале: Snipcademy
726
0

We will cover java's File class in this tutorial.

The following code may have been modified to meet Youtube's guidelines. Please go to    • Java I/O 00. Input/Output Basics Setup   to download the actual files.

/**
GUI File Chooser
You can also have the user choose a file through a GUI
This GUI is from javax.swing.JFileChooser
**/

import javax.swing.JFileChooser;
import java.util.Scanner;
import java.io.File;

public class GUIFileChooser {

public static void main(String[] args) throws Exception {

JFileChooser fileChooser = new JFileChooser();

if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
File openedFile = fileChooser.getSelectedFile();

Scanner in = new Scanner(openedFile);

System.out.println("Printing contents of the chosen file: ");

while(in.hasNext()) {
System.out.println(in.nextLine());
}

in.close();

} else {

System.out.println("File not selected");
System.exit(1);

}
}

}