File类的操作

 

public class TestFile {

//遍历浏览文件
@Test
public void scanFile() {
System.out.println("============");
String path = "/".trim();
File file = new File("H:\\");

File[] files = file.listFiles();
for (File current : files) {
System.out.println("--" + current.getName());
if (current.isDirectory()) {
File getFile = new File(current.getAbsoluteFile().toString());

if (getFile.listFiles() != null) {
File[] files2 = getFile.listFiles();
for (File current2 : files2) {
System.out.println("------------" + current2.getName());
}
}
}
}

}

@Test
public void testFile2() {
System.out.println("============");
String path = "d:\\".trim();
File file = new File(path);

String[] files = file.list();
for (String current : files) {
System.out.println("--" + current);

}

}

@Test
public void testFile3() {
System.out.println("============");
String path = "H:\\".trim();
File file = new File(path);

File[] files = file.listFiles();
for (File current : files) {
System.out.println("--" + current.getAbsolutePath().toString());
System.out.println("--" + current.getParent());

}

}



//创建文件

@Test
public void makeFile() {
System.out.println("============");
String path = "H:\\abc.txt".trim();

File file = new File(path);
System.out.println(file.getAbsolutePath());

if (!file.exists()) {
boolean result = file.mkdirs();
if (result) {

System.out.println("文件创建成功");
} else {

System.out.println("文件创建失败");
}

} else {
System.out.println("文件已经存在");
}

}


//写入文件
@Test
public void writeFile() {
try {
FileWriter fileWriter = new FileWriter("h:\\ceshi.txt".trim(), true);
int[] a = new int[] { 111111, 222222 };
for (int i = 0; i < a.length; i++) {
fileWriter.write(String.valueOf(a[i]) + "");
}
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值