Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

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

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

2011/2012

Uploaded on 07/11/2012

dhananad
dhananad 🇮🇳

4

(4)

39 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java.io
Examples
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

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");