Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Reading and Writing Files in Java | Input-Output (I-O) and Networking
Advanced Java 2077
course content

Course Content

Advanced Java 2077

Advanced Java 2077

1. Data Structures
2. Sorting and Searching
3. Concurrent Programming
4. Input-Output (I-O) and Networking
5. Java GUI Development

bookReading and Writing Files in Java

Reading and writing files is a common task in many Java programs. Java provides several classes and interfaces for reading and writing files, which can be used to perform Input/Output (I/O) operations using streams.

File Class

In Java, the File class represents a file or directory on the file system. The File class provides methods for creating, deleting, and renaming files and directories, as well as for getting information about a file or directory, such as its name, path, and size.

To create an instance of the File class, you need to provide the path to the file or directory you want to work with. Here is an example code:

java

Main

copy
1
File file = new File("path/to/file.txt");

Reading Files

To read a file in Java, you need to create an instance of the appropriate input stream class and use its methods to read data from the file. Java provides several classes for reading files, including FileInputStream, FileReader, BufferedReader, and Scanner.

Here's an example of reading a file using a Scanner:

java

Main

copy
12345678910
try { Scanner scanner = new Scanner(new File("path/to/file.txt")); while (scanner.hasNextLine()) { String line = scanner.nextLine(); System.out.println(line); } scanner.close(); } catch (FileNotFoundException e) { e.printStackTrace(); }

In this code, we use a Scanner to read data from a file called file.txt. The hasNextLine() method of the Scanner class is used to check if there is more data to be read, and the nextLine() method is used to read a single line of text from the file.

Writing Files

To write data to a file in Java, you need to create an instance of the appropriate output stream class and use its methods to write data to the file. Java provides several classes for writing files, including FileOutputStream, FileWriter, BufferedWriter, and PrintWriter.

Here's an example of writing to a file using a PrintWriter:

java

Main

copy
1234567
try { PrintWriter writer = new PrintWriter(new FileWriter("path/to/file.txt")); writer.println("Hello, world!"); writer.close(); } catch (IOException e) { e.printStackTrace(); }

In this code, we use a PrintWriter to write the text "Hello, world!" to a file called file.txt. The println() method of the PrintWriter class is used to write a line of text to the file.

File Input/Output Streams

In addition to the classes and interfaces for reading and writing files, Java also provides classes for working with input and output streams directly. These classes include FileInputStream, FileOutputStream, BufferedInputStream, and BufferedOutputStream.

Here's an example of reading a file using FileInputStream:

java

Main

copy
12345678910
try { FileInputStream in = new FileInputStream("path/to/file.txt"); int c; while ((c = in.read()) != -1) { System.out.print((char) c); } in.close(); } catch (IOException e) { e.printStackTrace(); }

In this code, we use a FileInputStream to read data from a file called file.txt. The read() method of the FileInputStream class is used to read a single byte of data at a time, and the while loop is used to read all the data from the file.

1. Which of the following classes is used to read data from a text file in Java?
2. Which method of the Scanner class is used to read a single line of text from a file in Java?
Which of the following classes is used to read data from a text file in Java?

Which of the following classes is used to read data from a text file in Java?

Select the correct answer

Which method of the Scanner class is used to read a single line of text from a file in Java?

Which method of the Scanner class is used to read a single line of text from a file in Java?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 4. Chapter 2
some-alt