Docsity

Java I/O File Paths and URLs: Constructing, Converting, and Determining Equality, Slides of Java Programming

Partial preview of the text

Download Java I/O File Paths and URLs: Constructing, Converting, and Determining Equality and more Slides Java Programming in PDF only on Docsity!

Java.ioExamples

Filenames

and

Pathnames

Converting

Between

a^

Filename

Path

and

a^

URL

// Create a file objectFile file = new File(

"filename");

// Convert the file object to a URLURL 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 objectfile = new File(url.getFile()); // d:/almanac1.4/java.io/filename// Read the file contents using the URLtry {

// Open an input streamInputStream is = url.openStream();// Read from isis.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.txtfile = new File(

"dir"+File.separatorChar+"filename.txt");

file = file.getAbsoluteFile(); // c:\temp\dir\filename.txtfile = 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(); // nullFile parentDir = file.getParentFile(); // null // Get the parents of an absolute filename path file = new File(

"D:\almanac\Ex1.java");

parentPath = file.getParent(); // D:\almanacparentDir = file.getParentFile(); // D:\almanacparentPath = parentDir.getParent(); // D:\parentDir = parentDir.getParentFile(); // D:\parentPath = parentDir.getParent(); // nullparentDir = 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 existboolean 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 filelong 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 nameFile file = new File(

"oldname");

// File (or directory) with new nameFile 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 movedFile file = new File(

"filename");

// Destination directoryFile dir = new File(

"directoryname");

// Move file to new directoryboolean 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");