Notes
Notes - notes.io |
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.swing.JFileChooser; // Added missing import
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
public class JFileChooserDemo extends JFrame
{
private final JTextArea outputArea; // displays file contents
// set up GUI
public JFileChooserDemo() throws IOException
{
super("JFileChooser Demo");
outputArea = new JTextArea();
add(new JScrollPane(outputArea)); // outputArea is scrollable
analyzePath(); // get Path from user and display info
}
// display information about file or directory user specifies
public void analyzePath() throws IOException
{
// get Path to user-selected file or directory
Path path = getFileOrDirectoryPath();
if (path != null && Files.exists(path)) // if exists, display info
{
// gather file (or directory) information
StringBuilder builder = new StringBuilder();
builder.append(String.format("%s:%n", path.getFileName()));
builder.append(String.format("%s a directory%n",
Files.isDirectory(path) ? "Is" : "Is not"));
builder.append(String.format("%s an absolute path%n",
path.isAbsolute() ? "Is" : "Is not"));
builder.append(String.format("Last modified: %s%n",
Files.getLastModifiedTime(path)));
builder.append(String.format("Size: %s%n", Files.size(path)));
builder.append(String.format("Path: %s%n", path));
builder.append(String.format("Absolute path: %s%n",
path.toAbsolutePath()));
if (Files.isDirectory(path)) // output directory listing
{
builder.append(String.format("%nDirectory contents:%n"));
// object for iterating through a directory's contents
DirectoryStream<Path> directoryStream =
Files.newDirectoryStream(path);
for (Path p : directoryStream)
builder.append(String.format("%s%n", p));
}
outputArea.setText(builder.toString()); // display String content
}
else // Path does not exist
{
JOptionPane.showMessageDialog(this, path.getFileName() +
" does not exist.", "ERROR", JOptionPane.ERROR_MESSAGE);
}
} // end method analyzePath
// allow user to specify file or directory name
private Path getFileOrDirectoryPath()
{
// configure dialog allowing selection of a file or directory
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = fileChooser.showOpenDialog(this);
// if user clicked Cancel button on dialog, return
if (result == JFileChooser.CANCEL_OPTION)
System.exit(1);
// return Path representing the selected file
return fileChooser.getSelectedFile().toPath();
}
// Main method to run the application
public static void main(String[] args) throws IOException {
JFileChooserDemo application = new JFileChooserDemo();
application.setSize(400, 400);
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.setVisible(true);
}
} // end class JFileChooserDemo
![]() |
Notes is a web-based application for online taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000+ notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 14 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team
