2019.3.20 I/O相关(未完)
Posted lzb1234
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
}
}
}
以上是关于2019.3.20 I/O相关(未完)的主要内容,如果未能解决你的问题,请参考以下文章