美文网首页
java IO-File类的基本使用

java IO-File类的基本使用

作者: BuddhaCoder | 来源:发表于2018-10-21 00:40 被阅读0次

    【创建一个新的文件】

    package cn.ljtnono.io;
    
    import java.io.File;
    import java.io.IOException;
    
    /**
     * File 类的基本使用方式
     * @author Administrator
     *
     */
    public class UseFile {
    
        
        
        /**
         * 创建一个新的File
         */
        public static void createNewFile(String filePath) throws IllegalArgumentException{
            if (filePath == null || filePath.isEmpty()) {
                throw new IllegalArgumentException("请指定正确的文件路径!");
            }
            File file = new File(filePath);
            if (file.exists()) {
                System.out.println("当前File已经存在");
            } else {
                try {
                    file.createNewFile();
                    System.out.println("文件创建成功!");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    System.out.println("create File failed!");
                    e.printStackTrace();
                }
            }
        }
        
        public static void main(String[] args) {
            createNewFile("Demo.txt");
            System.out.println(File.separator);
            System.out.println(File.pathSeparator);
        }
        
        //怎么判断一个文件的路径是合法的
    }
    

    运行结果:在eclipse项目的根目录创建了一个Demo.txt的文件。

    【删除一个文件】

    public static void main(String[] args) {
            String fileName = "Demo.txt";
            File file = new File(fileName);
            if (file.exists()) {
                file.delete();
            } else {
                System.out.println("文件不存在!");
            }
        }
    

    运行结果:删除了创建的Demo.txt文件

    【创建一个文件夹】

    public static void main(String[] args) {
            String fileName = "Demo";
            File file = new File(fileName);
            file.mkdir();
        }
    

    运行结果:在eclipse项目的根目录下创建一个名为Demo的文件夹

    【列出指定目录的全部文件(包括隐藏文件)】

    public static void main(String[] args) {
            String fileName = "src";
            File file = new File(fileName);
            String[] list = file.list();
            for (String f : list) {
                System.out.println(f);
            }
        }
    

    运行结果:



    由于没有做是文件还是文件夹的判断,所以显示的是main和test文件夹。

    【判断文件是否是目录】

    public static void main(String[] args) {
            String fileName = "src";
            File file = new File(fileName);
            if (file.isDirectory()) {
                System.out.println("文件是目录!");
            } else {
                System.out.println("文件不是目录!");
            }
        }
    

    运行结果:文件是目录

    【搜索指定目录的全部内容】

    //搜索指定目录的全部内容
        public static void main(String[] args) {
            String fileName = "src";
            File file = new File(fileName);
            print(file);
        }
        
        public static void print(File f) {
            if (f != null) {
                if (f.isDirectory()) {
                    File[] fileArray = f.listFiles();
                    if (fileArray != null) {
                        for (int i = 0; i < fileArray.length;i++) {
                            print(fileArray[i]);
                        }
                    }
                }
                else {
                    System.out.println(f);
                }
            }
        }
    

    运行结果:


    【使用RandomAccessFile写入文件】

        public static void main(String[] args) throws IOException {
            String fileName = "hello.txt";
            File f = new File(fileName);
            RandomAccessFile demo = new RandomAccessFile(f,"rw");
            demo.writeBytes("asdsad");
            demo.writeInt(12);
            demo.writeBoolean(true);
            demo.writeChar('A');
            demo.writeFloat(1.21f);
            demo.writeDouble(12.123);
            demo.close();
        }
    

    运行结果:在eclipse根目录下的hello.txt文件中写入了各种数据,但是打开看的话发现是乱码。

    【文件的复制】

    public static void main(String[] args) throws IOException {
            //1.创建两个File
            String sourcePath = "bmp.bmp";
            String targetPath = "bb.bmp";
            File source = new File(sourcePath);
            File target = new File(targetPath);
            if (!source.exists()) {
                System.out.println("原文件不存在!");
            } else {
                // 2. 创建输入流
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(source));
                byte[] buff = new byte[bis.available()];
                //3.创建输出流
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(target));
                
                while (bis.read(buff, 0, buff.length) != -1) {
                    bos.write(buff, 0, buff.length);
                }
                bis.close();
                bos.close();
            }
        }
    

    运行结果:复制了bmp.bmp文件

    相关文章

      网友评论

          本文标题:java IO-File类的基本使用

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