mirror of
https://github.com/Dichgrem/Java.git
synced 2025-12-16 12:41:58 -05:00
update:demo5
This commit is contained in:
40
com/study/demo5/DataIO.java
Normal file
40
com/study/demo5/DataIO.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
20
com/study/demo5/FileCopy.java
Normal file
20
com/study/demo5/FileCopy.java
Normal file
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
25
com/study/demo5/FileStat.java
Normal file
25
com/study/demo5/FileStat.java
Normal file
@@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
17
com/study/demo5/Main.java
Normal file
17
com/study/demo5/Main.java
Normal file
@@ -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 + " 字节");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,7 +3,7 @@
|
|||||||
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
|
||||||
outputs = { self, nixpkgs }:
|
outputs = { self, nixpkgs }:
|
||||||
let
|
let
|
||||||
supportedSystems = [ "x86_64-linux" "aarch64-linux" ];
|
supportedSystems = [ "x86_64-linux" ];
|
||||||
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
|
forEachSupportedSystem = f: nixpkgs.lib.genAttrs supportedSystems (system: f {
|
||||||
pkgs = import nixpkgs { inherit self system; };
|
pkgs = import nixpkgs { inherit self system; };
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user