zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    我是如何折腾.NET Resx资源文件的 当计算机中的资源已经足够多时,我们也要学会尽可能的借用
    当程序开发人员开始抛弃技术时,是否意味着噩梦的开始?抛弃了SQL Server 2000才发现客户的简单问题真的很难解决
    分享.NET ERP项目开发中应用到的重量级工具 选择合适的工具和资源,做项目效率高而且规范程度高
    Management Console ERP项目开发辅助工具 正确的方法+适当的工具使做项目的效率高而且问题少
    ERP系统管理员的工具箱 推荐几款优秀的数据比较同步工具 Data Compare and Sync tool
    亲自下载CSDN社区600万用户数据 设计两条编程题目考验你的.NET编程基础
    知识管理系统Data Solution研发日记之十六 保存服务器文档为本机PDF格式
    【转】好的学习方法
    iPhone开发学习笔记[7/50]在xcode里配置成功subversion
    iPhone开发学习笔记[4/50]表视图的使用
  • 原文地址:https://www.cnblogs.com/wangkc/p/11118478.html
Copyright © 2011-2022 走看看