Docsity

Java File I/O Operations and Directory Management, Slides of Computer Engineering and Programming

Partial preview of the text

Download Java File I/O Operations and Directory Management and more Slides Computer Engineering and Programming in PDF only on Docsity!

Java.io

Examples

Filenames and Pathnames

Converting Between a Filename Path

and a URL

// 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 }

Getting an Absolute Filename Path

from a Relative Filename Path

File file = new File("filename.txt");

file = file.getAbsoluteFile(); // c:\temp\filename.txt

file = new File("dir"+File.separatorChar+"filename.txt");

file = file.getAbsoluteFile(); // c:\temp\dir\filename.txt

file = new File(".."+File.separatorChar+"filename.txt");

file = file.getAbsoluteFile(); // c:\temp..\filename.txt

// Note that filename.txt does not need to exist

Getting the Parents of a Filename

Path

// 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

Determining If a Filename Path Is a

File or a Directory

File dir = new File("directoryName");

boolean isDir = dir.isDirectory();

if (isDir)

{

// dir is a directory

}

else

{

// dir is a file

}

Determining If a File or Directory

Exists

boolean exists = (new File("filename")).exists();

if (exists) { // File or directory exists } else { // File or directory does not exist }

Creating a File

try {

File file = new File("filename");

// 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) { }

Getting the Size of a File

File file = new File("infilename");

// Get the number of bytes in the file

long length = file.length();

Deleting a File

boolean success = (new file( "filename" )).delete();

if (!success)

// Deletion failed

Renaming a File or Directory

// File (or directory) with old name

File file = new File("oldname");

// File (or directory) with new name

File file2 = new File("newname");

// Rename file (or directory) boolean success = file.renameTo(file2); if (!success) { // File was not successfully renamed

}

Moving a File or Directory to Another

Directory

// File (or directory) to be moved

File file = new File("filename");

// Destination directory

File dir = new File("directoryname");

// Move file to new directory boolean success = file.renameTo(new File(dir, file.getName())); if (!success) { // File was not successfully moved

}

Directory

Getting the Current Working

Directory

String curDir = System.getProperty("user.dir");