博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java-IO-字节流和字符流
阅读量:4072 次
发布时间:2019-05-25

本文共 3385 字,大约阅读时间需要 11 分钟。

InputStream和Reader

InputStream

  • int read():从输入流中读取单个字节,返回所对应的字节数据(字节数据可直接转换成int类型)。
  • int read(byte[] b):从输入流中最对读取b.length个字节数据,并将其存储在数组b中,返回实际读取的字节数。
  • int read(byte[] b, int off, int len):从off位置开始将最多len个字节数据读取并存储进字节数组b中。

Reader

  • int read():从输入流中读取单个字符,返回所对应的字符数据(字符数据可直接转换成int类型)。
  • int read(char[] cbuf):从输入流中最对读取b.length个字符数据,并将其存储在数组cbuf中,返回实际读取的字符数。
  • int read(char[] cbuf, int off, int len):从off位置开始将最多len个字符数据读取并存储进字符数组cbuf中。

InputStream和Reader都是抽象类,本身不能创建实例,他们分别有一个读取文件的输入流:FileInputStream和FileReader,它们都是节点流(会直接和指定文件关联)。

package filetest;import java.io.FileInputStream;import java.io.FileNotFoundException;public class FileInputStreamTest {
public static void main(String[] args) { try { //创建字节输入流 FileInputStream fis=new FileInputStream("first.txt"); //穿件一个长度为1024的字节数组 byte[] bbuf=new byte[1024]; //保存实际读取的字节数 int hasRead=0; //使用循环来重复取字节 while((hasRead=fis.read(bbuf))>0) { //将读取出来的字节数组转换成字符串输出 System.out.println(new String(bbuf,0,hasRead)); } fis.close(); //关闭文件输入流 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package filetest;import java.io.FileReader;import java.io.IOException;public class FileReadderTest {
public static void main(String[] args) throws IOException { //创建字符输入流 FileReader fr=new FileReader("E:\\Java-IO\\first.txt"); //创建字符数组 char[] cbuf =new char[512]; //保存读取字符数 int hasRead=0; //使用循环来读取字符 while((hasRead=fr.read(cbuf))>0) { //将字符数组转换成字符串输出 System.out.println(new String(cbuf,0,hasRead)); } fr.close(); }}

OutputStream和Writer

三个方法:

  • void write(int c):将指定的字节/字符输出到输出流中。
  • void write(byte[]/char[] buf):将字节数组/字符数组中的数据输出到指定的输出流中。
  • void write(byte[]/char[] buf, int off, int len):将字节数组/字符数组从off开始到长度为len的字节/字符输出到输出流中。

Writer还包含两个方法:

  • void write(String str):将str字符串里包含的字符串输出到指定的输出流中。
  • void write(String str, int off, int len):将str字符串从off开始,到len个字符串输出到输出流中。
package filetest;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;public class FileOutputStreamTest {
public static void main(String[] args) { try { //创建字节输入流 FileInputStream fis=new FileInputStream("E:\\Java-IO\\first.txt"); //创建字节输出流 FileOutputStream fos=new FileOutputStream("E:\\Java-IO\\second.txt"); byte[] bbuf=new byte[32]; int hasRead=0; //循环从输入流中取出数据 while((hasRead=fis.read(bbuf))>0) { //每读取一次,就写入文件输出流 fos.write(bbuf,0,hasRead); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }}
package filetest;import java.io.FileWriter;import java.io.IOException;public class FileWriterTest {
public static void main(String[] args) { try { FileWriter fw=new FileWriter("E:\\Java-IO\\poem.txt"); //Windows平台的换行符是"\r\n",linux平台使用"\n" fw.write("张三\r\n"); fw.write("李四\r\n"); fw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }}

转载地址:http://eykni.baihongyu.com/

你可能感兴趣的文章
十进制字符串转十六进制字符串
查看>>
属性字符串(富文本)的使用
查看>>
cell上label的背景颜色在选中状态下改变的解决办法
查看>>
GPS定位
查看>>
地图、显示用户位置、大头针
查看>>
自定义大头针
查看>>
UIButton添加block点击事件
查看>>
利用runtime给类别添加属性
查看>>
本地推送
查看>>
FMDB的使用
查看>>
UIImage存为本地文件与UIImage转换为NSData
查看>>
[转]打印质数的各种算法
查看>>
[转]javascript with延伸的作用域是只读的吗?
查看>>
php的autoload与global
查看>>
IE不支持option的display:none属性
查看>>
[分享]mysql内置用于字符串型ip地址和整数型ip地址转换函数
查看>>
TableDnd(JQuery表格拖拽控件)应用进阶
查看>>
[转]开源中最好的Web开发的资源
查看>>
Docker上部署SpringBoot项目并推送镜像到Docker Hub上---以MacOS为例
查看>>
bibtex I was expecting a `,‘ or a `}‘ 问题解决
查看>>