Detailed explanation of the classes for Java file read and write operations
In Java programming, file read and write operations are a very common and important part. Through file read and write operations, we can achieve functions such as persistent storage of data, reading of data, copying and deleting files. Java provides many classes and methods to support file reading and writing operations. This article will introduce in detail several commonly used classes for Java file reading and writing operations, and provide specific code examples.
1.1 Create a file
Use the File class to create a new file, which can be achieved by calling the createNewFile() method. The sample code is as follows:
File file = new File("D:/test.txt"); // 创建File对象 try { if (file.createNewFile()) { System.out.println("文件创建成功!"); } else { System.out.println("文件已存在!"); } } catch (IOException e) { e.printStackTrace(); }
1.2 Delete file
Use the File class to delete an existing file, which can be achieved by calling the delete() method. The sample code is as follows:
File file = new File("D:/test.txt"); // 创建File对象 if (file.delete()) { System.out.println("文件删除成功!"); } else { System.out.println("文件删除失败!"); }
1.3 Obtaining file information
Use the File class to obtain file-related information, such as file name, file path, file size, etc. The sample code is as follows:
File file = new File("D:/test.txt"); // 创建File对象 System.out.println("文件名:" + file.getName()); System.out.println("文件路径:" + file.getPath()); System.out.println("文件大小:" + file.length() + "字节"); System.out.println("是否为目录:" + file.isDirectory()); System.out.println("是否为文件:" + file.isFile());
2.1 File reading
Use the FileInputStream class to read the contents of a file. The sample code is as follows:
FileInputStream fis = null; try { fis = new FileInputStream("D:/test.txt"); byte[] buffer = new byte[1024]; int length; while ((length = fis.read(buffer)) != -1) { System.out.write(buffer, 0, length); } } catch (IOException e) { e.printStackTrace(); } finally { if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } }
2.2 File writing
Use the FileOutputStream class to write data to a file. The sample code is as follows:
FileOutputStream fos = null; try { fos = new FileOutputStream("D:/test.txt"); String content = "Hello, World!"; byte[] bytes = content.getBytes(); fos.write(bytes); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.1 Text file reading
Use the BufferedReader class to read the contents of a text file. The sample code is as follows:
BufferedReader br = null; try { br = new BufferedReader(new FileReader("D:/test.txt")); String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } }
3.2 Text file writing
Use the BufferedWriter class to write data to a text file. The sample code is as follows:
BufferedWriter bw = null; try { bw = new BufferedWriter(new FileWriter("D:/test.txt")); bw.write("Hello, World!"); } catch (IOException e) { e.printStackTrace(); } finally { if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } }
Summary:
This article introduces in detail some common classes for Java file reading and writing operations, including File class, FileInputStream class, FileOutputStream class, BufferedReader class and BufferedWriter class, and provides specific code example. By learning and mastering the use of these classes, we can perform file reading and writing operations more flexibly and efficiently, further improving our Java programming capabilities.
The above is the detailed content of Detailed explanation of Java file operations. For more information, please follow other related articles on the PHP Chinese website!