新增/未测试 文件操作

This commit is contained in:
2026-08-12 03:00:02 +08:00
parent 1729ea3319
commit c38b8857d5
5 changed files with 127 additions and 2 deletions

View File

@@ -6,7 +6,7 @@
<groupId>top.gtb520.java.gnu</groupId>
<artifactId>gnu_java_utils_library</artifactId>
<version>1.3-SNAPSHOT</version>
<version>1.4-SNAPSHOT</version>
<properties>
<maven.compiler.source>25</maven.compiler.source>

View File

@@ -2,6 +2,12 @@ package top.gtb520.java.gnu.java.utils.library;
import top.gtb520.java.gnu.java.utils.library.utils.print.ColorNameEnum;
import java.io.File;
import java.nio.file.Path;
import static top.gtb520.java.gnu.java.utils.library.utils.file.CreateFile.BinFileType;
public interface main {
// 压缩包操作相关接口与方法
interface compress {
@@ -127,4 +133,19 @@ public interface main {
}
}
}
// 文件操作接口与方法
interface file {
interface CreateFile {
// 创建文件创建空白文件异步IO
static File NewFile(String FileName, boolean isBin, BinFileType binFileType) {
return top.gtb520.java.gnu.java.utils.library.utils.file.CreateFile.NewFile(FileName, isBin, binFileType);
}
// 以字节流方式保存文件异步IO
static boolean StreamSaveFile(File file, Path TargePath, String FileName) {
return top.gtb520.java.gnu.java.utils.library.utils.file.CreateFile.StreamSaveFile(file, TargePath, FileName);
}
}
}
}

View File

@@ -0,0 +1,89 @@
package top.gtb520.java.gnu.java.utils.library.utils.file;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.concurrent.CompletableFuture;
// 创建文件
public class CreateFile {
/***
* 创建文件,创建空白文件
* isBin为True时创建一个空白的二进制文件否则创建一个空白的普通文件
* 异步IO操作
*
* @param FileName 文件名
* @param isBin 是否二进制文件
* @param binFileType 二进制文件类型如果是普通文件则不需要设置次值将其设置为null即可
* 参考isBin参数isBin参数为False则此参数设置为null
*
* @return 文件
*/
public static File NewFile(String FileName, boolean isBin, BinFileType binFileType) {
File TargetFile = null;
byte[] BufferByte = new byte[1024];
try {
TargetFile = new File(FileName);
if (TargetFile.createNewFile()) {
if (isBin && binFileType != null) {
// 根据二进制文件类型写入文件头标识
byte[] header = switch (binFileType) {
case ELF -> new byte[]{0x7F, 'E', 'L', 'F'};
case PE -> new byte[]{'M', 'Z'};
};
try (OutputStream os = Files.newOutputStream(TargetFile.toPath(),
StandardOpenOption.WRITE, StandardOpenOption.TRUNCATE_EXISTING)) {
os.write(header);
os.flush();
}
}
}
} catch (IOException e) {
TargetFile = null;
}
return TargetFile;
}
/***
* 以字节流方式保存文件
* 异步IO操作
*
* @param file 文件
* @param TargePath 目标路径
* @param FileName 文件名
*
* @return 是否保存成功True为保存成功False为保存失败
*/
public static boolean StreamSaveFile(File file, Path TargePath, String FileName) {
try {
Path targetFile = TargePath.resolve(FileName);
return CompletableFuture.supplyAsync(() -> {
try (OutputStream os = Files.newOutputStream(targetFile,
StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING)) {
byte[] buffer = new byte[1024];
int bytesRead;
try (var is = Files.newInputStream(file.toPath())) {
while ((bytesRead = is.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
}
os.flush();
return true;
} catch (IOException e) {
return false;
}
}).get();
} catch (Exception e) {
return false;
}
}
public enum BinFileType {
ELF, PE;
}
}

View File

@@ -0,0 +1,15 @@
package top.gtb520.java.gnu.java.utils.library.utils.file;
import java.io.File;
public class Encryption {
/***
* 文件加密
*
* @param file 文件
* @param type 加密类型
*/
public static void EncryptionFile(File file, String type) {
}
}

View File

@@ -2,7 +2,7 @@ package top.gtb520.java.gnu.java.utils.library;
public class version {
// 项目的版本信息,不代表实时运行环境
public static final String VERSION = "1.3";
public static final String VERSION = "1.4";
public static final String JAVA_VERSION = "25";
public static final String OpenJDK_VERSION = "25.0.0.0.1";
public static final String OpenJDK_Runtime_Environment = "25.0.0.0.1+36-GA";