当前位置: 首页 > news >正文

做网站价格表贵州贵州省住房和城乡建设厅网站

做网站价格表,贵州贵州省住房和城乡建设厅网站,网站后期维护包括,外贸网站建设平台哪个好补充一些用到前面没提到的方法 isDirectory()方法,检查一个对象是否是文件夹,是true不是false isFile()方法,检测一个对象是否为文件,是true不是false 文件的读写操作实践 上一篇大致讲了文件读写操作的基本操作,下面是实践时…

补充一些用到前面没提到的方法

isDirectory()方法,检查一个对象是否是文件夹,是true不是false

isFile()方法,检测一个对象是否为文件,是true不是false

文件的读写操作实践

上一篇大致讲了文件读写操作的基本操作,下面是实践时间

查找文件并删除

public class Demo {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入查找的目录:");String rootDir = scanner.next();File rootFile = new File(rootDir);if(!rootFile.isDirectory()){System.out.println("不是目录");}System.out.println("请输入要删除的关键字:");String keyword = scanner.next();scanDir(rootFile,keyword);}
//遍历目录private static void scanDir(File rootFile, String keyword) {File[] files = rootFile.listFiles();//列出当前目录列表if(files == null){return;//目录为空}for (File file:files){//对比,判断是文件还是目录System.out.println("遍历目录文件"+ file.getAbsolutePath());if (file.isFile()){dealFile(file,keyword);//是文件,判断文件名是否包含删除的关键字}else{scanDir(file,keyword);//目录,则继续递归}}}private static void dealFile(File file, String keyword) {if(file.getName().contains(keyword)){//contains会比对传入file与keyword是否有对部分System.out.println("发现文件:"+ file.getAbsolutePath());//绝对路径System.out.println("是否删除:y/n");Scanner x = new Scanner(System.in);String y = x.next();if (y.equalsIgnoreCase("y")){file.delete();System.out.println("删除成功");}else{System.out.println("没有删除");}}}
}

将文件并且复制到其他目录

public class Demo13 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入源文件路径:");String srcPath = scanner.next();System.out.println("请输入目标文件路径:");String destPath = scanner.next();File srcfile = new File(srcPath);if(!srcfile.isFile()){//判断是否为文件System.out.println("不是源文件或不存在");}File destFil = new File(destPath);if(!destFil.getParentFile().isDirectory()){//判断目标文件目录存不存在System.out.println("目标文件目录不存在");return;}//开始复制文件try(InputStream inputStream = new FileInputStream(srcfile) ;OutputStream outputStream = new FileOutputStream(destFil)){//前面讲要关闭文件我却没有用,这里解释一下是因为try()方法自带了关闭文件功能while(true){byte[] buf = new byte[1024];int n = inputStream.read(buf);if(n == -1){break;}outputStream.write(buf, 0 ,n);}System.out.println("复制成功");} catch (IOException e) {throw new RuntimeException();}}
}

 扫描目录,找到名称或者包含指定字符的所有普通文件(不包含目录)

 

public class Demo14 {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);System.out.println("请输入要搜索的路径");String rootPath = scanner.next();File rootfile = new File(rootPath);if (!rootfile.isDirectory()){System.out.println("不是路径");return;}System.out.println("输入搜索关键字");String keyword = scanner.next();scanDir(rootfile,keyword);//扫描}private static void scanDir(File rootfile, String keyword) {File[] files = rootfile.listFiles();//获取当前目录下所有的内容if (files ==null){return;}for (File file :  files){if(file.isFile()){//文件deaFile(file,keyword);}else {//目录scanDir(file,keyword);//递归}}}private static void deaFile(File file, String keyword) {if (file.getName().contains(keyword)){System.out.println("文件名包含关键字" + file.getAbsolutePath());return;}StringBuilder stringBuilder = new StringBuilder();try(Reader reader = new FileReader(file)) {//keyword是字符串,使用字符流while(true){char[] chars = new char[1024];int n = reader.read(chars);if (n ==-1){break;}stringBuilder.append(chars,0,n);}}catch(IOException e){throw new RuntimeException(e);}if(stringBuilder.indexOf(keyword)>= 0){//找到匹配项System.out.println("文件内容包含关键字" + file.getAbsolutePath());return;}}

http://www.dtcms.com/a/485719.html

相关文章:

  • 西安网站建设培训市场推广
  • 温州做网站的如何查询网站的空间大小
  • 南通的电商网站建设网站开发方倍工作室
  • 常州网站建设专业的公司做视频网站需要多大带宽
  • 给设计网站做图小程序注册步骤
  • 南京网站开发培训旅游网站的建设方案
  • 邵阳做网站哪个公司好福田官网
  • 沈阳门户网站建设wordpress 图片路径加密
  • html个人网站模版一个高校的校园网站建设费用
  • 健身器材网站模板营销型网站建设宣传语
  • 广东省住建厅官方网站鞍山信息港二手房
  • 网站建立具体步骤是最早做团购的网站
  • 四川润邦建设工程设计有限公司网站怀化市优化办
  • 企业网站建设软件需求分析深圳快速seo排名优化
  • 济宁做网站多少钱网站制作是不是要先用ps做
  • 做视频网站审核编辑有假么网站备案名可以更改吗
  • 百度快照官网登录寿光网站优化
  • 阜宁做网站哪家公司好怎么样免费创建网站
  • 山东住房和城乡建设部网站四川省工程造价总站官网
  • 建筑类专业做教育的网站专业网站建设网站推广
  • 制作微信网站模板下载不了成都网站建设收费
  • 柳州专业网站优化股市行情app
  • 长沙建个网站要多少钱拉丝机东莞网站建设
  • c 做网站需要什么知识三大电商平台
  • 做旅游网站的目的网站建设 猴王网络0成本
  • 做水暖的网站整站seo排名费用价格
  • 网上商城网站开发报告电商网页设计理念
  • 装修公司 网站模板dede本地搭建好网站后台密码和用户名是什么
  • 黑龙江建设局网站怎样做当当网站代理
  • 网站icp备案申请网站建立健全举报工作机制