0% found this document useful (0 votes)
13 views55 pages

Java I-O

The document provides an overview of I/O streams in Java, detailing the two main types: byte streams and character streams, along with their respective classes and methods. It includes examples of using FileInputStream, FileOutputStream, BufferedInputStream, BufferedOutputStream, ObjectInputStream, ObjectOutputStream, FileReader, and FileWriter for reading and writing data. Additionally, it discusses the BufferedReader class for efficient text reading and presents sample programs for file copying and counting characters, lines, and words in a text file.

Uploaded by

srinusirisala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views55 pages

Java I-O

The document provides an overview of I/O streams in Java, detailing the two main types: byte streams and character streams, along with their respective classes and methods. It includes examples of using FileInputStream, FileOutputStream, BufferedInputStream, BufferedOutputStream, ObjectInputStream, ObjectOutputStream, FileReader, and FileWriter for reading and writing data. Additionally, it discusses the BufferedReader class for efficient text reading and presents sample programs for file copying and counting characters, lines, and words in a text file.

Uploaded by

srinusirisala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 55

I/O Streams

Stream: in java, stream is a continuous flow of data.


In java streams of two types.
1. Byte Streams
2. Character Streams
• Byte streams are used to read or write binary data.
• Character streams are used to handle input and output of characters.
Byte Streams :
In Byte streams, the top level two abstract classes are
• InputStream
• OutputStream.
Methods :
The InputStream and OutputStream classes defined several key
methods.
Two of the most important are read( ) and write( ), which, respectively,
read and write bytes of data.
FileInputStream Class
• Java FileInputStream class obtains input bytes from a file.
• It is used for reading byte-oriented data (streams of raw bytes) such
as image data, audio, video etc.
Java Program: Reading Data from a File using FileInputStream
import java.io.FileInputStream;
try {
import java.io.IOException;
// Close the stream if it was opened
if (fileInputStream != null) {
public class FileInputStreamExample {
fileInputStream.close();
}
public static void main(String[] args) {
} catch (IOException e) {
FileInputStream fileInputStream = null;
e.printStackTrace();
try {
}
// Specify the file path
}
fileInputStream = new FileInputStream("example.txt");
}
}
// Read data from the file byte by byte
int data;
while ((data = fileInputStream.read()) != -1) {
// Convert byte data to character and print
System.out.print((char) data);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
FileOutputStream Class
• Java FileOutputStream is an output stream used for writing data to a
file.
• If you have to write primitive values into a file, use FileOutputStream
class.
• You can write byte-oriented as well as character-oriented data
through FileOutputStream class.
Java Program: Writing Data to a File using FileOutputStream
import java.io.FileOutputStream;
import java.io.IOException; catch (IOException e) {
e.printStackTrace();
public class FileOutputStreamExample{ }

public static void main(String[] args) { }


FileOutputStream fileOutputStream = null; }
try {
// Specify the file path (the file will be created if it
doesn't exist)
fileOutputStream = new FileOutputStream("output.txt");
// Data to be written to the file
String data = "Hello, World! This is written to the file
using FileOutputStream.";

// Convert string to bytes and write to the file


fileOutputStream.write(data.getBytes());
System.out.println("Data written to the file successfully!");

}
BufferedInputStream Class
• Java BufferedInputStream class is used to read information from
stream.
Constructor:

Constructor Description
It creates the BufferedInputStream and saves it
BufferedInputStream(InputStream IS)
argument, the input stream IS, for later use.

Method:
int read() :It read the next byte of data from the input stream.
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class BufferedInputStreamExample{

public static void main(String[] args) {


BufferedInputStream bufferedInputStream = null;
try {
// Specify the file path
FileInputStream fileInputStream = new FileInputStream("example.txt");
bufferedInputStream = new BufferedInputStream(fileInputStream);

// Read data from the file


int data;
while ((data = bufferedInputStream.read()) != -1) {
// Convert byte data to character and print
System.out.print((char) data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Java BufferedOutputStream Class
• Java BufferedOutputStream class is used for buffering an output stream.
class declaration
• public class BufferedOutputStream extends FilterOutputStream
Constructor
BufferedOutputStream(OutputStream os) :
Constructor Description
It creates the new buffered output stream which is used for writing the
BufferedOutputStream(OutputStream os)
data to the specified output stream.

Methods:
Method Description
void write(int b) It writes the specified byte to the buffered output stream.
void flush() It flushes the buffered output stream.
import java.io.*;
public class BufferedOutputStreamExample{
public static void main(String args[])throws Exception{
FileOutputStream fout=new FileOutputStream("D://javaprogs/testout.txt");
BufferedOutputStream bout=new BufferedOutputStream(fout);
String s="Welcome to javaTpoint.";
byte b[]=s.getBytes();
bout.write(b);
bout.flush();

bout.close();
fout.close();
System.out.println("success");
}
}
ObjectOutputStream:
The purpose of ObjectOutputStream in Java is to serialize objects,
which means converting an object’s state intoa sequence of bytes.
These bytes can then be saved to a file, transmitted over a network,
or stored in a database.
ate an ObjectOutputStream
reates a FileOutputStream where objects from ObjectOutputStream are written
eOutputStream fileStream = new FileOutputStream(String file);

reates the ObjectOutputStream


ectOutputStream objStream = new ObjectOutputStream(fileStream);

thods
te() - writes a byte of data to the output stream
teBoolean() - writes data in boolean form
teChar() - writes data in character form
teInt() - writes data in integer form
teObject() - writes object to the output stream
ObjectInputStream Class
The ObjectInputStream class of the java.io package can be used to read objects that were
previously written by ObjectOutputStream.

Working of ObjectInputStream
• The ObjectInputStream is mainly used to read data written by the ObjectOutputStream.
• Basically, the ObjectOutputStream converts Java objects into corresponding streams. This is known
as serialization. Those converted streams can be stored in files or transferred through networks.
• Now, if we need to read those objects, we will use the ObjectInputStream that will convert the
streams back to corresponding objects. This is known as deserialization.
Methods
Create an ObjectInputStream • readBoolean() - reads data in boolean form
•readChar() - reads data in character form
// Creates a file input stream linked with the specified file •readInt() - reads data in integer form
•readObject() - reads the object from the
FileInputStream fileStream = new FileInputStream(String file);
input stream
// Creates an object input stream using the file input stream •read() - reads a byte of data from the input
stream
ObjectInputStream objStream = new ObjectInputStream(fileStream);
import java.io.FileInputStream; // Reads data using the ObjectInputStream
import java.io.FileOutputStream; FileInputStream fileStream = new
import java.io.ObjectInputStream; FileInputStream("file.txt");
import java.io.ObjectOutputStream; ObjectInputStream objStream = new
ObjectInputStream(fileStream);
class FileOutputStreamEX1{
public static void main(String[] args) { System.out.println("Integer data :" +
objStream.readInt());
int data1 = 5; System.out.println("String data: " +
String data2 = "This is programiz"; objStream.readObject());

try { output.close();
objStream.close();
FileOutputStream file = new FileOutputStream("file.txt"); }

// Creates an ObjectOutputStream catch (Exception e) {


ObjectOutputStream output = new ObjectOutputStream(file); e.getStackTrace();
}
// writes objects to output stream }
output.writeInt(data1); }
output.writeObject(data2);
import java.io.FileInputStream; try {
import java.io.FileOutputStream; FileOutputStream fileOut = new FileOutputStream("file1.txt");
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; // Creates an ObjectOutputStream
import java.io.Serializable; ObjectOutputStream objOut = new ObjectOutputStream(fileOut);

class Dog implements Serializable { // Writes objects to the output stream


objOut.writeObject(dog1 );
String name; // Reads the object
String breed; FileInputStream fileIn = new FileInputStream("file1.txt");
ObjectInputStream objIn = new ObjectInputStream(fileIn);
public Dog(String name, String breed) {
this.name = name; // Reads the objects
this.breed = breed; Dog newDog = (Dog) objIn.readObject();
}
} System.out.println("Dog Name: " + newDog.name);
System.out.println("Dog Breed: " + newDog.breed);
class FileOutputStreamEx2{
public static void main(String[] args) { objOut.close();
objIn.close();
// Creates an object of Dog class }
Dog dog1 = new Dog("Tyson", "Labrador"); catch (Exception e) {
e.getStackTrace();
); }
}
Character Stream Classes:
• Character Stream classes are used to work with 16-bit Unicode
characters. They can perform operations on characters, char arrays
and Strings.
Character streams are defined by using two class hierarchies.
1. Reader
2. Writer.
In java.io package Reader and Writer classes are abstract.
There are many child classes for Reader and Writer.
• Reader and Writer define several key methods that the other stream
classes implement.
• Two of the most important methods are read( ) and write( ),
Reader and Writer Classes Hierarchy
Reader Child Classes
SN Class Description

1. BufferedReader This class provides methods to read characters from the buffer.

2. CharArrayReader This class provides methods to read characters from the char array.

3. FileReader This class provides methods to read characters from the file.

4 InputStreamReader This class provides methods to convert bytes to characters.

5 StringReader This class provides methods to read characters from a string.

Reader Class Method

SN Method Description
This method returns the integral representation of the next character
1 int read() present in the input. It returns -1 if the end of the input is
encountered.
Writer Class child classes
SN Class Description

1 BufferedWriter This class provides methods to write characters to the buffer.

2 FileWriter This class provides methods to write characters to the file.

3 CharArrayWriter This class provides methods to write the characters to the


character array.

4 OutpuStreamWriter This class provides methods to convert from bytes to characters.

6 StringWriter This class provides methods to write the characters to the string.
Write Class Methods:
SN Method Description
This method is used to write the data to the output
1 void write()
stream.
This method is used to write a single character to
2 void write(int i)
the output stream.
This method is used to write the array of characters
3 Void write(char buffer[]) to the output stream.

This method is used to write the nChars characters


4 void write(char buffer [],int loc, int nChars) to the character array from the specified location.

This method is used to close the output stream.


5 void close () However, this generates the IOException if an
attempt is made to write to the output stream after
closing the stream.

This method is used to flush the output stream and


6 void flush () writes the waiting buffered characters.
Java FileReader Class
• It is character-oriented class which is used for file handling in java.
• Java FileReader class is used to read data from the file. It returns data
in byte format.
Constructors of FileReader class
Constructor Description
It gets filename in string. It opens the given file in read mode. If file
FileReader(String file) doesn't exist, it throws FileNotFoundException.

Methods
It is used to return a character in ASCII form. It returns -1 at the end
int read() of file.
//In this example, we are reading the data from the text file testout.txt using
Java FileReader class.
1. package com.javatpoint;
2.
3. import java.io.FileReader;
4. public class FileReaderExample {
5. public static void main(String args[])throws Exception{
6. FileReader fr=new FileReader("D:\\testout.txt");
7. int i;
8. while((i=fr.read())!=-1)
9. System.out.print((char)i);
10. fr.close();
11. }
12. }
FileWriter Class
• Java FileWriter class is used to write character-oriented data to a file.
• Unlike FileOutputStream class, you don't need to convert string into
byte array because it provides method to write string directly.
Constructors
Constructor Description
FileWriter(String file) Creates a new file. It gets file name in string.

Methods
Method Description
void write(String text) It is used to write the string into FileWriter.
void write(char c) It is used to write the char into FileWriter.
void write(char[] c) It is used to write char array into FileWriter.
void flush() It is used to flushes the data of FileWriter.
void close() It is used to close the FileWriter.
FileWriter Example
1. package com.javatpoint;
2. import java.io.FileWriter;
3. public class FileWriterExample {
4. public static void main(String args[]){
5. try{
6. FileWriter fw=new FileWriter("D:\\testout.txt");
7. fw.write("Welcome to javaTpoint.");
8. fw.close();
9. }catch(Exception e){System.out.println(e);}
10. System.out.println("Success...");
11. }
12. }
BufferedReader Class
• Java BufferedReader class is used to read the text from a character-
based input stream.
• It can be used to read data line by line using readLine() method. It
makes the performance fast.
• It inherits Reader class.
constructors
Constructor Description
It is used to create a buffered character input stream that uses the
BufferedReader(Reader rd)
default size for an input buffer.
Methods
Method Description
int read() It is used for reading a single character.

int read(char[] cbuf, int off, int len) It is used for reading characters into a portion of an array.
String readLine() It is used for reading a line of text.
It closes the input stream and releases any of the system
void close()
resources associated with the stream.
BufferedReader Example
1. package com.javatpoint;
2. import java.io.*;
3. public class BufferedReaderExample {
4. public static void main(String args[])throws Exception{
5. FileReader fr=new FileReader("D:\\testout.txt");
6. BufferedReader br=new BufferedReader(fr);
7.
8. int i;
9. while((i=br.read())!=-1){
10. System.out.print((char)i);
11. }
12. br.close();
13. fr.close();
14. }
15. }
Using FileReader and FileWriter, write a program to perform file
copying and any other suitable operations.
import java.io.FileReader; // Read each character and write it to the destination file
import java.io.FileWriter; while ((data = reader.read()) != -1) {
writer.write(data);
import java.io.IOException;
characterCount++;
}
public class FileCopy {
public static void main(String[] args) { System.out.println("File copied successfully!");
System.out.println("Total characters copied: " +
FileReader reader = null;
characterCount);
FileWriter writer = null; } catch (IOException e) {
int characterCount = 0; System.out.println("An error occurred: " +
e.getMessage());
} finally {
try {
try {
// Specify the source and destination files // Close FileReader and FileWriter to free resources
String sourceFile = "source.txt"; if (reader != null) reader.close();
String destFile = "destination.txt"; if (writer != null) writer.close();
} catch (IOException e) {
System.out.println("Failed to close resources: " +
// Create FileReader and FileWriter objects e.getMessage());
reader = new FileReader(sourceFile); }
writer = new FileWriter(destFile); }
}
}
int data;
Write a Java Program that displays the number of characters, lines and
words in a text file.
import java.io.BufferedReader; charCount += line.length(); // Add number of
import java.io.FileReader; characters in the current line
import java.io.IOException;
// Split the line into words and count them
public class FileStatistics { String[] words = line.split(“ "); // Regex for spaces,
tabs, etc.
public static void main(String[] args) {
wordCount += words.length;
// Path to the file
}
String filePath = "sample.txt"; // Update this with the
actual file path
// Output the results
int charCount = 0; System.out.println("Number of characters: " +
charCount);
int wordCount = 0;
System.out.println("Number of words: " +
int lineCount = 0; wordCount);
System.out.println("Number of lines: " + lineCount);
try (BufferedReader reader = new
BufferedReader(new FileReader(filePath))) { } catch (IOException e) {
String line; System.out.println("An error occurred while reading
the file: " + e.getMessage());
// Read the file line by line }
while ((line = reader.readLine()) != null) { }
lineCount++; // Increment line count }
Predefined Streams:
In java “System” is a class which contains three predefined stream
variables:
1. In
2. Out
3. err.
These fields are declared as public, static, and final within System.
• System.out refers to the standard output stream(console o/p:
monitor).
• System.in refers to standard input (console i/p: keyboard)
• System.err refers to the standard error stream(which also is the
console by default).
Reading Console Input:
In java, console input read by following statement.
BufferedReader br = new BufferedReader(new InputStreamReader(System.in))
Here,
• BufferedReader character stream class which connect to the System.in through
InputStreamReader .
• InputStreamReader can converts bytes to characters.
Java program that uses BufferedReader to read different types of values

import java.io.BufferedReader; // Reading a double


import java.io.InputStreamReader; System.out.print("Enter a double: ");
import java.io.IOException; double dbl = Double.parseDouble(reader.readLine());
System.out.println("You entered: " + dbl);
public class BufferedReaderExample {
public static void main(String[] args) { // Reading a character (first character of string input)
BufferedReader reader = new BufferedReader(new System.out.print("Enter a character: ");
InputStreamReader(System.in)); char character = reader.readLine().charAt(0);
System.out.println("You entered: " + character);
try {
// Reading a string } catch (IOException e) {
System.out.print("Enter a string: "); System.out.println("An error occurred: " + e.getMessage());
String str = reader.readLine(); } catch (NumberFormatException e) {
System.out.println("You entered: " + str); System.out.println("Invalid number format: " +
e.getMessage());
// Reading an integer }
System.out.print("Enter an integer: "); }
int integer = Integer.parseInt(reader.readLine()); }
System.out.println("You entered: " + integer);
Writing Console Output (System.out):
In java we can write on console using
1. System.out(which is object of byte stream class PrintStream).
2. PrintWriter(which is character stream class.)
The following application illustrates using a PrintWriter to handle console output:
// Demonstrate PrintWriter
import java.io.*;
public class PrintWriterDemo{
public static void main(String args[]) {
PrintWriter pw = new PrintWriter(System.out, true);
pw.println("This is a string");
int i = -7;
pw.println(i);
double d = 4.5e-7;
pw.println(d);
}
}
Console class
• The Java Console class is be used to get input from console. It
provides methods to read texts and passwords.
• If you read password using Console class, it will not be displayed to
the user.
• The java.io.Console class is attached with system console internally.
The Console class is introduced since 1.5.
Java Console class declaration:

Let's see the declaration for Java.io.Console class:


public final class Console extends Object implements Flushable
Java Console class methods
Method Description
String readLine() It is used to read a single line of text from the console.

It provides a formatted prompt then reads the single line of text from
String readLine(String fmt, Object... args)
the console.

char[] readPassword() It is used to read password that is not being displayed on the console.

char[] readPassword(String fmt, Object... It provides a formatted prompt then reads the password that is not
args) being displayed on the console.

Console format(String fmt, Object... args) It is used to write a formatted string to the console output stream.

Console printf(String format, Object...


It is used to write a string to the console output stream.
args)
It is used to retrieve the PrintWriter object associated with the
PrintWriter writer()
console.
void flush() It is used to flushes the console.
• import java.io.Console;
import java.io.PrintWriter;
import java.util.Arrays;
public class ConsoleClassMethodsDemo {
public static void main(String[] args) {
// Get the Console object
Console console = System.console();
// Check if console is available (it might be null when running from an IDE)
if (console == null) {
System.out.println("No console available. This program needs to be run in a real terminal.");
return;
}
// 1. readLine() - Read a line from the console
String name = console.readLine("Enter your name: ");
console.printf("Hello, %s!\n", name);
// 2. readLine(String fmt, Object... args) - With format and arguments
String country = console.readLine("Which country are you from,
%s? ", name);
console.printf("So, you're from %s!\n", country);
// 3. readPassword() - Reading a password (password input is
masked)
char[] password = console.readPassword("Enter your password: ");
console.printf("Your password is: %s\n", new String(password)); //
Just for demonstration; don't display passwords in real apps!
// 4. readPassword(String fmt, Object... args) - With format and arguments
char[] confirmPassword = console.readPassword("Please confirm your password, %s:
", name);
if (Arrays.equals(password, confirmPassword)) {
console.printf("Passwords match!\n");
} else {
console.printf("Passwords do not match!\n");
}

// 5. format(String fmt, Object... args) - Output with formatted text


int age = Integer.parseInt(console.readLine("Enter your age: "));
console.format("You are %d years old.\n", age);
// 6. printf(String format, Object... args) - Another way to format output
double salary = Double.parseDouble(console.readLine("Enter your salary: "));
console.printf("Your salary is: %.2f\n", salary);

// 7. writer() - Get a PrintWriter for more advanced console output


PrintWriter writer = console.writer();
writer.println("This line is printed using PrintWriter.");

// 8. flush() - Flush the console's output buffer


console.flush();

System.out.println("Program completed successfully.");


}
}
Scanner
• Scanner class in Java is found in the java.util package. Java provides
various ways to read input from the keyboard, the java.util.Scanner
class is one of them.
• The Java Scanner class breaks the input into tokens using a delimiter
which is whitespace by default. It provides many methods to read and
parse various primitive values.
• The Java Scanner class extends Object class and implements
Iterator and Closeable interfaces.
• Scanner class provides nextXXX() methods to return the type of value
such as nextInt(), nextByte(), nextShort(), next(), nextLine(),
nextDouble(), nextFloat(), nextBoolean(), etc.
• To get a single character from the scanner, you can call next().charAt(0)
method which returns a single character.
Scanner Class Declaration
public final class Scanner
extends Object
implements Iterator<String>
How to get Java Scanner
Scanner sc = new Scanner(System.in);
Scanner Class Constructors
SN Constructor Description
It constructs a new Scanner that produces values scanned
1) Scanner(File source)
from the specified file.
Creates a Scanner object that treats the string "Hello how are
Scanner(String)
2) you" as the input source, not the console. The string will be
split into tokens (words) by spaces.

import java.util.Scanner;

public class ScannerFromStringExample2 {


public static void main(String[] args) {
String input = "Hello how are you";
Scanner sc = new Scanner(input);
// Reading the entire line (even though it's a single string here)
String line = sc.nextLine();
System.out.println("Line: " + line);

sc.close();
}
}
import java.io.File; // Example: Reading line by line
import java.io.FileNotFoundException; while (sc.hasNextLine()) {
import java.util.Scanner; String line = sc.nextLine();
System.out.println("Line: " + line);
public class ScannerFromFileExample { }
public static void main(String[] args) {
try { // Step 4: Close the scanner
// Step 1: Create a File object for the file sc.close();
you want to read
File file = new File("example.txt"); } catch (FileNotFoundException e) {
System.out.println("File not found. Please
// Step 2: Create a Scanner object that check the file path.");
reads from the file e.printStackTrace();
Scanner sc = new Scanner(file); }
}
// Step 3: Use the Scanner to read the }
contents of the file
File Class
• The File class is an abstract representation of file and directory
pathname. A pathname can be either absolute or relative.
• The File class have several methods for working with directories and
files such as creating new directories or files, deleting and renaming
directories or files, listing the contents of a directory etc.
Constructors

Constructor Description
It creates a new File instance from a
File(File parent, String child) parent abstract pathname and a
child pathname string.
It creates a new File instance by
File(String pathname) converting the given pathname
string into an abstract pathname.
It creates a new File instance from a
File(String parent, String child) parent pathname string and a child
pathname string.
It creates a new File instance by
File(URI uri) converting the given file: URI into an
abstract pathname.
1. File(File parent, String child)
This constructor creates a new File instance from a parent directory (as a File
object) and a child file name (as a String).
import java.io.File;
public class FileConstructorExample1{
public static void main(String[] args) {
// Parent directory as a File object
File parentDir = new File("D:\\javaprogs");

// Child file name as a string


String childFile = "FileCon1.txt";

// Creating the File object using the parent directory and child file name
File file = new File(parentDir, childFile);

// Printing the absolute path of the file


System.out.println("File path: " + file.getAbsolutePath());
}
2. File(String pathname)
This constructor creates a new File instance by converting a pathname string into an abstract
pathname.

import java.io.File;

public class FileConstructorExample2{


public static void main(String[] args) {
// Providing the full pathname as a string
String pathname = "D://javaprogs/FileCon2.txt";

// Creating the File object using the pathname


File file = new File(pathname);

// Printing the absolute path of the file


System.out.println("File path: " + file.getAbsolutePath());
}
}
File(String parent, String child)
This constructor creates a new File instance from a parent directory name (as a String) and a child file name (as a String).
import java.io.File;

public class FileConstructorExample3{


public static void main(String[] args) {
// Parent directory as a string
String parentDir = "D:\\javaprogs";

// Child file name as a string


String childFile = "Filecon3.txt";

// Creating the File object using the parent directory and child file name
File file = new File(parentDir, childFile);

// Printing the absolute path of the file


System.out.println("File path: " + file.getAbsolutePath());
}
}
4. File(URI uri)
This constructor creates a new File instance by converting a URI into an abstract pathname.
You must ensure the URI points to a file on the local file system (e.g., using the file: scheme).
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;

public class FileConstructorExample4 {


public static void main(String[] args) {
try {
// Creating a URI pointing to a file on the local file system
URI fileUri = new URI("file:///C:/Users/John/Documents/example.txt");

// Creating the File object using the URI


File file = new File(fileUri);

// Printing the absolute path of the file


System.out.println("File path: " + file.getAbsolutePath());
} catch (URISyntaxException e) {
System.out.println("Invalid URI");
e.printStackTrace();
}
}
}
Modifier and Method Description
Type
It atomically creates a new, empty file named by this
boolean createNewFile() abstract pathname if and only if a file with this name
does not yet exist.
It tests whether the application can modify the file
boolean canWrite()
denoted by this abstract pathname.String[]
It tests whether the application can execute the file
boolean canExecute()
denoted by this abstract pathname.
It tests whether the application can read the file
boolean canRead()
denoted by this abstract pathname.
boolean isAbsolute() It tests whether this abstract pathname is absolute.
It tests whether the file denoted by this abstract
boolean isDirectory()
pathname is a directory.
It tests whether the file denoted by this abstract
boolean isFile()
pathname is a normal file.
It returns the name of the file or directory denoted by this abstract
String getName()
pathname.
It returns the pathname string of this abstract pathname's parent, or
String getParent()
null if this pathname does not name a parent directory.
It returns a java.nio.file.Path object constructed from the this abstract
Path toPath()
path.
URI toURI() It constructs a file: URI that represents this abstract pathname.
It returns an array of abstract pathnames denoting the files in the
File[] listFiles()
directory denoted by this abstract pathname
It returns the number of unallocated bytes in the partition named by
long getFreeSpace()
this abstract path name.
It returns an array of strings naming the files and directories in the
String[] list(FilenameFilter filter) directory denoted by this abstract pathname that satisfy the specified
filter.
boolean mkdir() It creates the directory named by this abstract pathname.

You might also like