Objectives:
- Understand the concepts of text files and binary files
- Use the Java File class to create, open, and close files
- Read and write text data to files
- Read and write binary data to files
Introduction:
Files are essential components of software development, allowing for the storage and retrieval of data. Java provides the java.io
package for file handling, including classes for creating, opening, and closing files, reading and writing data, and managing file permissions.
Text Files and Binary Files:
Files can be classified as text files or binary files. Text files contain human-readable data represented as characters, while binary files contain machine-readable data represented as bytes. Java supports both text and binary files, using different methods for reading and writing them.
Creating, Opening, and Closing Files:
The java.io
package provides the File
class for creating, opening, and closing files. To create a file, use the new File()
constructor followed by the file path.
Java
File file = new File("data.txt");
To open a file, use the open()
method of the FileInputStream
or FileOutputStream
class. These classes handle reading or writing text data, respectively.
Java
FileInputStream inputStream = new FileInputStream(file);
FileOutputStream outputStream = new FileOutputStream(file);
After using a file, it’s essential to close it to release associated resources. Use the close()
method of the file input or output stream.
Java
inputStream.close();
outputStream.close();
Reading Text Data:
To read text data from a file, use the read()
method of the FileInputStream
class. This method returns a byte, which is converted to a character using the char
data type.
Java
FileInputStream inputStream = new FileInputStream("data.txt");
int data;
while ((data = inputStream.read()) != -1) {
char character = (char) data;
System.out.print(character);
}
inputStream.close();
Writing Text Data:
To write text data to a file, use the write()
method of the FileOutputStream
class. This method takes a byte array as input, which is converted to a character array using the getBytes()
method of the String
class.
Java
FileOutputStream outputStream = new FileOutputStream("data.txt");
String data = "This is some text data";
byte[] bytes = data.getBytes();
outputStream.write(bytes);
outputStream.close();
Reading Binary Data:
To read binary data from a file, use the read()
method of the DataInputStream
class. This method returns a primitive data type, such as int
, double
, or boolean
.
Java
FileInputStream inputStream = new FileInputStream("data.bin");
DataInputStream dataInputStream = new DataInputStream(inputStream);
int value = dataInputStream.readInt();
System.out.println(value);
dataInputStream.close();
inputStream.close();
Writing Binary Data:
To write binary data to a file, use the write()
method of the DataOutputStream
class. This method takes a primitive data type as input.
Java
FileOutputStream outputStream = new FileOutputStream("data.bin");
DataOutputStream dataOutputStream = new DataOutputStream(outputStream);
dataOutputStream.writeInt(100);
dataOutputStream.close();
outputStream.close();
Summary:
Reading and writing files are essential tasks in software development. Java provides the java.io
package for file handling, allowing you to create, open, and close files, read and write text and binary data. By understanding these concepts, you can effectively manage data storage and retrieval in your Java applications.
Hands-on Practice
Exercise 1: Reading Text Data from a File:
Create a program that reads text data from a file named “data.txt” and displays it on the console.
Java
import java.io.FileInputStream;
public class ReadTextFile {
public static void main(String[] args) throws Exception {
FileInputStream inputStream = new FileInputStream("data.txt");
try {
int data;
while ((data = inputStream.read()) != -1) {
char character = (char) data;
System.out.print(character);
}
} finally {
inputStream.close();
}
}
}
Exercise 2: Writing Text Data to a File:
Create a program that prompts the user to enter some text and then writes that text to a file named “output.txt”.
Java
import java.io.*;
public class WriteTextFile {
public static void main(String[] args) throws Exception {
System.out.print("Enter some text: ");
Scanner scanner = new Scanner(System.in);
String text = scanner.nextLine();
scanner.close();
FileOutputStream outputStream = new FileOutputStream("output.txt");
try {
outputStream.write(text.getBytes());
} finally {
outputStream.close();
}
}
}
Exercise 3: Reading and Writing Binary Data:
Create a program that reads an integer value from a file named “data.bin” and then writes the value 100 to the file.
Java
import java.io.*;
public class ReadAndWriteBinary {
public static void main(String[] args) throws Exception {
DataInputStream dataInputStream = new DataInputStream(new FileInputStream("data.bin"));
try {
int value = dataInputStream.readInt();
System.out.println("Read value: " + value);
DataOutputStream dataOutputStream = new DataOutputStream(new FileOutputStream("data.bin"));
dataOutputStream.writeInt(100);
} finally {
dataInputStream.close();
}
}
}