2019.3.20 I/O相关
I/O
相关简介
什么是I/O?
IO,即Input (输入)和Output (输出)的首字母缩写。什么是流?
流(Stream)是抽象概念,它代表任何有能力产出数据的数据源对象或者是与能力接收数据的接收端对象,“流”屏蔽了实际的I/O设备中处理数据的细节。流的种类
- 1.字符流
- 2.字节流
字符流
字符输出流
输出流是将内容“写”到外部文件
public void test1() {
FileWriter fileWriter = null;
try {
// ./代表当前目录
fileWriter = new FileWriter("./aaa.txt", true);
fileWriter.write("冰川双子天下第一"); //括号里是想输出的东西
fileWriter.write("mskk天下第一");
fileWriter.write("彩千圣天下第一");
// fileWriter.close(); //必须要关了流才能把东西写进去,但是要在finally里写,见以下代码
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWriter != null) {
fileWriter.close(); //先try catch 再surround
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流
输入流是将内容“读”进内部文件
- read方法的过程
- 1.从文件中读取的内容会依次放入buffer数组中
- 2.要么读满数组,要么文件结束
- 3.如果文件已经到达最后的位置,就会返回-1
public void test2() {
FileReader fileReader = null;
try {
fileReader = new FileReader("./aaa.txt");
//每次读取的最大长度
char[] buffer = new char[8];
// int len = fileReader.read(buffer);//len是字符数
// System.out.println(len);
// System.out.println(new String(buffer, 0, len));
int len = 0;
while ((len = fileReader.read(buffer)) != -1) {
System.out.println(new String(buffer, 0, len));//参数意义:使用buffer数组,从第0个开始,读取len长度
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习:将aaa.txt复制一份生成一个bbb.txt
public void test3() {
FileWriter fileWriter1 = null;
FileReader fileReader1 = null;
try {
fileWriter1 = new FileWriter("./bbb.txt");
fileReader1 = new FileReader("./aaa.txt");
char[] buffer = new char[8];
int len = 0;
while ((len = fileReader1.read(buffer)) != -1) {
fileWriter1.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter1 != null) {
try {
fileWriter1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileReader1 != null) {
try {
fileReader1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
带缓冲区的字符输出流
public void test4() {
FileWriter fileWriter2 = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter2 = new FileWriter("./ccc.txt");
//bufferedWriter对象并不是一个真正的流
//他只是在fileWriter对象的基础上添加了新的逻辑
bufferedWriter = new BufferedWriter(fileWriter2);//括号里一定要填一个流
bufferedWriter.write("写一行");
bufferedWriter.newLine(); //换行
//将缓冲区的内容写到文件中,清空缓冲区
//方便后续继续写入
bufferedWriter.flush(); //刷新缓冲区
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter != null) {
bufferedWriter.close(); //当使用缓冲区时,只关闭缓冲区就可以
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
带缓冲区的字符输入流
public void test5() {
FileReader fileReader2 = null;
BufferedReader bufferedReader = null;
try {
fileReader2 = new FileReader("./ccc.txt");
bufferedReader = new BufferedReader(fileReader2);
String line = null;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
练习:使用缓冲区将ccc.txt复制到ddd.txt
public void test6() {
FileReader fileReader3 = null;
BufferedReader bufferedReader1 = null;
FileWriter fileWriter3 = null;
BufferedWriter bufferedWriter1 = null;
try {
fileReader3 = new FileReader("./ccc.txt");
bufferedReader1 = new BufferedReader(fileReader3);
fileWriter3 = new FileWriter("./ddd.txt");
bufferedWriter1 = new BufferedWriter(fileWriter3);
String line = null;
while ((line = bufferedReader1.readLine()) != null) {
bufferedWriter1.write(line);
bufferedWriter1.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferedWriter1 != null) {
bufferedWriter1.close();
}
if (bufferedReader1 != null) {
bufferedReader1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节流
字节输出流
public void test7() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream("./eee.txt");
fileOutputStream.write("摩卡兰天下第一".getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节输入流
public void test8() {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("./eee.txt");
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
System.out.println(new String(bytes));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习:用字节流复制图片
public void test9() {
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
try {
fileOutputStream = new FileOutputStream("G:\\破烂\\666.jpg"); //此处是新图片地址,可以新建可以覆盖
fileInputStream = new FileInputStream("G:\\破烂\\11111.jpg"); //此处是想要复制的图片的地址
int len = 0;
byte[] bytes = new byte[1024];
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
练习:用字节流复制mp3
public void test10() {
FileOutputStream fileOutputStream = null;
FileInputStream fileInputStream = null;
try {
fileOutputStream = new FileOutputStream("G:\\破烂\\有你的江湖.mp3");
fileInputStream = new FileInputStream("G:\\破烂\\DMYoung,岚aya - 有你的江湖-TVsize.mp3");
int len = 0;
byte[] bytes = new byte[10240000];
while ((len = fileInputStream.read(bytes)) != -1) {
fileOutputStream.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileInputStream != null) {
fileInputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲区字节输出流
public void test11() {
BufferedOutputStream bufferedOutputStream = null;
FileOutputStream fileOutputStream= null;
try {
fileOutputStream = new FileOutputStream("./xxx.txt");
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
bufferedOutputStream.write("花音千圣天下第一".getBytes());
bufferedOutputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (bufferedOutputStream != null) {
bufferedOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
缓冲区字节输入流
public void test12(){
FileInputStream fileInputStream = null;
BufferedInputStream bufferedInputStream = null;
try {
fileInputStream = new FileInputStream("./xxx.txt");
bufferedInputStream = new BufferedInputStream(fileInputStream);
byte[] bytes = new byte[1024];
int len = 0;
while ((len = bufferedInputStream.read(bytes)) != -1){
System.out.println(new String(bytes,0,len));
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
- 字符流和字节流的区别
- 1.处理对象不同
- 2.处理数据的单位不同,字符流操作char,字节流操作byte(Java中一个中文占3个字节)
序列化
序列化
public void test13(){
Student student = new Student();
student.setName("刘子博");
student.setAge(22);
student.setId("12345678");
//1.类结构要不要保存? 不需要
//2.数据要不要? 需要
//3.变量名和创建位置要不要? 不需要
//保存一个对象最重要的就是保存数据
ObjectOutputStream oos = null;
FileOutputStream fos = null;
try {
fos = new FileOutputStream("./stu.out");
oos = new ObjectOutputStream(fos);
//将一个对象写入本地
oos.writeObject(student);
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
反序列化
public void test14(){
ObjectInputStream ois = null;
FileInputStream fis = null;
try {
fis = new FileInputStream("stu.out");
ois = new ObjectInputStream(fis);
Object o = ois.readObject();
System.out.println(o);
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
工程
链接:https://pan.baidu.com/s/1BwbDrxsoAkLMAvzQwg6kMg
提取码:yhm0
2019.3.20 I/O相关的更多相关文章
- 【盛派周三分享-2019.2.20】开放分享内容,本期主题:《SCF、DDD及相关架构思想讨论》
“周三分享”是盛派网络约定的每周三晚上定时举办的内部分享活动,活动主要由技术人员分享各方面的技术主题,并由所有参与者围绕主题进行讨论.除技术话题外,也可能涉及到相关的设计.财税.金融.政策等方面的延伸 ...
- Python + Anaconda + vscode环境重装(2019.4.20)
目录 卸载程序 安装Ananconda 检查系统环境变量 更换国内镜像源 设置VS CODE 用户配置及工作环境配置 @(Python + Anaconda + vscode环境重装) 工程目录的使用 ...
- 2019.03.20 mvt,Django分页
MVT模式 MVT各部分的功能: M全拼为Model,与MVC中的M功能相同,负责和数据库交互,进行数据处理. V全拼为View,与MVC中的C功能相同,接收请求,进行业务处理,返 ...
- 2019.4.18 HTML + CSS相关整理
目录 标签 块标签 行标签 行块转化 嵌套规则 css引入方式 行间样式 内部引入 外部引入 选择器 基础选择器 组合选择器 盒模型 css样式 字体属性 设置字体的大小 设置字体的粗细 设置字体的风 ...
- 【2019.8.20 NOIP模拟赛 T3】小X的图(history)(可持久化并查集)
可持久化并查集 显然是可持久化并查集裸题吧... 就是题面长得有点恶心,被闪指导狂喷. 对于\(K\)操作,直接\(O(1)\)赋值修改. 对于\(R\)操作,并查集上直接连边. 对于\(T\)操作, ...
- 【2019.7.20 NOIP模拟赛 T1】A(A)(暴搜)
打表+暴搜 这道题目,显然是需要打表的,不过打表的方式可以有很多. 我是打了两个表,分别表示每个数字所需的火柴棒根数以及从一个数字到另一个数字,除了需要去除或加入的火柴棒外,至少需要几根火柴棒. 然后 ...
- 2019.6.20 校内测试 NOIP模拟 Day 1 分析+题解
这次是zay神仙给我们出的NOIP模拟题,不得不说好难啊QwQ,又倒数了~ T1 大美江湖 这个题是一个简单的模拟题. ----zay 唯一的坑点就是打怪的时候计算向上取整时,如果用ceil函数一 ...
- 【2019.3.20】NOI模拟赛
题目 这里必须标记一下那个傻逼问题,再不解决我人就没了! 先放一个 $T3$ $20$ 分暴力 #include<bits/stdc++.h> #define rep(i,x,y) for ...
- 2019.10.20 csp-s模拟测试 lrd试题 反思总结
赶进度赶进度,丢个代码两三句备注一下完事了. day1: 前面两道题没实际写代码怕印象不深所以描述一下大意. T1: 题目大意:给出两个数&.|.^的结果(可能只给出其中某一项或者某两项),求 ...
随机推荐
- atom markdown报错:AssertionError: html-pdf: Failed to load PhantomJS module.
今天安装markdown-pdf之后运行的时候报错: AssertionError: html-pdf: Failed to load PhantomJS module. You have to se ...
- MySQL性能调优与架构设计——第3章 MySQL存储引擎简介
第3章 MySQL存储引擎简介 3.1 MySQL 存储引擎概述 MyISAM存储引擎是MySQL默认的存储引擎,也是目前MySQL使用最为广泛的存储引擎之一.他的前身就是我们在MySQL发展历程中所 ...
- 换零钞——第九届蓝桥杯C语言B组(国赛)第一题
原创 标题:换零钞 x星球的钞票的面额只有:100元,5元,2元,1元,共4种.小明去x星旅游,他手里只有2张100元的x星币,太不方便,恰好路过x星银行就去换零钱.小明有点强迫症,他坚持要求200元 ...
- Recyclerview添加头布局和尾布局,点击效果
简介: 本篇博客主要包括recyclerview添加多种布局以及添加头布局和尾布局,还有item点击事件 思路: 主要重写Recyclerview.Adapter中的一些方法 1.public int ...
- 一套最全的JavaScript 语言基础知识点总结(思维导图10张)
1.DOM基础操作 2.数组基础 3.函数基础 4.运算符 5.流程控制语句 6.正则表达式 7.字符串函数 8.数据类型 9.变量 10.window对象
- python merge、concat合并数据集
数据规整化:合并.清理.过滤 pandas和python标准库提供了一整套高级.灵活的.高效的核心函数和算法将数据规整化为你想要的形式! 本篇博客主要介绍: 合并数据集:.merge()..conca ...
- 趣图:快下班了,剩一个bug,修复一下再走
趣图:当我给老板展示我修复了那个 bug 时 趣图:当我以为这是最后一个Bug时……
- 【BZOJ2159】Crash的文明世界 斯特林数+树形dp
Description Crash 小朋友最近迷上了一款游戏--文明5(Civilization V).在这个游戏中,玩家可以建立和发展自己的国家,通过外交和别的国家交流,或是通过战争征服别的国家.现 ...
- loj#6436. 「PKUSC2018」神仙的游戏(NTT)
题面 传送门 题解 一旦字符串踏上了通配符的不归路,它就永远脱离了温暖的字符串大家庭的怀抱 用人话说就是和通配符扯上关系的字符串就不是个正常的字符串了比如说这个 让我们仔细想想,如果一个长度为\(le ...
- 【guava】字符串操作
一,Strings类 public void testStrings(){ Strings.isNullOrEmpty("");//返回true Strings.nullToEmp ...