IO操作

一、File类:

  定义:文件描述符,用于描述在磁盘上的一个文件,或目录

  File 类构造器:

二、io读取xxx.properties文件

public class PropDemo{
public vo test(){
    Properties prop = new Properties();
    //1.绝对路径
    File file = new File("/users/kechunwang/BaiCe/config.properties");
    prop.load(new FileInputStream(file));
    //读取key的value
    Object obj = prop.get("driver");
    obj.sout;

    //2.相对于resources目录的相对路径
    Properties prop1 = new Properties();
    prop1.load(PropDemo.getClassLoader().getResourceAsStream("db/config.properties"));
    Object obj1 = prop.get("driver");
    obj1.sout;  
}
}
View Code

三、FileReader读取文件标准写法:

public class FileReaderDemo {
    public static void main(String[] args) {
        FileReader reader = null;
        try{
            //文件描述
            File file = new File("test.log");
            //定义文件取流
            reader = new FileReader(file);
            //文件读取
            char[] buf = new char[256];
            int len = 0;
            while ((len = reader.read(buf)) != -1){
                System.out.println("len:"+len);
                //String构造,注意边界
                String val = new String(buf,0,len);
                System.out.println(val);
            }
            // 理解的读取过程
            // // while (len != -1) {
            // //   len = reader.read(buf);
            // //   if (len != -1) {
            // //     String val = new String(buf, 0, len);
            // //     System.out.println(val);
            // //   }
            // }
        }catch (FileNotFoundException fnfe){
            fnfe.printStackTrace();
        }catch (IOException ioe){
            ioe.printStackTrace();
        }finally {
            //关流
            if (null != reader){
                try {
                    reader.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
        }
    }
}
View Code

四、OutputStream

public class OutputStreamDemo {

    public static void main(String[] args) throws IOException {
        FileOutputStream outputStream = null;
        try{
            //定位文件描述
            File file = new File("test.log");
            //实例化要写文件的基本类FileOutputStream
            outputStream = new FileOutputStream(file);
            String str = "hello world111";
            //写数据
            outputStream.write(str.getBytes());
        }catch (FileNotFoundException ffe){
            System.out.println("文件没有");
        }catch (IOException ioe){
            System.out.println("写入失败");
        }finally {
            //关流,千万要记得关闭
            try {
                if (null != outputStream){
                    outputStream.close();
                }
            }catch (IOException e){
                e.printStackTrace();
            }
            outputStream.close();
        }
    }
}
View Code

五、InputStream

public class IPDemo01 {
    public static void main(String[] args)  {
        File file = new File("test.log");
        //
        try (FileInputStream ins = new FileInputStream(file)){
            byte[] buf = new byte[512];
            int len = 0;
            while ((len=ins.read(buf))!= -1){
                String val = new String(buf,0,len);
                System.out.println(val);
        }

        }catch (IOException ex){
            throw new IllegalStateException(ex);
        }
    }
    public void test01(){
        File file = new File("test.log");
        try (InputStream ins = new FileInputStream(file)){
            byte[] buf = new byte[512];
            int len = 0;
            while ((len = ins.read(buf))!= -1){
                String val = new String(buf,0,len);
                System.out.println(val);
            }
        }catch (IOException ioe){
            throw new IllegalStateException(ioe);
        }
    }
}
View Code

 六、常用方法:

@Test
    public void test1(){
        File file = new File("test.log");
        //是否可读
        boolean canRead = file.canRead();
        //是否可写
        boolean canWrite = file.canWrite();
        //是否可执行:
        boolean canExecute = file.canExecute();
        //给文件赋值读权限
        file.setReadable(true);
        //获取文件名字
        String fileName = file.getName();
        //获取绝对路径:
        File fileAbsouluteFile = file.getAbsoluteFile();
        //返回String类型的绝对路径:
        String fileAbsolutePath = file.getAbsolutePath();
        //返回父目录
        String fileParent = file.getParent();
        System.out.println("fileParent:"+fileParent);
        //
        File fileParentFile = file.getParentFile();
        System.out.println("fileParentFile:"+fileParentFile);
        //
        String filePath = file.getPath();
        System.out.println("filePath:"+filePath);

        //判断是否为文件
        boolean isFiled = file.isFile();
        //判断是否为目录
        boolean isDirectoryd = file.isDirectory();
    }
View Code

七、复制:

public class CopyFile {
    public static void main(String[] args) throws Exception {
        String src = "test.log";
        String dst = "test1.log";

        //
        BufferedReader bufferedReader = new BufferedReader(new FileReader(src));
        String val = null;
        String data = "";
        while ((val = bufferedReader.readLine()) != null){
            data += val + "
";
        }

        //
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(dst));
        bufferedWriter.write(data);
        bufferedWriter.flush();
    }
}
View Code

八、遍历本地一个目录下,找到后缀名是.log的,且占用空间最大的那个文件删除

@Test
    public void test1(){
        File dir = new File("/user");
        //找到目录下的文件:
        File[] files = dir.listFiles();
        //定义最小文件大小
        long maxFileSize = 0;
        //定义最小文件
        File maxFile = null;
        //遍历循环
        for (File file:files){
            String fileName = file.getName();
            //判断名字结尾
            if (fileName.endsWith(".log")){
                long len = file.length();
                if (len > maxFileSize){
                    maxFileSize = len;
                    maxFile = file;
                }
            }
        }
        maxFile.delete();
    }
View Code

九、BufferedReader

public class BufferedReaderDemo2 {
    public static void main(String[] args) {
        //文件描述
        File file = new File("test.log");
        BufferedReader bufferedReader = null;
        //定义文件读取流
        try {
            //相比FileReader的区别
            bufferedReader = new BufferedReader(new FileReader(file));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //文件读取
        String val = null;
        try {
            while ((val = bufferedReader.readLine()) != null){
                System.out.println(val);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关流
            if (bufferedReader != null){
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
View Code
public class BufferedReaderDemo {
    public static void main(String[] args) {
        Reader reader = null;
        try {
            //文件描述
            File file = new File("test.log");
            //定义文件读取流
            reader = new BufferedReader(new FileReader(file));
            //读取文件
            char[] buf = new char[256];
            int len = 0;
            while ((len = reader.read(buf)) != -1){
                String val = new String(buf,0,len);
                System.out.println("val:"+val);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException ioe){
            ioe.printStackTrace();
        } finally {
            //关流
            if (reader != null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}
View Code

十、BufferedOutputStream

public class BufferedOutputStreamDemo {
    public static void main(String[] args) {
        BufferedOutputStream bufferedOutputStream = null;
        //定义文件描述,文件名称和路径
        File file = new File("test2.log");
        try {
            //定义文件写入流
            bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file));
            String wirete = "hello workd a ";
            bufferedOutputStream.write(wirete.getBytes());
        }catch (FileNotFoundException fnfe){
            fnfe.printStackTrace();
        }catch (IOException ioe){
            ioe.printStackTrace();
        }finally {
            //关流
            if (bufferedOutputStream != null){
                try {
                    bufferedOutputStream.close();
                }catch (IOException ioe){
                    ioe.printStackTrace();
                }
            }
        }
    }
}
View Code
原文地址:https://www.cnblogs.com/wangkc/p/11118478.html