字节流,字符流,转换流,序列化流

FileInputStream
输入流   字节流   节点流
1.构造方法
   public FileInputStream(String name)
   public FileInputStream(File file)
2.常用API方法
   public int read()
   public int read(byte b[])
   public int read(byte b[], int off, int len)
   public void close()  关闭方法

 JDK1.7
try() 里的对象,必须是 implements Closeable 可关闭的资源
try( 声明流对象 ){
     //流操作
}catch(IOException e){
     //发生异常
}finally{

 }

        try (
                FileInputStream fis = new FileInputStream("abc.txt");
        ){
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = fis.read(bytes))!=-1){
                //第一个参数:读取数据时使用的容器  第二个参数:起始偏移量   第三个参数:读取的字节数
                String str = new String(bytes , 0 , len);
                System.out.println(str);
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

FileOutputStream
java.io.FileOutputStream  输出流  字节流   节点流
1.构造方法
public FileOutputStream(String name)
public FileOutputStream(String name, boolean append)
public FileOutputStream(File file)
public FileOutputStream(File file, boolean append)
2.常用API方法
public void write(int b)
public void write(byte b[])
public void write(byte b[], int off, int len)
public void close()
public void flush()  刷新

字节流:更偏向于做文件拷贝、音视频播放等
字符流:更偏向于做文件的文本内容的读写

        try (
                //第二个参数  append=true 代表在原有的文本内容上进行追加
                FileOutputStream fos = new FileOutputStream("C:\\Java\\code\\abc.txt" , true);
        ){
            fos.write('\n');
            fos.write('B');
            fos.write("b".getBytes());
            fos.write('\n');
            String str1 = "hello world";
            fos.write(str1.getBytes());
            fos.write('\n');
            String str2 = "你好";
            fos.write(str2.getBytes());
            //刷新
            fos.flush();
            System.out.println("写出完成!");
        } catch (IOException e) {
            throw new RuntimeException(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值