IO流(1)--字节流
来源:互联网 发布:mac磁盘越来越小 编辑:程序博客网 时间:2024/06/06 01:52
IO流分类:
流向:
输入流
输出流
数据类型:
1.字节流
字节输入流
字节输出流
2.字符流
字符输入流
字符输出流
字节输入流:
具体操作步骤:
字节输入流操作步骤:
A:创建字节输入流对象
FileInputStream fis = new FileInputStream(“a.txt”);
B:调用方法读取数据
一次读取一个字节:read()
C:释放资源
fis.close
& FileOutputStream fos = new FileOutputStream(“fos.txt”);
请问上面这个操作做了哪几件事情?
1.创建了一个文件输出流fos,指向文件a.txt
2.创建了a.txt这个文件
输出演示:
public class OutputStreamExercise { /** * 需求:请用字节流往一个文本文件中写一句话:"helloworld"。 * @throws IOException */ public static void main(String[] args) throws IOException { FileOutputStream fos = new FileOutputStream("a.txt"); //调用输出流的写数据的方法给文件中写数据 String s="helloworld"; byte[] bs = s.getBytes(); fos.write(bs); //释放资源,关流操作 fos.close(); }
输入演示:
public class InputStreamExercise { /** * A:创建字节输入流对象 FileInputStream fis = new FileInputStream("a.txt"); * B:调用方法读取数据 一次读取一个字节:read() * C:释放资源 fis.close * @throws IOException */ public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("a.txt"); int a; while((a=fis.read())!=-1){ System.out.println((char)a); } fis.close(); }}
练习:复制一个图片文件
public class CopyExercise { /** * 复制一个图片 * @throws IOException */ public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("E://KanKan//W020080220354931747530.jpg"); FileOutputStream fos = new FileOutputStream("c.jpg"); //一次读写一个字节数组 byte[] bys = new byte[1024]; int len; while((len=fis.read(bys))!=-1){ fos.write(bys, 0, len); } fis.close(); fos.close(); }
0 0
- IO流(1)--字节流
- IO流(字节流)
- IO流字节流
- IO流字节流
- IO流-字节流
- IO流--字节流
- IO之字节流
- IO字节流
- java14-IO字节流
- IO之字节流
- IO之字节流
- IO一:字节流
- IO之字节流
- IO 字节流学习
- Java IO 字节流
- IO字节流
- IO字节流
- IO字节流总结
- [leetcode:python]38.Count and Say
- 如何解决 Notice: Undefined index: submit in D:\wamp\www\bbs\add.php on line 9
- 建站工具
- 【tyvj4089】n^2(dp)
- Linux之使用MogileFS分布式文件系统并使用nginx实现反向代理
- IO流(1)--字节流
- Android Studio中JNI NDK开发(三)
- 常见dom树操作
- Spring第四篇【Intellij idea环境下、Struts2和Spring整合】
- android6.0 读取storage文件夹需要手动检测读取权限
- zcmu 1018
- 数据结构之顺序表——c(&为取值符)
- woj658 华师2017校赛A题 队列
- java面试五