java i/o的基本使用

前言

字节流 – 字符流 – 高级流 都有相同的方法
在代码里我就没有写了

读数据

InputStream 的使用(字节流)


	public static void main(String[] args) throws IOException {


        /**
         * 获取文件对象
         */
        File file = new File("文件路径");
        /**
         * 获取输入流
         */
        InputStream inputStream = new FileInputStream(file);
        /**
         * bytes 用来存储数据的数组
         */
        byte[] bytes = new byte[4];
        while (true){
            /**
             * 循环读取文件内的内容
             */
            int readCount = inputStream.read(bytes);
            /**
             * 如果 readCount == -1 代表没有督导数据
             * 那就停止循环
             */
            if(readCount == -1) break;
            /**
             * 打印读取到的数据
             */
            System.out.println(new String(bytes,0,readCount));
        }
        /**
         * 手动关闭
         */
        inputStream.close();
    }


Reader的使用(字符流–适合读取中文)

	 public static void main(String[] args) throws IOException {

        /**
         * 获取文件对象
         */
        File file = new File("文件路径");
        /**
         * 获取一个字符流
         */
        Reader reader = new FileReader(file);
        char[] chars = new char[4];
        while (true){
            /**
             * 循环读取文件内的内容
             */
            int readCount = reader.read(chars);
            /**
             * 如果 readCount == -1 代表没有督导数据
             * 那就停止循环
             */
            if(readCount == -1) break;
            /**
             * 打印读取到的数据
             */
            System.out.println(new String(chars,0,readCount));
        }
        reader.close();
    }


BufferedReader 按行读取数据(高级流)

	public static void main(String[] args) throws IOException {
    	/**
         * 一行行的读取文件
         */
        File file = new File("文件路径");
        FileReader reader = new FileReader(file);
        /**
         * 创建一个高级流
         */
        BufferedReader bufferedReader = new BufferedReader(reader);
        /**
         * 循环读取文件行数据
         *
         * line = bufferedReader.readLine() 先赋值在判断 再输出
         * 因为调用了 bufferedReader.readLine() 之后再次调用就是下一行数据了
         */
        String line = null;
        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
        }

        /**
         * 手动关闭
         */
        reader.close();
        bufferedReader.close();
	}

写数据

outputStream的使用(字节流)

简单使用


   public static void main(String[] args) throws IOException {

        /**
         * 简单使用
         * 
         * 获取文件对象
         */
        File file = new File("文件路径");
        /**
         * 如果文件不存在就创建一个
         */
        if(!file.exists()) file.createNewFile();
        /**
         * 创建输出流
         */
        OutputStream outputStream = new FileOutputStream(file);

        String msg ="这是我要存入文件的数据";
        /**
         * 将数据写入
         */
        outputStream.write(msg.getBytes());
        /**
         * 手动关闭
         */
        outputStream.close();
    }


克隆文件实例



    public static void main(String[] args) throws IOException {

        cloneFile("数据源文件路径",
                "目标文件路径");
    }

    public static boolean cloneFile(String resource,String target) throws IOException {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            File resourceFile = new File(resource);
            File targetFile = new File(target);
            if(!resourceFile.exists()) throw new RuntimeException("无法找到数据源文件");
            /**
             * 如果目标文件不存在就创建一个
             */
            if(!targetFile.exists()) targetFile.createNewFile();
            /**
             * 获取输入流 == 数据原文件
             */
            inputStream = new FileInputStream(resourceFile);
            /**
             * 获取输出流 == 目标文件
             * */
            outputStream = new FileOutputStream(targetFile);
            /**
             * bytes 用来存储数据的数组
             */
            byte[] bytes = new byte[4];
            while (true){
                /**
                 * 循环读取文件内的内容
                 */
                int readCount = inputStream.read(bytes);
                /**
                 * 如果 readCount == -1 代表没有督导数据
                 * 那就停止循环
                 */
                if(readCount == -1) break;
                /**
                 * 将数据写入目标文件
                 */
                outputStream.write(bytes);
            }
            return true;
        }catch (IOException e){
            throw e;
        }finally {
            /**
             * 真实场景中把关闭流的操作放在 finally 比较合适
             * 如果报错了也要关闭流
             */
            inputStream.close();
            outputStream.close();
        }

    }


Writer 的使用(字符流)


 public static void main(String[] args) throws IOException {


        File file = new File("文件路径");
        /**
         * 如果文件不存在就创建一个
         */
        if(!file.exists()) file.createNewFile();
        /**
         * 创建输出流
         */
        Writer writer = new FileWriter(file);

        String msg ="这是我要存入文件的数据";
        /**
         * 将数据写入
         * Writer 可以直接写入 String 类型的数据
         */
        writer.write(msg);
        /**
         * 手动关闭
         */
        writer.close();
    }

BufferedWriter的简单使用(高级流)

  public static void main(String[] args) throws IOException {


        File file = new File("文件路径");
        /**
         * 如果文件不存在就创建一个
         */
        if(!file.exists()) file.createNewFile();
        /**
         * 创建输出流
         */
        Writer writer = new FileWriter(file);
        BufferedWriter bufferedWriter = new BufferedWriter(writer);
        String msg ="这是我要存入文件的数据bufferedWriter";
        /**
         * 将数据写入
         * Writer 可以直接写入 String 类型的数据
         */
        bufferedWriter.write(msg);
//        bufferedWriter.append(msg);
        bufferedWriter.flush();
        /**
         * 手动关闭
         */
        writer.close();
        bufferedWriter.close();
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值