1.1 流
输入流:从外部读入数据到代码中,叫做输入流,父类 InputStream
输出流:从代码当中,把内容写入文件,叫做输出流,父类 OutputStream
字节流:以字节为单位读写文件数据
字符流:以字符为单位读写文件数据
流的读入,可使用 InputStream 的 read 方法,会返回一个实际读入的长度,没有读到数据时,会返回-1.
流的写入,可使用 OutputStream 的 write 方法, 可通过 write不断写数据,直到数据末尾
1.1.1 读写字节
例子:
❤李癩❤李癩❤李癩
public class Main {
public static void main(String[] args) throws InterruptedException, ExecutionException {
try {
//输入
OutputStream outputStream = new FileOutputStream("a.properties");
String str = "Returns true if this map maps one or more keys to then" +
"specified value. More formally, returns true if and only ifn" +
"this map contains at least one mapping to a value v such thatn" +
"(value==null ? v==null : value.equals(v)). This operationn" +
"will probably require time linear in the map size for mostn" +
"implementations of the Map interface.";
byte[] tmps = str.getBytes();
int LEN = 128;
for(int start = 0; start
❤李癩❤李癩❤李癩
结果:
尿寮
尿寮
注意,使用流之后必须关闭(close())。否则会占用文件,无法对文件进行额外操作,比如无法对文件重命名。
1.1.2 完整的流家族
Java中有各种各样的流,然后书的作者给展示了一些图片(太多了,光看名字也吸收不了没什么用,这部分跳过)
JDK 5 的四个重要接口:Closeable,Flushable,Readable和Appendable
四个重要类实现情况如下:
Closeable 全部实现,所有流最终都需要关闭
Readable Reader实现(内部用字符,不是字节,所以字节流不能实现)
Flushable 写入流必然实现
Appendable Writer 实现(内部用字符,不是字节,所以字节流不能实现)
相关内容:选择 《Java核心技术 卷1》查找相关笔记
评论点赞收藏✨关注,是送给作者最好的礼物,愿我们共同学习,一起进步
如果对作者发布的内容感兴趣,可点击下方关注公众号 钰娘娘知识汇总 查看更多作者文章哦!



