What is File?
A file can refer to various resources such as a disk file, terminal, or printer. Files stored on secondary storage devices are known as physical files.
To process a file within a program, a logical file must be created in RAM. This logical file is essentially an object of a file data type. Java's I/O system provides a consistent interface for file operations, regardless of the underlying device.
A stream is generally a sequence of data and serves as an abstraction between the programmer and the actual device (file). Streams are categorized into two types:
- Text stream: A sequence of characters.
- Binary stream: A sequence of bytes with a direct one-to-one mapping to the external device—no character translation occurs.
File input output in Java
To work with files in Java, the java.io
package must be imported using:
import java.io.*;
Java allows reading from input devices like the keyboard and files using various I/O classes:
Reading Input
- BufferedReader: Reads characters from an input stream efficiently.
- FileReader: Reads characters from a file.
- InputStreamReader: Converts byte streams to character streams using a specified charset.
Example:
BufferedReader br = new BufferedReader(new FileReader("b.txt")); BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Writing Output
- BufferedWriter: Writes characters to an output stream efficiently.
- FileWriter: Writes characters to a file and is often used with BufferedWriter.
- OutputStreamWriter: Converts character streams to byte streams using a specified charset.
Java File I/O Class Hierarchy and Relationships
public class FileInputStream
extendsInputStream
public class FileReader
extendsInputStreamReader
public class FileOutputStream
extendsOutputStream
public class FileWriter
extendsOutputStreamWriter
public class File
extendsObject
public abstract class InputStream
extendsObject
public abstract class OutputStream
extendsObject
public class BufferedReader
extendsReader
public class BufferedWriter
extendsWriter
public class DataInputStream
extendsFilterInputStream
implementsDataInput
public class DataOutputStream
extendsFilterOutputStream
implementsDataOutput
File and FileDialog Classes
The File
class is part of the java.io.*
package. It represents
an abstract view of file and directory pathnames in a system-independent manner.
Constructor:
File(String pathname)
Creates a new File
instance by converting the given pathname string into an abstract pathname.
Common Methods:
boolean canRead()
: Checks if the file is readable.boolean canWrite()
: Checks if the file is writable.String getName()
: Returns the name of the file or directory.
FileDialog Class
The FileDialog
class, from the java.awt
package, provides a platform-specific dialog that allows the user to select files for reading or writing.
Static Constants:
FileDialog.LOAD
: Used to open a file (read mode).FileDialog.SAVE
: Used to save a file (write mode).
Common Constructor:
FileDialog(Frame parent, String title, int mode)
- Frame parent: The parent window of the dialog.
- String title: The title of the dialog window.
- int mode: Indicates whether the dialog is for loading or saving a file.
The FileDialog
class can be used in combination with other I/O classes such as:
FileWriter
,FileReader
InputStreamReader
,OutputStreamWriter
BufferedReader
,BufferedWriter
Common Methods:
String getDirectory()
: Returns the selected directory.String getFile()
: Returns the selected file.
Example:→Creating new File with Java:
Source code
import java.io.File;
public class OneCreateFile {
public OneCreateFile() {
createFile(); //Calling createFile()
}
public static void main(String[] args) {
OneCreateFile cr = new OneCreateFile();
}
public void createFile()
{
try {
File file = new File("oop.txt");
if (file.createNewFile()) {
System.out.println("File Created!");
System.out.println("Created File Name:"+file.getName());
System.out.println("Created File can Execute?:"+file.canExecute());
System.out.println("Created File can Write?:"+file.canWrite());
} else {
System.out.println("File aleardy Created!");
System.out.println("File Information");
System.out.println("Absolute Path:" + file.getAbsolutePath());
}
} catch (Exception e) {
System.err.println("Exception:" + e.getMessage());
}
}
}