zoukankan      html  css  js  c++  java
  • java File类的常见用法

    1. File类简单用法!  
    1.    
    1. import java.io.File;  
    2. import java.io.IOException;  
    3.   
    4. public class TestFile {  
    5.       
    6.     public void createFile(String path){  
    7.         File file=new File(path);  
    8.         if(!file.exists()){//判断文件是否存在  
    9.             try {  
    10.                 file.createNewFile();  //创建文件  
    11.                   
    12.             } catch (IOException e) {  
    13.                 // TODO Auto-generated catch block  
    14.                 e.printStackTrace();  
    15.             }  
    16.         }  
    17.         /*获取文件名*/  
    18.         String name=file.getName();  
    19.         /*获取文件路径*/  
    20.         String path_=file.getPath();  
    21.         /*获取绝对路径名*/  
    22.         String absPath=file.getAbsolutePath();  
    23.         /*获取父亲文件路径*/  
    24.         String parent=file.getParent();  
    25.         /*文件大小*/  
    26.         long size=file.length();  
    27.         /*最后一次修改时间*/  
    28.         long time=file.lastModified();  
    29.         System.out.println("文件名:"+name);  
    30.         System.out.println("文件路径:"+path_);  
    31.         System.out.println("文件的绝对路径:"+absPath);  
    32.         System.out.println("文件的父文件路径:"+parent);  
    33.         System.out.println("文件的大小:"+size);  
    34.         System.out.println("文件最后一次修改时间:"+time);  
    35.         //file.delete();   //删除文件  
    36.           
    37.     }  
    38.       
    39.     public void createDir(String path){  
    40.         File file=new File(path);  
    41.         if(!file.exists()){  
    42.             file.mkdirs();  //创建文件夹  
    43.         }  
    44.           
    45.         //file.delete();  若文件夹为空,则删除文件夹  
    46.           
    47.     }  
    48.       
    49.     /** 
    50.      * 遍历文件夹中的文件并显示 
    51.      */  
    52.     public void fileTest(String path){  
    53.         File file=new File(path);  
    54.         File[] files=file.listFiles();  
    55.         //System.out.println(files.length);  
    56.           
    57.         for (File f : files) {  
    58.             if(f.isFile()){  
    59.                 System.out.println(f.getName()+"是文件!");  
    60.             }else if(f.isDirectory()){  
    61.                 fileTest(f.getPath());  
    62.             }  
    63.         }  
    64.           
    65.     }  
    66.       
    67.     public void reFileName(String fromPath,String toPath){  
    68.         File file1=new File(fromPath);  
    69.         File file2=new File(toPath);  
    70.         /*判断file2文件夹路径存在与否,不存在则创建*/  
    71.         if(!file2.exists()){  
    72.             new File(file2.getParent()).mkdirs();  
    73.         }  
    74.         file1.renameTo(file2);  //修改文件名  
    75.     }  
    76.   
    77.     public static void main(String[] args) {  
    78. //      File file=new File("E:\myjava\1.txt");  //  Window路径\  
    79. //      File flie_=new File("E:/myjava","1.txt"); //linux路径 /  
    80. //        
    81.         TestFile tf=new TestFile();  
    82.         //tf.createFile("E:\myjava\1.txt");  
    83.         //tf.fileTest("E:/wepull");  
    84.         //tf.createDir("e:/sunxiao/abc/1.txt");  
    85.         //tf.reFileName("E:\my\2.txt","E:\myjava\1.txt");  
    86.         tf.copyFlie("E:\myjava","F:\");  
    87.     }  
    88.   
    89.     private void copyFlie(String src, String to) {  
    90.         File file1=new File(src);  
    91.         String topath=to+"\"+file1.getName();  
    92.         File file2=new File(topath);  
    93.         if(!file2.exists()){  
    94.             file2.mkdirs();  
    95.         }  
    96.         System.out.println(topath);  
    97.         File[] file=file1.listFiles();  
    98.         for (File f : file) {  
    99.             if(f.isFile()){  
    100.                 String path2=topath+"\"+f.getName();  
    101.                 Creatfile(path2);  
    102.             }else if(f.isDirectory()){  
    103.                 String s=f.getPath();  
    104.                 copyFlie(s,topath);  
    105.             }  
    106.         }  
    107.           
    108.     }  
    109.   
    110.     private void Creatfile(String path2) {  
    111.         File file3=new File(path2);  
    112.         if(!file3.exists()){//判断文件是否存在  
    113.             try {  
    114.                 file3.createNewFile();  //创建文件  
    115.                   
    116.             } catch (IOException e) {  
    117.                 // TODO Auto-generated catch block  
    118.                 e.printStackTrace();  
    119.             }  
    120.         }  
    121.     }  
  • 相关阅读:
    wc项目
    随笔之——伪类选择器:nthchild(n) 与 nthoftype(n)的区别!!!
    随笔之——浮动(float)的影响及其清除、、clear与overflowhidden清除float的不同!!!
    随笔之——各大热门网站search 搜索框的写法,浅析!
    一个简单的注册页面
    自我介绍、目标
    position的6个属性的实验和相关分析
    第二次作业
    第三次作业
    第一次作业
  • 原文地址:https://www.cnblogs.com/Lanyuxuan/p/5267616.html
Copyright © 2011-2022 走看看