JAVA---IO流(二)

1.Java文件与目录的管理

及演示命令: 可以运用字符流转换成字节流,再用字节流转换成字符流输出。学习的方法有很多

package com.newdream.class4;
import java.io.File;


public class createFile {
    public static void main(String[] args) {
        //1. 创建文件、检查文件是否存在、删除文件
        String filepath="E:/演示目录/演示.txt";
        String root_path="E:/演示目录";
        
        createFile cf=new createFile();
        cf.createFile(filepath);        
        //2. 判定文件是否有效
        cf.test_isDirectory("path");
        cf.test_isDirectory("E:/演示目录/1dasd/asdasd/qwwerqwr");     //这是一个不存在的目录      
        //3. 获取文件名称和路径
        cf.get_fileName(filepath);
        cf.get_fileName(root_path);
        //4. 获取文件大小
        cf.get_filelength(filepath);
        //5. 获取目录信息
        cf.get_dirList(root_path);        
        //6. 创建目录
        cf.del_file(root_path+"//test");
        //7. 删除文件        
        cf.del_file(filepath);   //删除文件
        cf.del_file(root_path+"//test");  //删除目录
        cf.del_file(filepath+"//asdasd");  //删除不存在目录
    }        
    //1. 创建文件、检查文件是否存在、删除文件
    public  void  createFile(String filepath){        
        //创建一个文件对象
        File file=new File(filepath);        
        //如果文件存在,就删除文件再创建
        boolean flag=file.exists();
        if(flag)
            file.delete();
        try{
            flag=file.createNewFile();
            if(flag)
                System.out.println("系统提示:文件创建成功!");
            else
                System.out.println("系统提示:文件创建失败!");
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            
        }        
    }    
        //2. 判定文件是否有效
        public  void test_isDirectory(String root_path){            
            File file=new File(root_path);            
            if (file.isDirectory()){
                System.out.println("文件夹目录有效");
            }else{
                System.out.println("文件夹目录无效");
            }
        }    
        //3. 获取文件名称和路径
        public void get_fileName(String filepath){
            File file=new File(filepath);
            String name = file.getName();
            String path = file.getPath();
            System.out.println("文件路径为:"+path+",文件夹路径为:"+name);            
        }
        //4. 获取文件大小
        public void get_filelength(String filepath){
            File file=new File(filepath);
            long file_size=file.length();
            System.out.println("文件大小为:"+file_size);
        }        
        //5. 获取目录信息                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
        public void get_dirList(String root_path){
            File file=new File(root_path);
            String [] files=file.list();
            System.out.println(root_path+":目录信息:");
            for(String name:files){
                System.out.println(name);
            }
        }
        //6. 创建目录
        public void createDir(String root_path){
            File file =new File(root_path);
            boolean flag;
            flag=file.mkdirs();
            if(flag)
                System.out.println(root_path+"目录创建成功!");
            else 
                System.out.println(root_path+"目录创建失败!");
        }
        //7. 删除文件
        public void del_file(String filepath){
            File file=new File(filepath);
            boolean flag=file.delete();
            if(flag)
                System.out.println("删除成功!");
            else
                System.out.println("删除失败!");
        }
}

 

 本文仅代表作者观点,系作者@请多指教 发表。
 欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
http://www.cnblogs.com/yushengaqingzhijiao/p/yushengaqingzhijiao-.html
posted @ 2018-01-25 13:16  罐装七喜  阅读(28)  评论(0)    收藏  举报