Java基础,输入输出
发布日期:2025-04-03 01:38:59 浏览次数:10 分类:精选文章

本文共 5288 字,大约阅读时间需要 17 分钟。

Java文件操作Test系列

Test02:目录操作测试

package hanqi.test;
import java.io.File;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
File dir = new File("d:/test/newdir"); // 创建目录对象
if (dir.exists()) {
System.out.println("目录存在");
} else {
System.out.println("目录不存在");
dir.mkdirs(); // 创建新目录,自动处理父目录
System.out.println("创建目录成功");
}
String dir1 = "d:/test01"; // 存储目录路径
String filename = "test01.txt"; // 文件名
File d1 = new File(dir1); // 创建目录文件对象
if (!d1.exists()) {
d1.mkdirs(); // 确保目录存在
}
File file = new File(d1, filename); // 组合成文件对象
try {
file.createNewFile(); // 创建空文件
} catch (IOException e) {
e.printStackTrace();
}
}
}

Test07:文件存在与否及重命名操作

package hanqi.test;
import java.io.File;
import java.io.IOException;
public class Test07 {
public static void main(String[] args) throws IOException {
File file = new File("d:/test.txt"); // 创建文件对象
if (file.exists()) {
System.out.println(file.getName() + "存在");
file.delete(); // 删除存在的文件
System.out.println(file.getName() + "已删除");
// 文件重命名
file.renameTo(new File("d:/test2.txt")); // 移动文件
System.out.println(file.getName() + "文件移名成功");
} else {
System.out.println(file.getName() + "不存在");
try {
file.createNewFile(); // 创建新文件
System.out.println("创建" + file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

Test03:字节流输入输出操作

package hanqi.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class Test03 {
public static void main(String[] args) throws Exception {
try {
// 字节输出流,写入文件
File file = new File("d:/test"); // 目标目录
if (!file.exists()) {
file.mkdirs(); // 创建不存在的目录
}
File targetFile = new File("d:/test03.txt"); // 目标文件
FileOutputStream out = new FileOutputStream(targetFile);
// 写入内容
String content = "今天是星期一";
byte[] bytes = content.getBytes();
out.write(bytes);
out.close(); // 释放资源
// 字节输入流,读取文件
FileInputStream in = new FileInputStream("d:/test/test03.txt");
byte[] buffer = new byte[1024];
int readBytes = in.read(buffer); // 读取数据
StringBuilder sb = new StringBuilder();
while (readBytes > 0) {
sb.append(new String(buffer, 0, readBytes));
readBytes = in.read(buffer); // 继续读取
}
System.out.println(sb.toString());
// 读取最后一次写入的内容
byte[] lastBytes = new byte[1024];
int lastRead = in.read(lastBytes);
System.out.println((lastRead > 0 ? new String(lastBytes, 0, lastRead) : ""));
} catch (Exception e) {
e.printStackTrace();
}
}
}

Test04:文件追加与覆盖操作

package hanqi.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test04 {
public static void main(String[] args) throws IOException {
try {
// 另一个方法实现追加与覆盖
// 第一步:读取原文件内容
File source = new File("d:/test/test03.txt");
FileInputStream in = new FileInputStream(source);
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int readBytes = -1;
while ((readBytes = in.read(buffer)) > 0) {
sb.append(new String(buffer, 0, readBytes));
}
// 第二步:写入新内容到文件中
String appendContent = "\n这是追加的内容"; // 追加的内容
FileOutputStream out = new FileOutputStream(source);
out.write(appendContent.getBytes());
out.close();
// 如果需要覆盖,可以直接写入新的内容
// out.write(sb.toString().getBytes());
System.out.println("操作完成");
} catch (Exception e) {
e.printStackTrace();
}
}
}

Test05:使用新构造方法进行文件操作

package hanqi.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test05 {
public static void main(String[] args) throws IOException {
// 使用新的transit方法进行操作
FileOutputStream out = new FileOutputStream("d:/test/test03.txt", true); //追加模式
String content = "\r\n新的构造方法";
out.write(content.getBytes());
out.close();
// 阅读文件
FileInputStream in = new FileInputStream("d:/test/test03.txt");
byte[] buffer = new byte[1024];
StringBuilder sb = new StringBuilder();
int readBytes = -1;
while ((readBytes = in.read(buffer)) > 0) {
sb.append(new String(buffer, 0, readBytes));
}
System.out.println(sb.toString());
}
}

以上是对各Test类的完整实现及示例代码,涵盖了文件和目录的基本操作,从创建、删除、重命名,到字节流的读写,包括追加和覆盖等功能。

上一篇:Kubernetes 访问集群 API 的方法
下一篇:Kubernetes 设计理念

发表评论

最新留言

能坚持,总会有不一样的收获!
[***.219.124.196]2025年04月25日 09时02分25秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章