In the software development process, file operations are a common requirement. As a popular programming language, Java has powerful file operation functions. This article will introduce you to the "Top 10 Best Practices for Using Java to Operate Files" to help you operate files more efficiently and safely. Whether you are a Java beginner or an experienced developer, you will benefit from these practices. This article is carefully compiled by PHP editor Zimo, hoping to provide some help for your development work.
// 使用 Path 类操作文件 Path path = Paths.get("my-file.txt"); Files.readAllBytes(path); // 使用 nio.Path 类操作文件 java.nio.file.Path path = java.nio.file.Paths.get("my-file.txt"); java.nio.file.Files.readAllBytes(path);
try (BufferedReader reader = new BufferedReader(new FileReader("my-file.txt"))) { String line; while ((line = reader.readLine()) != null) { // Process the line } } catch (IOException e) { // Handle the exception }
// 创建一个新文件 Files.createFile(Paths.get("my-file.txt")); // 写入文件 Files.write(Paths.get("my-file.txt"), "Hello world!".getBytes()); // 读取文件 byte[] bytes = Files.readAllBytes(Paths.get("my-file.txt")); String content = new String(bytes); // 删除文件 Files.delete(Paths.get("my-file.txt"));
// 写入文件 try (BufferedWriter writer = new BufferedWriter(new FileWriter("my-file.txt"))) { writer.write("Hello world!"); } catch (IOException e) { // Handle the exception } // 读取文件 try (BufferedReader reader = new BufferedReader(new FileReader("my-file.txt"))) { String line; while ((line = reader.readLine()) != null) { // Process the line } } catch (IOException e) { // Handle the exception }
// 写入文件 try (FileChannel channel = FileChannel.open(Paths.get("my-file.txt"), StandardOpenOption.WRITE)) { MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_WRITE, 0, 1024); buffer.put("Hello world!".getBytes()); } catch (IOException e) { // Handle the exception } // 读取文件 try (FileChannel channel = FileChannel.open(Paths.get("my-file.txt"), StandardOpenOption.READ)) { MappedByteBuffer buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, 1024); String content = new String(buffer); } catch (IOException e) { // Handle the exception }
// 遍历文件和目录 Files.walk(Paths.get("/home/user"), new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { // Process the file return FileVisitResult.CONTINUE; } });
// 复制文件 Files.copy(Paths.get("src.txt"), Paths.get("dest.txt")); // 移动文件 Files.move(Paths.get("src.txt"), Paths.get("dest.txt")); // 删除文件 Files.delete(Paths.get("my-file.txt"));
// 获取文件信息 File file = new File("my-file.txt"); System.out.println(file.getName()); System.out.println(file.getPath()); System.out.println(file.length());
// 在文件中随机访问数据 try (RandomAccessFile file = new RandomAccessFile("my-file.txt", "rw")) { // Seek to a specific position in the file file.seek(100); // Read data from the file byte[] bytes = new byte[10]; file.read(bytes); } catch (IOException e) { // Handle the exception }
// 选择性地写入文件 FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { return file.getName().endsWith(".txt"); } }; try (FileOutputStream output = new FileOutputStream("output.txt")) { Files.copy(Files.list(Paths.get("/home/user")).filter(filter), output); } catch (IOException e) { // Handle the exception }
The above is the detailed content of 10 Best Practices for Manipulating Files in Java. For more information, please follow other related articles on the PHP Chinese website!