public class FileKnow
{
public static void main(String[] args)
{
//构建file对象 ,参数表示文件所在的路径
File file = new File("d:\\niit.log"); //判断文件是否存在
System.out.println(file.exists());
//判断文件是否是单个的文件
System.out.println(file.isFile());
//判断文件是否是文件夹
System.out.println(file.isDirectory());
//获取文件的绝对路径
System.out.println(file.getAbsolutePath());
//获取文件名
System.out.println(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\\")+1,file.getAbsolutePath().lastIndexOf(".")));
//获取文件完整的名称包括后缀名
System.out.println(file.getName());
//获取文件所在的相对路径
System.out.println(file.getParent());
//获取文件所在盘符的大小空间
System.out.println(file.getTotalSpace());
//获取文件所在盘符的可用空间
System.out.println(file.getFreeSpace());
System.out.println(file.getUsableSpace());
//获取文件自身的字节数
System.out.println(file.length());
//是否隐藏文件
System.out.println(file.isHidden());
//是否可读
System.out.println(file.canRead());
//设置文件是否可写(只读性的设置)
file.setWritable(false);
//获取文件最后次修改的时间
System.out.println(file.lastModified());
System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(file.lastModified())));
// FileKnow fileKnow = new FileKnow();
// fileKnow.createFile("E:/【 图 片 】", "weifei.jpg");
}
/*******************************************文件基本操作**********************************************/
/**
* 根据指定路径搜索显示所有的文件信息
* @param path
*/
public void showFiles(String path)
{
//通过路径构建文件
File file = new File(path);
//判断文件是否存在
if(file.exists())
{
//打印输出当前文件的路径
System.out.println(file.getAbsolutePath());
//判断是否是文件夹
if(file.isDirectory())
{
//判断文件夹中是否有文件
File[] files = file.listFiles();
if(files != null)
{
//遍历子文件
for(File childFile : files)
{
showFiles(childFile.getAbsolutePath());
}
}
}
}
else
{
System.out.println("文件不存在!");
}
}
/**
* @param args
*/
/**
* 创建文件
*/
public void createFile(String path,String fileName)
{
//创建文件对象
File file = new File(path, fileName);
//判断文件是否存在
if(file.exists())
{
System.out.println("文件已经存在");
}
else
{
//创建文件
try
{
if (file.createNewFile())
{
System.out.println("文件创建成功");
}
else {
System.out.println("文件创建失败");
}
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/**
* 创建文件夹
* @param path
*/
public void createDirectroy(String path, String diretroyName)
{
File file = new File(path, diretroyName);
//判断是否存在
if(!file.exists())
{
//创建文件夹
file.mkdir();
}
else
{
System.out.println("文件夹已经存在");
}
}
/**
* 拷贝文件
* @param sourcePath 复制文件的路径 如:D:/
* @param fileName 文件名 如:back.jpg
* @param newPath 复制到的位置 如:E:/
*/
public void copyFile(String sourcePath, String fileName, String newPath)
{
//创建复制的文件
File sourceFile = new File(sourcePath,fileName);
//判断文件是否存在
if(sourceFile.exists())
{
File newFile = new File(newPath,fileName);
//如果文件存在,判断文件的类型
if(sourceFile.isFile())
{
//如果是单个的文件,使用流读写文件
try
{
//构建输入流读取复制的文件
BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile),1024*1024*10);
//构建输出流写出文件
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(newFile),1024*1024*10);
int data;
while((data = input.read()) != -1)
{
output.write(data);
}
//关闭流
output.flush();
output.close();
input.close(); } catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
{
//如果是文件夹,新建文件夹
newFile.mkdir();
//判断文件夹中是否还有子文件
File[] files = sourceFile.listFiles();
//遍历子文件
for(File childFile : files)
{
//递归复制子文件
copyFile(childFile.getParent(), childFile.getName(), newFile.getAbsolutePath());
}
}
}
}
/**
* 剪切文件
* @param sourcePath
* @param fileName
* @param newPath
*/
public void cutFile(String sourcePath, String fileName, String newPath)
{
//
}
/**
* 重命名
* @param path
* @param fileName
* @param newName
*/
public void renameFile(String path, String fileName, String newName)
{
//创建源文件
File oldFile = new File(path, fileName);
//判断源文件是否存在
if(oldFile.exists())
{
//创建新文件
File newFile = new File(path, newName);
//判断新文件是否存在
if(!newFile.exists())
{
//重命名
oldFile.renameTo(newFile);
}
else
{
System.out.println("文件名已存在");
} }
else
{
System.out.println("文件不存在");
} }
/**
* 删除文件
* @param path
* @param fileName
*/
public void deleteFile(String path, String fileName)
{
//获取要删除的文件对象
File file = new File(path,fileName);
//判断文件是否存在
if(file.exists())
{
//如果存在,判断该文件是文件还是文件夹
if(file.isDirectory())
{
//如果是文件夹,获取该文件夹的子文件
File[] files = file.listFiles();
//递归删除子文件
for(File childFile : files)
{
deleteFile(childFile.getParent(), childFile.getName());
}
}
//删除整个文件
file.delete();
}
}
}

最新文章

  1. xamarin 手机顶部状态栏
  2. linux--------------今天又遇到一个奇葩的问题,就是linux文件的权限已经是777了但是还是没有写入权限,按照下面的命令就解决了
  3. 物理Data Guard的日常维护
  4. 传入的表格格式数据流(TDS)远程过程调用(RPC)协议流不正确。此 RPC 请求中提供了过多的参数。最多应为 2100
  5. python day5--正则表达式
  6. Shell教程3-Shell特殊变量
  7. linux解压cpio.gz类型文件
  8. ASP.NET Web API教程(六) 安全与身份认证
  9. 解决android自带textview右侧文字不能对齐的问题
  10. tomcat 启动超时
  11. Sampling Distributions and Central Limit Theorem in R(转)
  12. php+中文分词scws+sphinx+mysql打造千万级数据全文搜索
  13. SQL 游标的写法
  14. [Abp vNext 源码分析] - 1. 框架启动流程分析
  15. docker命令
  16. grafana安装使用及与zabbix集成
  17. http://ctf.bugku.com/challenges#%E9%80%86%E5%90%91%E5%85%A5%E9%97%A8:bugku--逆向入门
  18. 洛谷 P2466 Sue的小球 解题报告
  19. oracle故障解决
  20. Scala入门系列(六):面向对象之object

热门文章

  1. spring mvc框架 遇到的问题
  2. 浅谈Spring(四)
  3. install tool
  4. JSP page include taglib
  5. leetcode 31. Next Permutation(字典序的下一个)
  6. R与数据分析旧笔记(⑨)广义线性回归模型
  7. html回车事件
  8. 去掉iphone 的圆角样式
  9. 在CentOS 7 / Gnome 3 双屏时设置主屏
  10. 阅读 - Code Complete 2 - 第33章 - 个人性格