IO流
IO流的作用:就是可以对文件或者网络中的数据进行读、写的操作
- 把数据从磁盘、网络中读取到程序中来,用到的是输入流。
- 把程序中的数据写入磁盘、网络中,用到的是输出流。
- 简单记:输入流(读数据)、输出流(写数据)
IO流分为两大派系:
1.字节流:字节流又分为字节输入流、字节输出流
2.字符流:字符流由分为字符输入流、字符输出流
例题:
第一步:创建FileInputStream文件字节输入流管道,与源文件接通。
第二步:调用read()方法开始读取文件的字节数据。
第三步:调用close()方法释放资源
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// 1、创建文件字节输入流管道,与源文件接通。
FileInputStream stream = new FileInputStream("文件路径");
// 2、开始读取文件的字节数。
// public int read(): 每次读一个字节返回,如没有数了,返回-1.
// int i = stream.read();
// System.out.println((char) i);
// int read = stream.read();
// System.out.println((char) read);
//开始读取文件的字节数。
//固定写法!!!
int b;
while ((b = stream.read())!=-1){
System.out.print((char) b);
}
//使用完毕后,必须结束
stream.close();
}
}
FilelnputStream每次读多个字节
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
// 1、创建文件字节输入流管道,与源文件接通。
FileInputStream stream = new FileInputStream("文件路径");
//2、开始读取文件中的字节数:每次读取多个字节
// public int read(byte b[]) throws IOException
// 每次读取多个字节到字节数组中去,返回读取的字节数量,读取完毕会返回-1
byte[] bytes = new byte[1024];//读取1024次(规范)
int len;
while ((len=stream.read(bytes))!=-1){
String s = new String(bytes, 0, len);
System.out.println(s);
}
//使用完毕后,必须结束
stream.close();
}
}
一下子打印所有的
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
FileInputStream stream = new FileInputStream("文件路径");
byte[] bytes = stream.readAllBytes();
System.out.println(new String(bytes));
stream.close();
}
}
FileOutputStream写字节
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test02 {
public static void main(String[] args) throws IOException {
// 1、创建一个字节输出流管道与月标文件接通。
// public FileOutputStream(String name, boolean append ): append=true,代表追加
//没有 true 代表写进去的 就会覆盖原来的
FileOutputStream stream = new FileOutputStream("文件路径", true);
//write()方法:写入数据
stream.write('q');
stream.write('w');
stream.write('e');
stream.write('r');
//关闭
stream.close();
}
}
字节流复制文件
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test03 {
public static void main(String[] args) throws IOException {
// 需求: 复制照片。把5.png照片复制出一个新的图片6.png
//一:创建字节输入流,读取数据
FileInputStream s = new FileInputStream("文件路径");
// 二:创建字节输出流,写入数据
// 后面加true 覆盖
FileOutputStream s1 = new FileOutputStream("文件路径", true);
// 三: 指定一个字节缓冲数组,一次读取1024个字节
byte[] bytes = new byte[1024];
// 四:一次读取了多个个字节
int len;
while ((len = s.read(bytes)) != -1){
// 五: 1.写入字节 2.字节数组起始位置 3.多少个数据
s1.write(bytes,0,len);
}
//关闭流
s.close();
s1.close();
}
}
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test01 {
public static void main(String[] args) throws IOException {
FileInputStream f1 = new FileInputStream("文件路径");
FileOutputStream f2 = new FileOutputStream("文件路径");
byte[] bytes = new byte[1024];
int len;
while ((len=f1.read(bytes))!=-1){
f2.write(bytes,0,len);
}
f1.close();
f2.close();
}
}
IO 资源释放 关流
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/**
*使用try...catch...finally语句来处理
*/
public class TryCatchDemo {
public static void main(String[] args) throws IOException {
// try-catch-finally改造图片复制
// ctrl + alt + T:为包赛住的代码,生成try-catch-finally
FileInputStream f1 = null;
FileOutputStream f2 = null;
try {
// 1、创建一个字节输入流管道与源文件接通
f1 = new FileInputStream("文件路径");
// 2、创建一个字节输出流管道与目标文件接通。
f2 = new FileOutputStream("文件路径");
// 3、创建一个字节数组,负责转移字节数据。
byte[] bytes = new byte[1024];
// 4、从字节输入流中读取字节数据,写出去到字节输出流中。读多少写出去多少。
int len;// 记住每次读取了多少个字节。
while ((len=f1.read(bytes))!=-1){
f2.write(bytes,0,len);
}
System.out.println("复制完成!!");
} catch (IOException e) {
e.printStackTrace();
}
//直接在里面写f1.close()和f2.close()然后ALT+enter选第三个 在点ctrl+alt+t选try/catch/finally
finally {
// 释放资源的操作
try {
if (f1 != null) {
f1.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (f2 != null) {
f2.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* 目标:掌握释放资源的方式:try-with-resource
*/
public class TryCatchTest {
public static void main(String[] args) {
try (
// 1、创建一个字节输入流管道与源文件接通
FileInputStream f1 = new FileInputStream("//图片路径");
// 2、创建一个字节输出流管道与目标文件接通。
FileOutputStream f2 = new FileOutputStream("//路径")
) {
// 3、创建一个字节数组,负责转移字节数据。
byte[] bytes = new byte[1024];
// 4、从字节输入流中读取字节数据,写出去到字节输出流中。读多少写出去多少。
int len;// 记住每次读取了多少个字节。
while ((len = f1.read(bytes)) != -1) {
f2.write(bytes, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
IO流(二)
FileReader读取文件的步骤
- 第一步:创建FileReader对象与要读取的源文件接通
- 第二步:调用read()方法读取文件中的字符
- 第三步:调用close()方法关闭流
import java.io.FileReader;
import java.io.IOException;
public class Test01 {
/**
* 目标:掌握文件字符输入流。
*/
public static void main(String[] args) throws IOException {
try (
// 1、创建一个文件字符输入流管道与源文件接通
final FileReader f1 = new FileReader("文件路径");
) {
// 2、一个字符一个字符的读(性能较差)
// int c; // 记住每次读取的字符编号。
// while ((c = fr.read()) != -1){
// System.out.print((char) c);
// }
// 每次读取一个字符的形式,性能肯定是比较差的。
// 3、每次读取多个字符。(性能是比较不错的!)
char[] chars = new char[1024];
int len;// 记住每次读取了多少个字符。
while ((len = f1.read(chars)) != -1) {
// 读取多少倒出多少
final String s = new String(chars, 0, len);
System.out.println("打印完成:" + s);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
FileWriter往文件中写字符数据
- 第一步:创建FileWirter对象与要读取的目标文件接通
- 第二步:调用write(字符数据/字符数组/字符串)方法读取文件中的字符
- 第三步:调用close()方法关闭流
import javax.imageio.IIOException;
import java.io.FileWriter;
import java.io.IOException;
public class Test02 {
/**
* 目标:掌握文件字符输出流:写字符数据出去
*/
public static void main(String[] args) throws IOException {
FileWriter writer = null;
//
//
try {
// 0、创建一个文件字符输出流管道与目标文件接通。
// 覆盖管道
// Writer fw = new FileWriter("文件路径");
// 追加数据的管道
//后面加true 覆盖掉
writer = new FileWriter("文件路径",true);
// 1、public void write(int c):写一个字符出去
writer.write('P');
writer.write('a');
writer.write('u');
writer.write('l');// 写一个字符出去
writer.write("\r\n");// 换行
// 3、public void write(String c ,int pos ,int len):写字符串的一部分出去
String s = "Paul喜欢喝生椰拿铁!";
writer.write(s, 0, 11);
//刷新数据 要么写close() 要么写flush()
writer.flush();
} catch(IIOException e){
throw new RuntimeException(e);
}finally{
// if (writer != null) {
// try {
// writer.close();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
}
}
}
复制文件
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class Test {
public static void main(String[] args) throws IOException {
try (
FileReader f1 = new FileReader("需要赋值文件路径");
FileWriter f2 = new FileWriter("复制到哪里的文件路径")
) {
char[] chars = new char[1024];
int len;
while ((len = f1.read(chars)) != -1) {
f2.write(chars,0,len);
System.out.println("复制完成!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}