Home >Java >javaTutorial >Discover class libraries for Java file operations
To explore the classes used for file reading and writing in Java, specific code examples are required
In Java programming, file reading and writing is a very common operation. Java provides a variety of classes to support file reading and writing operations. This article will explore the classes used for file reading and writing in Java and give specific code examples.
The main classes used for file reading and writing in Java are File, FileWriter, FileReader, BufferedReader and BufferedWriter. Below we will introduce these classes one by one and show their application in file reading and writing.
import java.io.File; import java.io.IOException; public class FileExample { public static void main(String[] args) { try { File file = new File("example.txt"); if (file.createNewFile()) { System.out.println("文件创建成功!"); } else { System.out.println("文件已存在!"); } } catch (IOException e) { e.printStackTrace(); } } }
import java.io.FileWriter; import java.io.IOException; public class FileWriterExample { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); writer.write("Hello, World!"); writer.close(); System.out.println("写入成功!"); } catch (IOException e) { e.printStackTrace(); } } }
The following is an example of using the FileReader class to read a file:
import java.io.FileReader; import java.io.IOException; public class FileReaderExample { public static void main(String[] args) { try { FileReader reader = new FileReader("example.txt"); int data; while ((data = reader.read()) != -1) { System.out.print((char) data); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderExample { public static void main(String[] args) { try { BufferedReader reader = new BufferedReader(new FileReader("example.txt")); String line; while ((line = reader.readLine()) != null) { System.out.println(line); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
The following is an example of using the BufferedWriter class to write a file:
import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; public class BufferedWriterExample { public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter("example.txt")); writer.write("Hello, World!"); writer.newLine(); writer.write("This is an example."); writer.close(); System.out.println("写入成功!"); } catch (IOException e) { e.printStackTrace(); } } }
Through the above code example, we can see Java provides a variety of classes to support file reading and writing operations. Using these classes, we can easily create, write, and read files. I hope this article will be helpful to your learning and practice in file reading and writing!
The above is the detailed content of Discover class libraries for Java file operations. For more information, please follow other related articles on the PHP Chinese website!