• JAVA FILE or I/O学习


      1 public class FileKnow
      2 {
      3     public static void main(String[] args)
      4     {
      5         //构建file对象 ,参数表示文件所在的路径
      6         File file = new File("d:\niit.log");
      7         
      8         //判断文件是否存在
      9         System.out.println(file.exists());
     10         //判断文件是否是单个的文件
     11         System.out.println(file.isFile());
     12         //判断文件是否是文件夹
     13         System.out.println(file.isDirectory());
     14         //获取文件的绝对路径
     15         System.out.println(file.getAbsolutePath());
     16         //获取文件名
     17         System.out.println(file.getAbsolutePath().substring(file.getAbsolutePath().lastIndexOf("\")+1,file.getAbsolutePath().lastIndexOf(".")));
     18         //获取文件完整的名称包括后缀名
     19         System.out.println(file.getName());
     20         //获取文件所在的相对路径
     21         System.out.println(file.getParent());
     22         //获取文件所在盘符的大小空间
     23         System.out.println(file.getTotalSpace());
     24         //获取文件所在盘符的可用空间
     25         System.out.println(file.getFreeSpace());
     26         System.out.println(file.getUsableSpace());
     27         //获取文件自身的字节数
     28         System.out.println(file.length());
     29         //是否隐藏文件
     30         System.out.println(file.isHidden());
     31         //是否可读
     32         System.out.println(file.canRead());
     33         //设置文件是否可写(只读性的设置)
     34         file.setWritable(false);
     35         //获取文件最后次修改的时间
     36         System.out.println(file.lastModified());
     37         System.out.println(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date(file.lastModified())));
     38 //        FileKnow fileKnow = new FileKnow();
     39 //        fileKnow.createFile("E:/【    图 片    】", "weifei.jpg");
     40     }
     41     /*******************************************文件基本操作**********************************************/
     42     /**
     43      * 根据指定路径搜索显示所有的文件信息
     44      * @param path
     45      */
     46     public void showFiles(String path)
     47     {
     48         //通过路径构建文件
     49         File file = new File(path);
     50         //判断文件是否存在
     51         if(file.exists())
     52         {
     53             //打印输出当前文件的路径
     54             System.out.println(file.getAbsolutePath());
     55             //判断是否是文件夹
     56             if(file.isDirectory())
     57             {
     58                 //判断文件夹中是否有文件
     59                 File[] files = file.listFiles();
     60                 if(files != null)
     61                 {
     62                     //遍历子文件
     63                     for(File childFile : files)
     64                     {
     65                         showFiles(childFile.getAbsolutePath());
     66                     }
     67                 }
     68             }
     69         }
     70         else
     71         {
     72             System.out.println("文件不存在!");
     73         }
     74     }
     75     /**
     76      * @param args
     77      */
     78     /**
     79      * 创建文件
     80      */
     81     public void createFile(String path,String fileName)
     82     {
     83         //创建文件对象
     84         File file = new File(path, fileName);
     85         //判断文件是否存在
     86         if(file.exists())
     87         {
     88             System.out.println("文件已经存在");
     89         }
     90         else
     91         {
     92             //创建文件
     93             try
     94             {
     95                 if (file.createNewFile())
     96                 {
     97                     System.out.println("文件创建成功");
     98                 }
     99                 else {
    100                     System.out.println("文件创建失败");
    101                 }
    102             } catch (IOException e)
    103             {
    104                 // TODO Auto-generated catch block
    105                 e.printStackTrace();
    106             }
    107         }
    108     }
    109     /**
    110      * 创建文件夹
    111      * @param path
    112      */
    113     public void createDirectroy(String path, String diretroyName)
    114     {
    115         File file = new File(path, diretroyName);
    116         //判断是否存在
    117         if(!file.exists())
    118         {
    119             //创建文件夹
    120             file.mkdir();
    121         }
    122         else
    123         {
    124             System.out.println("文件夹已经存在");
    125         }
    126     }
    127     /**
    128      * 拷贝文件
    129      * @param sourcePath 复制文件的路径  如:D:/
    130      * @param fileName 文件名  如:back.jpg
    131      * @param newPath 复制到的位置  如:E:/
    132      */
    133     public void copyFile(String sourcePath, String fileName, String newPath)
    134     {
    135         //创建复制的文件
    136         File sourceFile = new File(sourcePath,fileName);
    137         //判断文件是否存在
    138         if(sourceFile.exists())
    139         {
    140             File newFile = new File(newPath,fileName);
    141             //如果文件存在,判断文件的类型
    142             if(sourceFile.isFile())
    143             {
    144                 //如果是单个的文件,使用流读写文件
    145                 try
    146                 {
    147                     //构建输入流读取复制的文件
    148                     BufferedInputStream input = new BufferedInputStream(new FileInputStream(sourceFile),1024*1024*10);
    149                     //构建输出流写出文件
    150                     BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(newFile),1024*1024*10);
    151                     int data;
    152                     while((data = input.read()) != -1)
    153                     {
    154                         output.write(data);
    155                     }
    156                     //关闭流
    157                     output.flush();
    158                     output.close();
    159                     input.close();
    160                     
    161                 } catch (FileNotFoundException e)
    162                 {
    163                     // TODO Auto-generated catch block
    164                     e.printStackTrace();
    165                 } catch (IOException e)
    166                 {
    167                     // TODO Auto-generated catch block
    168                     e.printStackTrace();
    169                 }
    170             }
    171             else
    172             {
    173                 //如果是文件夹,新建文件夹
    174                 newFile.mkdir();
    175                 //判断文件夹中是否还有子文件
    176                 File[] files = sourceFile.listFiles();
    177                 //遍历子文件
    178                 for(File childFile : files)
    179                 {
    180                     //递归复制子文件
    181                     copyFile(childFile.getParent(), childFile.getName(), newFile.getAbsolutePath());
    182                 }
    183             }
    184         }
    185     }
    186     /**
    187      * 剪切文件
    188      * @param sourcePath
    189      * @param fileName
    190      * @param newPath
    191      */
    192     public void cutFile(String sourcePath, String fileName, String newPath)
    193     {
    194         //
    195     }
    196     /**
    197      * 重命名
    198      * @param path
    199      * @param fileName
    200      * @param newName
    201      */
    202     public void renameFile(String path, String fileName, String newName)
    203     {
    204         //创建源文件
    205         File oldFile = new File(path, fileName);
    206         //判断源文件是否存在
    207         if(oldFile.exists())
    208         {
    209             //创建新文件
    210             File newFile = new File(path, newName);
    211             //判断新文件是否存在
    212             if(!newFile.exists())
    213             {
    214                 //重命名
    215                 oldFile.renameTo(newFile);
    216             }
    217             else
    218             {
    219                 System.out.println("文件名已存在");
    220             }
    221             
    222         }
    223         else
    224         {
    225             System.out.println("文件不存在");
    226         }
    227         
    228     }
    229     /**
    230      * 删除文件
    231      * @param path
    232      * @param fileName
    233      */
    234     public void deleteFile(String path, String fileName)
    235     {
    236         //获取要删除的文件对象
    237         File file = new File(path,fileName);
    238         //判断文件是否存在
    239         if(file.exists())
    240         {
    241             //如果存在,判断该文件是文件还是文件夹
    242             if(file.isDirectory())
    243             {
    244                 //如果是文件夹,获取该文件夹的子文件
    245                 File[] files = file.listFiles();
    246                 //递归删除子文件
    247                 for(File childFile : files)
    248                 {
    249                     deleteFile(childFile.getParent(), childFile.getName());
    250                 }
    251             }
    252             //删除整个文件
    253             file.delete();
    254         }
    255     }
    256 }
  • 相关阅读:
    css文字选中效果
    使用HTML5来实现本地文件读取和写入
    web响应式图片的5种实现
    前端日志上报的新姿势“Beacon”
    lock文件_我们为什么需要 lock 文件
    本地电脑和远程电脑无法复制粘贴
    device supports x86 but apk only supports armeabi-v7a问题解决
    Android Studio中 安卓模拟器 联网
    Android Studio 配置Gradle
    Android连载1-自定义UI控件
  • 原文地址:https://www.cnblogs.com/Wfei/p/3332773.html
Copyright © 2020-2023  润新知