














Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Examples of java i/o operations including constructing a filename path, converting between a filename path and a url, getting an absolute filename path from a relative one, determining if two filename paths refer to the same file, getting the parents of a filename path, determining if a filename path is a file or a directory, checking if a file or directory exists, creating a file, copying one file to another, getting the size of a file, deleting a file, creating a temporary file, renaming a file or directory, moving a file or directory to another directory, getting and setting the modification time of a file or directory, getting the current working directory, creating a directory, and copying a directory. These examples are useful for understanding how to manipulate files and directories in java.
Typology: Slides
1 / 22
This page cannot be seen from the preview
Don't miss anything!
// Create a file object File file = new File("filename"); // Convert the file object to a URL URL url = null; try { // The file need not exist. url = file.toURL(); // file:/d:/almanac1.4/java.io/filename } catch (MalformedURLException e) { }
// Convert the URL to a file object file = new File(url.getFile()); // d:/almanac1.4/java.io/filename // Read the file contents using the URL try { // Open an input stream InputStream is = url.openStream(); // Read from is is.close(); } catch (IOException e) { // Could not open the file }
file = file.getAbsoluteFile(); // c:\temp\filename.txt
file = file.getAbsoluteFile(); // c:\temp\dir\filename.txt
file = file.getAbsoluteFile(); // c:\temp..\filename.txt
// Note that filename.txt does not need to exist
// Get the parent of a relative filename path File file = new File("Ex1.java"); String parentPath = file.getParent(); // null File parentDir = file.getParentFile(); // null
// Get the parents of an absolute filename path file = new File("D:\almanac\Ex1.java"); parentPath = file.getParent(); // D:\almanac parentDir = file.getParentFile(); // D:\almanac
parentPath = parentDir.getParent(); // D:
parentDir = parentDir.getParentFile(); // D:\
parentPath = parentDir.getParent(); // null parentDir = parentDir.getParentFile(); // null
File dir = new File("directoryName");
boolean isDir = dir.isDirectory();
if (isDir)
{
// dir is a directory
}
else
{
// dir is a file
}
if (exists) { // File or directory exists } else { // File or directory does not exist }
try {
// Create file if it does not exist boolean success = file.createNewFile(); if (success) { // File did not exist and was created } else { // File already exists }
} catch (IOException e) { }
// Get the number of bytes in the file
long length = file.length();
// File (or directory) with old name
// File (or directory) with new name
// Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed
}
// File (or directory) to be moved
// Destination directory
// Move file to new directory boolean success = file.renameTo(new File(dir, file.getName())); if (!success) { // File was not successfully moved
}
String curDir = System.getProperty("user.dir");