美文网首页
Java File 文件操作

Java File 文件操作

作者: buyaole | 来源:发表于2016-08-08 20:49 被阅读0次

    java.io包里的类:

    • InputStream/OutputStream : 以字节为单位读写文件内容,一次读/写一个字节,常用于读二进制文件,如图片、音频、视频等文件

    • FileInputStream/FileOutputStream : 以字节为单位读写文件内容,一次读写多个字节

    • Reader/Writer : 以字符为单位,一次读写一个字符

    • BufferedReader/BufferedWriter : 以行为单位,一次读写一行,以换行符为结束标志

    java.nio (new i/o)(jdk 1.4加入)
    特点:非阻塞

    创建方法

    1. boolean createNewFile() 不存在返回true 存在返回false
    2. boolean mkdir() 创建目录
    3. boolean mkdirs() 创建多级目录

    删除方法

    1. boolean delete()
    2. boolean deleteOnExit() 文件使用完成后删除

    判断方法

    1. boolean canExecute()判断文件是否可执行
    2. boolean canRead()判断文件是否可读
    3. boolean canWrite() 判断文件是否可写
    4. boolean exists() 判断文件是否存在
    5. boolean isDirectory() 判断是否是一个目录
    6. boolean isFile() 判断是否是一个文件
    7. boolean isHidden()8.boolean isAbsolute()判断是否是绝对路径 文件不存在也能判断

    获取方法

    1. String getName() 返回文件名
    2. String getPath()
    3. String getAbsolutePath()
    4. String getParent()//如果没有父目录返回null
    5. long lastModified()//获取最后一次修改的时间
    6. long length()
    7. boolean renameTo(File f)
    8. File[] liseRoots()//获取机器盘符
    9. String[] list()
    10. String[] list(FilenameFilter filter)

    具体文件操作代码:

    1. 创建文件/文件夹 :

    //创建文件夹
        public static void createFolder(String path){
            File file = new File(path);
            file.mkdirs();
        }
        
        //创建文件
        public static boolean createFile(String fileName){
            File file = new File(fileName);
            System.out.println(file.getAbsolutePath());
            boolean isSucess = false;
            try {
                isSucess = file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return isSucess;
        }
    
    1. 写文件:

    //写入文件
        public static void writeFile(String fileName){
            File file = new File(fileName);
            BufferedWriter bWriter = null;
            try {
                FileWriter writer = new FileWriter(file);
                bWriter = new BufferedWriter(writer);
                bWriter.write("hello ");
                bWriter.newLine();
                bWriter.write('a');
                bWriter.write(27);
                bWriter.write("world");
                
            } catch (IOException e) {
                e.printStackTrace();
            }finally{
                if (bWriter != null) {
                    try {
                        bWriter.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    
    1. 读文件:

    //读取文件
        public static void readFile(String fileName){
            File file = new File(fileName);
            BufferedReader bReader = null;
            try {
                FileReader reader = new FileReader(file);
                bReader = new BufferedReader(reader);
                String line = bReader.readLine();
                while(line != null){
                    System.out.println(line);
                    line = bReader.readLine();
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }finally {
                try {
                    if (bReader != null) {
                        bReader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    1. 列出当前目录下所有文件:

    //列出当前目录下的所有文件
        public static void listFile(String folderPath){
            File file = new File(folderPath);
            for(String fileName : file.list()){
                System.out.println("fileName : "+ fileName);
            }
        }
    
    1. 删除文件:

    //删除文件
        public static void delFile(String folderPath){
            File file = new File(folderPath);
            System.out.println(folderPath);
            file.delete();
        }
    
    1. 拷贝文件:

    2. 方法一:
    public static void copyFile(String oldPath, String newPath){
            File oldFile = new File(oldPath);
            File newFile = new File(newPath);
            BufferedReader bReader = null;
            BufferedWriter bWriter = null;
            try {
                FileReader fr = new FileReader(oldFile);
                bReader = new BufferedReader(fr);
                String all = "";
                String line = bReader.readLine();
                System.out.println("拷贝中。。。请稍候");
                while(line != null){
                    all += line;
                    line = bReader.readLine();
                }
                FileWriter fw = new FileWriter(newFile);
                bWriter = new BufferedWriter(fw);
                bWriter.write(all);
                System.out.println("完成!");
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }catch(IOException e){
                e.printStackTrace();
            }finally {
                try {
                    bReader.close();
                    bWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    
    1. 方法二:
    public static void copyFile2(File source, File dest) throws IOException {
            InputStream input = null;
            OutputStream output = null;
            try {
                input = new FileInputStream(source);
                output = new FileOutputStream(dest);
                byte[] buf = new byte[1024];
                int bytesRead;
                System.out.println("拷贝中。。。请稍候");
                while ((bytesRead = input.read(buf)) > 0) {
                    output.write(buf, 0, bytesRead);
                }
                System.out.println("完成!");
            } finally {
                input.close();
                output.close();
            }
        }
    
    1. 方法三:
    public static void copyFiles(File source, File dest){
            try {
                System.out.println("拷贝中。。。请稍候");
                Files.copy(source.toPath(), dest.toPath());
                System.out.println("完成!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:Java File 文件操作

          本文链接:https://www.haomeiwen.com/subject/coshsttx.html