Java中的I/O

IO流是Java中数据传输的通道,根据传输数据的不同可以分为:字节流(InputStream、OutputStream)和字符流(Reader、Writer),根据流向的不同可以分为:输入流(InputStream、Reader)和输出流(OutputStream、Writer),此外还有缓冲流(Buffre+流)和转换流(InputStreamReader、OutputStreamWriter)。输入流只能读,输出流只能写。
字节输入流的基类是InputStream,我们不能直接使用它,FileInputStream和BufferedInputStream是其使用得最多的子类:
FileInputStream:

		String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");

        //一次读取一个字节
        try {
            FileInputStream in = new FileInputStream(file);
            int len;
            while ((len = in.read()) != -1) {
                Log.i(TAG, "input:" + String.valueOf((char) len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //一次读取一定数量的字节并存入byte数组中
        try {
            FileInputStream inputStream = new FileInputStream(file);
            byte[] bytes = new byte[1024];
            //或者
//            int size = inputStream.available();
//            byte[] bytes = new byte[size];
            int len;
            while ((len = inputStream.read(bytes)) != -1) {
                String str = new String(bytes, 0, len);
                Log.i(TAG, "byte input:" + str);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

BufferedInputStream:字节缓冲流

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            FileInputStream inputStream = new FileInputStream(file);
            BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
            int len;
            byte[] bytes = new byte[1024];
            while ((len = bufferedInputStream.read(bytes)) != -1) {
                Log.i(TAG, "input:" + new String(bytes, 0, len));
            }
            bufferedInputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

字节输出流的基类是OutputStrean,FileOutputStream和BufferedOutputStream是其使用最多的子类。
FileOutputStream:

		String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            FileOutputStream out = new FileOutputStream(file);
            out.write("ABCD".getBytes());
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        //追加写入内容
        try {
            FileOutputStream appendOut = new FileOutputStream(file, true);
            appendOut.write("12345".getBytes());
            appendOut.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

BufferedOutputStream:带缓冲的输出流

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            FileOutputStream outputStream = new FileOutputStream(file,true);
            BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
            bufferedOutputStream.write("ABCD".getBytes());
            bufferedOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

字符输入流的基类是reader,FileReader和InputStreamReader是其使用最多的子类。
FileReader:

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            Reader reader = new FileReader(file);
            int count = 0;
            char[] ch = new char[1024];
            while ((count = reader.read(ch)) != -1) {
                Log.i(TAG, new String(ch, 0, count));
            }
            reader.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

InputStreamReader:装换流(字节输入流装换为字符输入流)

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            InputStream inputStream = new FileInputStream(file);
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            int len;
            char[] chars = new char[1024];
            while ((len = inputStreamReader.read(chars)) != -1) {
                Log.i(TAG, "inputStreamReader:" + new String(chars, 0, len));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

字符输出流的基类是Writer,FileWriter和OutputStreamWriter是其使用最多的子类。
FileWriter:

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            Writer writer = new FileWriter(file, true);
            writer.append("123");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

OutputStreamWriter:装换流(字节输出流转换为字符输出流)

        String path = Environment.getExternalStorageDirectory() + "/Pictures";
        File file = new File(path + "/012.txt");
        try {
            OutputStream outputStream = new FileOutputStream(file,true);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream);
            outputStreamWriter.write("00000");
            outputStreamWriter.flush();
            outputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值