Зміст курсу
Advanced Java 2077
Advanced Java 2077
Reading 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:
Main
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
:
Main
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
:
Main
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
:
Main
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.
Дякуємо за ваш відгук!