diff --git a/com/study/demo5/DataIO.java b/com/study/demo5/DataIO.java new file mode 100644 index 0000000..cfacd63 --- /dev/null +++ b/com/study/demo5/DataIO.java @@ -0,0 +1,40 @@ +package com.study.demo5; + +import java.io.*; + +public class DataIO { + public void writeConfig(String path) { + try (DataOutputStream dos = new DataOutputStream( + new FileOutputStream(path))) { + + dos.writeUTF("李华"); // 用户名 + dos.writeInt(25); // 年龄 + dos.writeBoolean(true); // 是否启用 + dos.writeDouble(1234.56); // 余额 + + System.out.println("配置写入成功: " + path); + + } catch (IOException e) { + System.err.println("写入配置失败: " + e.getMessage()); + } + } + + public void readConfig(String path) { + try (DataInputStream dis = new DataInputStream( + new FileInputStream(path))) { + + String username = dis.readUTF(); + int age = dis.readInt(); + boolean enabled = dis.readBoolean(); + double balance = dis.readDouble(); + + System.out.println("用户名: " + username); + System.out.println("年龄: " + age); + System.out.println("是否启用: " + enabled); + System.out.println("余额: " + balance); + + } catch (IOException e) { + System.err.println("读取配置失败: " + e.getMessage()); + } + } +} diff --git a/com/study/demo5/FileCopy.java b/com/study/demo5/FileCopy.java new file mode 100644 index 0000000..ff7b220 --- /dev/null +++ b/com/study/demo5/FileCopy.java @@ -0,0 +1,20 @@ +package com.study.demo5; + +import java.io.*; + +public class FileCopy { + public void copy(String source, String target) { + try (FileInputStream fis = new FileInputStream(source); + FileOutputStream fos = new FileOutputStream(target)) { + byte[] buffer = new byte[1024]; + int bytesRead; + while ((bytesRead = fis.read(buffer)) != -1) { + fos.write(buffer, 0, bytesRead); + } + } catch (FileNotFoundException e) { + System.err.println("源文件未找到: " + e.getMessage()); + } catch (IOException e) { + System.err.println("文件复制失败: " + e.getMessage()); + } + } +} diff --git a/com/study/demo5/FileStat.java b/com/study/demo5/FileStat.java new file mode 100644 index 0000000..e43a65c --- /dev/null +++ b/com/study/demo5/FileStat.java @@ -0,0 +1,25 @@ +package com.study.demo5; + +import java.io.*; + +public class FileStat { + public long countBytes(String filePath) { + long totalBytes = 0; + + try (FileInputStream fis = new FileInputStream(filePath)) { + + byte[] buffer = new byte[1024]; + int bytesRead; + + while ((bytesRead = fis.read(buffer)) != -1) { + totalBytes += bytesRead; + } + + } catch (IOException e) { + System.err.println("文件读取失败: " + e.getMessage()); + return -1; + } + + return totalBytes; + } +} diff --git a/com/study/demo5/Main.java b/com/study/demo5/Main.java new file mode 100644 index 0000000..e3d1a6b --- /dev/null +++ b/com/study/demo5/Main.java @@ -0,0 +1,17 @@ +package com.study.demo5; + + public class Main { + public static void main(String[] args) { + // 7.1 测试 + FileCopy fc = new FileCopy(); + fc.copy("source.txt", "target.txt"); + // 7.2 测试 + DataIO dataIO = new DataIO(); + dataIO.writeConfig("config.bin"); + dataIO.readConfig("config.bin"); + // 7.3 测试 + FileStat fs = new FileStat(); + long size = fs.countBytes("test3.txt"); + System.out.println("文件大小: " + size + " 字节"); + } +} diff --git a/flake.nix b/flake.nix index 3ca1e3b..2a4f052 100644 --- a/flake.nix +++ b/flake.nix @@ -3,7 +3,7 @@ inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; outputs = { self, nixpkgs }: let - supportedSystems = [ "x86_64-linux" "aarch64-linux" ]; + supportedSystems = [ "x86_64-linux" ]; forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f { pkgs = import nixpkgs { inherit self system; }; });