File表示存储设备上的一个文件或目录,使用方式查看API即可,比较简单

  1. package org.zln.io.file;
  2.  
  3. import java.io.File;
  4.  
  5. /**
  6. * Created by coolkid on 2015/6/21 0021.
  7. */
  8. public class TestFile {
  9. /*当前程序根路径*/
  10. public static final String rootPath = System.getProperty("user.dir");
  11. public static void main(String[] args) {
  12. //createFile1();
  13. // rename();
  14. showFileList(new File(rootPath));
  15. }
  16.  
  17. /**
  18. * 遍历文件夹
  19. * @param file 根目录
  20. */
  21. private static void showFileList(File file) {
  22. if (file.isDirectory()){
  23. System.out.println("目录:"+file.getAbsolutePath());
  24. File[] files = file.listFiles();
  25. for (File file1:files){
  26. showFileList(file1);
  27. }
  28. }else {
  29. System.out.println("文件:"+file.getAbsolutePath());
  30. }
  31. }
  32.  
  33. /**
  34. * 文件的重命名
  35. */
  36. private static void rename() {
  37. String myPath=rootPath+ File.separatorChar+"myFile";
  38. File file = new File(myPath);
  39. if (!file.exists()){
  40. createFile1();
  41. }else {
  42. File newFile = new File(rootPath+ File.separatorChar+"myFileNew");
  43. file.renameTo(newFile);
  44. System.out.println(newFile.getAbsolutePath()+"创建成功");
  45. }
  46.  
  47. }
  48.  
  49. /**
  50. * 文件的创建
  51. */
  52. private static void createFile1() {
  53. String myPath=rootPath+ File.separatorChar+"myFile";
  54. File file = new File(myPath);
  55. if (!file.exists()){
  56. System.out.println(file.getAbsolutePath()+"不存在,创建");
  57. if (file.mkdirs()){
  58. System.out.println("创建成功");
  59. }else {
  60. System.out.println("创建失败");
  61. }
  62. }
  63. }
  64. }

Java IO 之 File 的创建、重命名与遍历的更多相关文章

  1. java中实现File文件的重命名(renameTo)、将文件移动到其他目录下、文件的复制(copy)、目录和文件的组合(更加灵活方便)

    欢迎加入刚建立的社区:http://t.csdn.cn/Q52km 加入社区的好处: 1.专栏更加明确.便于学习 2.覆盖的知识点更多.便于发散学习 3.大家共同学习进步 3.不定时的发现金红包(不多 ...

  2. Java IO编程——File文件操作类

    在Java语言里面提供有对于文件操作系统操作的支持,而这个支持就在java.io.File类中进行了定义,也就是说在整个java.io包里面,File类是唯一 一个与文件本身操作(创建.删除.重命名等 ...

  3. Java实现批量修改文件名,重命名

    平时下载的文件.视频很多都会有网址前缀,比如一些编程的教学视频,被人共享出来后,所有视频都加上一串长长的网址,看到就烦,所以一般会重命名后看,舒服很多,好了,不多说,直接上代码: 以下代码演示使用递归 ...

  4. 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 ...

  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. [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 ...

  9. 运行基准测试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 - ...

随机推荐

  1. 思维导图 XMind 8 Update 8 Pro for Mac 中文破解版

    破解包下载地址 在官网下载8版本就可以了

  2. springboot properties文件中的数据通过@Value()形式注入

    首先在resources目录下新建一个properties文件,如下图 在photoPath.properties中写入内容,key=value的形式,如下图 在你需要引用properties的类头部 ...

  3. 【Linux】wget: command not found的两种解决方法

    1.rpm 安装 下载wget的RPM包: http://mirrors.163.com/centos/6.8/os/x86_64/Packages/wget-1.12-8.el6.x86_64.rp ...

  4. jQuery 使用问题

    attr('checked', 'checked')调用多次仅第一次生效 使用attr()获取这些属性的返回值为String类型,如果被选中(或禁用)就返回checked.selected或disab ...

  5. angularjs 自定义服务(serive,factory,provder) 以及三者的区别

    1.Serive 服务:通过service方式创建自定义服务,相当于new的一个对象:var s = new myService();,只要把属性和方法添加到this上才可以在controller里调 ...

  6. php使用file_get_contents 或者curl 发送get/post 请求 的方法总结

    file_get_contents模拟GET/POST请求 模拟GET请求: <?php $data = array( 'name'=>'zhezhao', 'age'=>'23' ...

  7. JS 定时器,定时调用PHP

    $(function() { var voiceplay=function(){ var site = location.href.split('_cms')[0] + '_cms/'; $.ajax ...

  8. Hue联合(hdfs yarn hive) 后续......................

    1.启动hdfs,yarn start-all.sh 2.启动hive $ bin/hive $ bin/hive --service metastore & $ bin/hive --ser ...

  9. Apache Struts最新漏洞 远程代码执行漏洞预警 2018年11月08日

    2018年11月8日,SINE安全监控检测中心,检测到Apache Struts官方更新了一个Struts漏洞补丁,这个漏洞是Apache Struts目前最新的漏洞,影响范围较广,低于Apache ...

  10. Leecode刷题之旅-C语言/python-111二叉树的最小深度

    /* * @lc app=leetcode.cn id=111 lang=c * * [111] 二叉树的最小深度 * * https://leetcode-cn.com/problems/minim ...