笔记:java File基本操作
/**
* 创建一个文件,目录或多级目录
* @param path
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static void createFile(String path) throws IOException
{
if(path == null){
return ;
}
File file = new File(path);
if(path == null){
return ;
}
File f = new File(path);
File fileParent = f.getParentFile();
if(!fileParent.exists()){
fileParent.mkdirs();
}
if(!f.exists()){
f.createNewFile();
}
// if(!file.exists()){
// file.createNewFile();
// /*//创建一个目录,其路径中的父目录都必须存在(D://test)
// if(!file.isDirectory()){
// file.mkdir();
// }
// //创建一个多级目录,其路径中的父目录可以不存在存在(D://test//test//test)
// if(!file.isDirectory()){
// file.mkdirs();
// }*/
// //只允许所有人使用可读
// file.setReadable(true);
// //只允许所有人使用可写
// file.setWritable(true);
// /* //只允许文件所有者使用可写
// file.setWritable(true, true);
// //只允许文件所有者使用可读
// file.setReadable(true, true);*/
// }
}
/**
* 查看该文件的基本信息
* @param path
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static void getFileInfo(String path) throws IOException
{
File file = new File(path);
//文件的路径
String filePath = file.getPath();
//文件的名称
String fileName = file.getName();
//文件父路径
String fileParentName = file.getParent();
//文件的绝对路径
String fileAbsolutePath = file.getAbsolutePath();
//文件的绝对路径
String fileCanonicalPath = file.getCanonicalPath();
//父目录的文件对象
File fileParentFile = file.getParentFile();
//父目录的文件对象
File fileAbsoluteFile = file.getAbsoluteFile();
//父目录的文件对象
File fileCanonicalFile = file.getCanonicalFile();
//文件的可使用的空间
long fileFreeSpace = file.getFreeSpace();
//文件的总共的空间
long fileTotalSpace = file.getTotalSpace();
//文件的未使用的空间
long fileUsableSpace = file.getUsableSpace();
System.out.println("filePath:"+filePath);
System.out.println("fileName:"+fileName);
System.out.println("fileParentName:"+fileParentName);
System.out.println("fileAbsolutePath:"+fileAbsolutePath);
System.out.println("fileCanonicalPath:"+fileCanonicalPath);
System.out.println("fileParentFile:"+fileParentFile);
System.out.println("fileAbsoluteFile:"+fileAbsoluteFile);
System.out.println("fileCanonicalFile:"+fileCanonicalFile);
System.out.println("fileFreeSpace:"+fileFreeSpace);
System.out.println("fileTotalSpace:"+fileTotalSpace);
System.out.println("fileUsableSpace:"+fileUsableSpace);
/*
结果集:
filePath:D:\powerd\core.chm
fileName:core.chm
fileParentName:D:\powerd
fileAbsolutePath:D:\powerd\core.chm
fileCanonicalPath:D:\powerd\core.chm
fileParentFile:D:\powerd
fileAbsoluteFile:D:\powerd\core.chm
fileCanonicalFile:D:\powerd\core.chm
fileFreeSpace:95729790976
fileTotalSpace:158986403840
fileUsableSpace:95729790976
*/
}
/**
* 删除文件夹(先删除文件下的文件在,删除目录)
* @param args
* @see [类、类#方法、类#成员]
*/
public static void deleteFile(String path)
{
File file = new File(path);
if(file.exists()){
if(file.isFile()){
System.out.println(file.getName()+"文件是否是隐藏的:"+file.isHidden());
file.delete();
/* //其中检查文件路径是否合法。不合法时,返回false。合法,删除文件,返回true
file.deleteOnExit();*/
}else if(file.isDirectory()){
File[] files = file.listFiles();
for(File f:files){
deleteFile(f.getAbsolutePath());
}
}
}
file.delete();
}
/**
* 重命名
* @param oldPath
* @param newPath
* @see [类、类#方法、类#成员]
*/
public static void rename(String oldPath,String newPath)
{
File oldFile = new File(oldPath);
File newFile = new File(newPath);
if(oldFile.exists() && !newFile.exists()){
if(!newFile.exists()){
new File(newFile.getParent()).mkdirs();
}
oldFile.renameTo(newFile);
}
}
/**
* 复制文件
* @param oldPath
* @param newPath
* @throws IOException
* @see [类、类#方法、类#成员]
*/
public static void copyFlie(String oldPath,String newPath) throws IOException
{
File oldFile=new File(oldPath);
newPath +="\\"+oldFile.getName();
File newFile=new File(newPath);
if(!newFile.exists()){
newFile.mkdirs();
}
System.out.println(newFile);
File[] file=oldFile.listFiles();
for (File f : file) {
if(f.isFile()){
String path2=newPath+"\\"+f.getName();
createFile(path2);
}else if(f.isDirectory()){
String s=f.getPath();
copyFlie(s,newPath);
}
}
}
/**
* 是否有文件的权限
* @param path
* @see [类、类#方法、类#成员]
*/
public static void isAutho(String path)
{
File f=new File(path);
if(!f.canExecute()){
f.setExecutable(true);
}
if(!f.canRead()){
f.setReadable(true);
}
if(!f.canWrite()){
f.setWritable(true);
}
/**
* 重命名,去除文件名结尾的后缀
* @param oldPath
* @param newPath
* @see [类、类#方法、类#成员]
*/
public static void renameDir(String path,String suffix)
{
File file = new File(path);
if(file.exists()){
if(file.isFile()){
if(file.getName().endsWith(suffix)){
String paths = file.getAbsolutePath();
String rePath =paths.substring(0, paths.length()-suffix.length());
rename(paths,rePath);
}
}else if(file.isDirectory()){
File[] files = file.listFiles();
for(File f:files){
renameDir(f.getAbsolutePath(),suffix);
}
}
}
}