1. public static void main(String[] args) {
  2. // TODO Auto-generated method stub
  3. //"G:\\JAVA\\test\\test.txt"
  4. createNewFile();
  5. showSeparator();
  6. useSepartor();
  7. mkNewDir();
  8. showAllNoSub();
  9. showAllWithSub(new File("G:"+File.separator+"JAVA"+File.separator+"test"+File.separator));
  10.  
  11. }
  12.  
  13. /*
  14. * 1.创建一个新文件
  15. */
  16. public static void createNewFile() {
  17. File file = new File("G:\\JAVA\\test\\test.txt");
  18. try {
  19. file.createNewFile();
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23.  
  24. }
  25.  
  26. /*
  27. * 2.file类的常量separator,patSeparator。文件分割符 //对于不同的系统分隔符孚不一样
  28. */
  29. public static void showSeparator() {
  30. System.out.println(File.separator); // \
  31. System.out.println(File.pathSeparator); // ;
  32. }
  33.  
  34. /*
  35. * 3.删除文件,用separator让程序更健壮
  36. */
  37. public static void useSepartor() {
  38. String fileName = "G:" + File.separator + "JAVA" + File.separator
  39. + "test" + File.separator + "test.txt";
  40. File file = new File(fileName);
  41. if (file.exists())
  42. file.delete();
  43. else
  44. System.out.println("文件不存在");
  45.  
  46. }
  47.  
  48. /*
  49. * 4.创建一个文件夹
  50. */
  51. public static void mkNewDir()
  52. {
  53. String filePath= "G:" + File.separator + "JAVA" + File.separator
  54. + "test" + File.separator + "newDir";
  55. File file=new File(filePath);
  56. if(file.isDirectory())
  57. System.out.println("dir exit");
  58. else
  59. System.out.println(file.mkdir());
  60. }
  61.  
  62. /*
  63. *5. 列出所在目录的所有文件包括隐藏文件(但是不显示子文件)
  64. */
  65. public static void showAllNoSub()
  66. {
  67. String filePath="G:"+File.separator+"JAVA"+File.separator+"test";
  68. File file=new File(filePath);
  69. String [] all=file.list();
  70. for(String s:all)
  71. {
  72. System.out.println(s);
  73. }
  74. }
  75.  
  76. /*
  77. *6. 用递归算法,列出所在目录的所有文件包括隐藏文件,包括子文件
  78. */
  79. public static void showAllWithSub(File file)
  80. {
  81. if(file.isFile()) //判断是文件
  82. System.out.println(" File:"+file.getPath());
  83. else if(file.isDirectory()) //判断是文件夹
  84. {
  85. System.out.println("Dir:"+file.getPath());
  86. File [] allfile=file.listFiles();
  87. for(File f : allfile)
  88. {
  89. showAllWithSub(f);
  90. }
  91.  
  92. }
  93.  
  94. }

java IO之File基本操作的更多相关文章

  1. [Storm] java.io.FileNotFoundException: File '../stormconf.ser' does not exist

    This bug will kill supervisors Affects Version/s: 0.9.2-incubating, 0.9.3, 0.9.4 Fix Version/s: 0.10 ...

  2. 运行基准测试hadoop集群中的问题:org.apache.hadoop.ipc.RemoteException: java.io.IOException: File /benchmarks/TestDFSIO/io_data/test_

    在master(即:host2)中执行 hadoop jar hadoop-test-1.1.2.jar DFSCIOTest -write -nrFiles 12 -fileSize 10240 - ...

  3. Spark启动报错|java.io.FileNotFoundException: File does not exist: hdfs://hadoop101:9000/directory

    at org.apache.spark.deploy.history.FsHistoryProvider.<init>(FsHistoryProvider.scala:) at org.a ...

  4. com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\salary-card\target\salary-card-0.0.1-SNAPSHOT.jar!\BOOT-INF\classes!\keystore\login_id_rsa 资源未找到

    com.jcraft.jsch.JSchException: java.io.FileNotFoundException: file:\D:\development\ideaProjects\sala ...

  5. Java—IO流 File类的常用API

    File类 1.只用于表示文件(目录)的信息(名称.大小等),不能用于文件内容的访问. package cn.test; import java.io.File; import java.io.IOE ...

  6. 关于spark入门报错 java.io.FileNotFoundException: File file:/home/dummy/spark_log/file1.txt does not exist

    不想看废话的可以直接拉到最底看总结 废话开始: master: master主机存在文件,却报 执行spark-shell语句:  ./spark-shell  --master spark://ma ...

  7. java io包File类

    1.java io包File类, Java.io.File(File用于管理文件或目录: 所属套件:java.io)1)File对象,你只需在代码层次创建File对象,而不必关心计算机上真正是否存在对 ...

  8. Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist

    Sqoop 抽数报错: java.io.FileNotFoundException: File does not exist 一.错误详情 2019-10-17 20:04:49,080 INFO [ ...

  9. Diagnostics: File file:/private/tmp/spark-d4ebd819-e623-47c3-b008-2a4df8019758/__spark_libs__6824092999244734377.zip does not exist java.io.FileNotFoundException: File file:/private/tmp/spark-d4ebd819

    spark伪分布式模式 on-yarn出现一下错误 Diagnostics: File file:/private/tmp/spark-d4ebd819-e623-47c3-b008-2a4df801 ...

随机推荐

  1. 使用exe4j把java程序生成可执行的.exe文件

    exe4j可以很容易把一个jar打成exe.  下载地址:http://dl.dbank.com/c0owlopqf8 1.下载的安装文件,里面包含一个注册码生成的工具 2.安装exe4j以及破解(注 ...

  2. 013:Rank、视图、触发器、MySQL内建函数

    一. Rank 给出不同的用户的分数,然后根据分数计算排名 (gcdb@localhost) 09:34:47 [mytest]> create table t_rank(id int,scor ...

  3. sublime text安装插件

    http://www.sublimetext.com/ 安装Sublime Text 2插件的方法: 1.直接安装 安装Sublime text 2插件很方便,可以直接下载安装包解压缩到Package ...

  4. angualr 之 $$phase

    对于angular, $$phase 是 作为angular 内部状态表示位,用来标示当前是处于哪个阶段. 用有的阶段有 $digest $apply 在使用的是例如你想调用scope.$apply的 ...

  5. IDA Pro 权威指南学习笔记(十四) - 操纵函数

    IDA 无法定位一个函数调用,由于没有直接的方法到达函数,IDA 将无法识别它们 IDA 可能无法正确确定函数的结束部分,需要手动干预,以更正反汇编代码中的错误 如果编译器已经将函数分割到几个地址范围 ...

  6. 11.solr学习速成之MoreLikeThis

    Solr相似匹配    在网页搜索或电商产品搜索结果页面,很多时候会看到一个相似文档.相似产品或找相似的链接.Solr 使用 MoreLikeThisComponent(MLT)和 MoreLikeT ...

  7. Foglight 5.6.7 控制台jboss报404

    Foglight 5.6.7 控制台jboss报404 [终极问题原来就一数据库权限问题!下文是由问题源找到的类似的dell support的相关文章,但却是关于sql server error的,感 ...

  8. linux条件变量

    条件变量用于线程之间的通信,和互斥锁一起使用.条件变量用于及时通知等待的线程条件的变化,使线程不至于错过变化. 考虑下面的情况,有AB两个线程对index这个全局变量进行++,一个线程C用于判断,in ...

  9. [转] c# 操作Word

    来自 风过四季天 的原文 c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想 到网页的信 ...

  10. Asp.net 的工作原理

     转:http://www.cnblogs.com/linjiancun/archive/2010/09/14/1825662.html 1.1.1          Asp.net 的工作原理 ...