Java IO ---- File 类

创建一个新文件

public boolean createNewFile() throws IOException

import java.io.File ;
import java.io.IOException ;
public class FileDemo01{
    public static void main(String args[]){
        File f = new File("d:\\test.txt") ;        // 实例化File类的对象
        try{
            f.createNewFile() ;        // 创建文件,根据给定的路径创建
        }catch(IOException e){
            e.printStackTrace() ;    // 输出异常信息
        }
    }
};
View Code

  注:d:\\test.txt中的两个斜杠的第一个斜杠是表示转义符

   以上已经完成了文件的创建功能,但如果开发时按照以上的格式编写,则肯定会出现错误,因为在各个操作系统中,实际上路径的分割符是不一样的,比如:windows中使用的反斜杠:“\” ;linux中使用正斜杠:“/”

  如果要想让java程序的可移植性继续保持,则最好根据所在操作系统来自动使用分隔符。

import java.io.File ;
import java.io.IOException ;
public class FileDemo02{
    public static void main(String args[]){
        System.out.println("pathSeparator:" + File.pathSeparator) ;    // 调用静态常量
        System.out.println("separator:" + File.separator) ;    // 调用静态常量
    }
};
import java.io.File ;
import java.io.IOException ;
public class FileDemo03{
    public static void main(String args[]){
        File f = new File("d:"+File.separator+"test.txt") ;        // 实例化File类的对象
        try{
            f.createNewFile() ;        // 创建文件,根据给定的路径创建
        }catch(IOException e){
            e.printStackTrace() ;    // 输出异常信息
        }
    }
};

  注意:在操作文件的时候一定要注意分隔符,使用File.separator表示

删除一个指定文件

  在File类中使用以下方法就可以删除文件

public boolean delete()
import java.io.File ;
import java.io.IOException ;
public class FileDemo04{
    public static void main(String args[]){
        File f = new File("d:"+File.separator+"test.txt") ;        // 实例化File类的对象
        f.delete() ;    // 删除文件
    }
};

import java.io.File ;
import java.io.IOException ;
public class FileDemo05{
    public static void main(String args[]){
        File f = new File("d:"+File.separator+"test.txt") ;        // 实例化File类的对象
        if(f.exists()){    // 如果文件存在则删除
            f.delete() ;    // 删除文件
        }
    }
};

综合创建,删除文件操作

import java.io.File ;
import java.io.IOException ;
public class FileDemo06{
    public static void main(String args[]){
        File f = new File("d:"+File.separator+"test.txt") ;        // 实例化File类的对象
        if(f.exists()){    // 如果文件存在则删除
            f.delete() ;    // 删除文件
        }else{
            try{
                f.createNewFile() ;        // 创建文件,根据给定的路径创建
            }catch(IOException e){
                e.printStackTrace() ;    // 输出异常信息
            }
        }
    }
};

创建一个文件夹

import java.io.File ;
import java.io.IOException ;
public class FileDemo07{
    public static void main(String args[]){
        File f = new File("d:"+File.separator+"mldn") ;        // 实例化File类的对象
        f.mkdir() ;    // 创建文件夹
    }
};

列出指定目录的全部文件

import java.io.File ;
import java.io.IOException ;
public class FileDemo08{
    public static void main(String args[]){
        File f = new File("d:"+File.separator) ;        // 实例化File类的对象
        String str[] = f.list() ;    // 列出给定目录中的内容
        for(int i=0;i<str.length;i++){
            System.out.println(str[i]) ;
        }
    }
};

import java.io.File ;
import java.io.IOException ;
public class FileDemo09{
    public static void main(String args[]){
        File f = new File("d:"+File.separator) ;        // 实例化File类的对象
        File files[] = f.listFiles() ;    // 列出全部内容
        for(int i=0;i<files.length;i++){
            System.out.println(files[i]) ;
        }
    }
};

判断一个给定的路径是否是目录

import java.io.File ;
import java.io.IOException ;
public class FileDemo10{
    public static void main(String args[]){
        File f = new File("d:"+File.separator) ;        // 实例化File类的对象
        if(f.isDirectory()){    // 判断是否是目录
            System.out.println(f.getPath() + "路径是目录。") ;
        }else{
            System.out.println(f.getPath() + "路径不是目录。") ;
        }
    }
};

要求:列出指定目录的全部内容

import java.io.File ;
import java.io.IOException ;
public class FileDemo11{
    public static void main(String args[]){
        File my = new File("d:" + File.separator) ;    // 操作路径
        print(my) ;
    }
    public static void print(File file){    // 递归调用
        if(file!=null){    // 判断对象是否为空
            if(file.isDirectory()){    // 如果是目录
                File f[] = file.listFiles() ;    // 列出全部的文件
                if(f!=null){    // 判断此目录能否列出
                    for(int i=0;i<f.length;i++){
                        print(f[i]) ;    // 因为给的路径有可能是目录,所以,继续判断
                    }
                }
            }else{
                System.out.println(file) ;    // 输出路径
            }
        }
    }
};

posted @ 2015-04-15 16:51  闲来垂钓  阅读(228)  评论(0)    收藏  举报