parent
b645ab6ea3
commit
786047512d
@ -1,93 +0,0 @@
|
||||
import java.io.IOException;
|
||||
import java.nio.file.FileAlreadyExistsException;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class filemanage {
|
||||
ArrayList<String> array = new ArrayList<>();
|
||||
|
||||
public static void main(String[] args) {
|
||||
filemanage f1 = new filemanage();
|
||||
f1.filecreate("张三1", "123");
|
||||
f1.fileread("张三1");
|
||||
for (String i : f1.array) {
|
||||
System.out.println(i);
|
||||
}
|
||||
f1.filewrite("张三1", "123", "1");
|
||||
}
|
||||
|
||||
public static List<String> getFileNames(String str) throws IOException {
|
||||
Path dirPath = Path.of("User-problem");
|
||||
Path filePathf = dirPath.resolve(str);
|
||||
try (var files = Files.list(filePathf)) {
|
||||
return files.filter(Files::isRegularFile)
|
||||
.map(path -> path.getFileName().toString()) // 提取文件名
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
||||
void fileread(String str) {
|
||||
String readfilef = str;
|
||||
Path dirPath = Path.of("User-problem");
|
||||
Path filePathf = dirPath.resolve(readfilef);
|
||||
try {
|
||||
getFileNames(str);
|
||||
for (String i : getFileNames(str)) {
|
||||
String readfile = i;
|
||||
Path filePath = filePathf.resolve(readfile);
|
||||
try {
|
||||
List<String> fileLines = Files.readAllLines(filePath);
|
||||
for (String j : fileLines) {
|
||||
array.add(j);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void filewrite(String str, String str1, String str2) {
|
||||
List<String> newLines = new ArrayList<>();
|
||||
newLines.add(str2);
|
||||
String writefilef = str;
|
||||
String writefile = str1 + ".txt";
|
||||
Path dirPath = Path.of("User-problem");
|
||||
Path filePathf = dirPath.resolve(writefilef);
|
||||
Path filePath = filePathf.resolve(writefile);
|
||||
try {
|
||||
Files.write(filePath, newLines,
|
||||
StandardOpenOption.APPEND); // 追加模式
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
void filecreate(String str, String str1) {
|
||||
String createfilef = str;
|
||||
String createfile = str1 + ".txt";
|
||||
try {
|
||||
Path dirPath = Paths.get("User-problem");
|
||||
if (!Files.exists(dirPath)) {
|
||||
Files.createDirectories(dirPath);
|
||||
}
|
||||
|
||||
// 在目录中创建文件
|
||||
Path filePathf = dirPath.resolve(createfilef);
|
||||
Path filePath = filePathf.resolve(createfile);
|
||||
Files.createFile(filePath);
|
||||
|
||||
} catch (FileAlreadyExistsException e) {
|
||||
//System.out.println("文件已存在!");
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue