package com.cdgq.util;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.junit.Test;
import com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class FileTest {
/***
* 计算所在目录所有文件的总和
* */
@Test
public void test06() {
File file=new File("E:/file");
stes(file);
}
public long stes(File str) {
if(str.isDirectory()) {
long sums = 0;
File[] llString=str.listFiles();
for (File file : llString) {
sums+=stes(file);
}
return sums;
}else {
return str.length();
}
}
//@Test
public void test05() {
File file=new File("E:/file/cdgq/cg");
File file2=new File("E:/file/cdgq/cggq");
file.renameTo(file2);
}
//@Test
public void test04() throws IOException{
File file=new File("E:/file/cdgq/cg/com");
System.out.println("文件夹存在..... 及删除");
file.delete();//可以删除文件和空目录
}
//@Test
public void test03() throws IOException{
File file=new File("e:/file/cdgq/chengdu");
if(!file.exists()) {
System.out.println("文件夹不存在.....");
file.mkdirs();//创建目录
}
}
//@Test
public void test02() throws IOException{
File file=new File("../com/OS.docx");
System.out.println("文件的路径"+file.getPath());//相对当前项目
System.out.println("文件的绝对路径"+file.getAbsolutePath());//非规范路径
System.out.println("文件的规范路径"+file.getCanonicalPath());
}
//@Test
public void test01() {
/**
* 路径分隔符:windows:\和/
* 其他平台:/
*
*
* */
File file=new File("E:/file/java/com/OS.docx");
System.out.println("文件名称:"+file.getName());
System.out.println("文件大小:"+file.length()+"字节");
System.out.println("文件是否是隐藏文件:"+file.isHidden());
System.out.println("文件是否存在:"+file.exists());
System.out.println("文件是否可读:"+file.canRead());
System.out.println("文件是否可写:"+file.canWrite());
String parentString=file.getParent();//返回父目录的名称
File dirFile=file.getParentFile();//返回父目录的File对象
System.out.println("文件的父目录:"+parentString);
SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yy-MM-dd HH:mm");
String date=simpleDateFormat.format(new Date(file.lastModified()));
System.out.println("文件最后修改时间:"+date+"毫秒");
}
}