IO流
流的分类
- 操作数据单位:字节流,字符流
- 数据的流向:输入流,输出流
- 流的角色:节点流(文件流),处理流
异常处理
所以IO流异常都用 try-catch-finally
常用重点流
节点流
FileReader()
用字符方式读取文件
//异常处理:为了保证流资源一定可以执行关闭操作,需要使用try-catch-finally处理
public class FileReader1 {
public static void main(String[] args){
FileReader fileReader = null;
try {
//1.指明要操作的文件
File file = new File("hello.txt");
// System.out.print(file.getAbsolutePath());
//2.指定具体的流
fileReader = new FileReader(file);
//3.数据读入
// read() 返回一个字符,如果达到文件末尾,返-1
int data;
while((data = fileReader.read()) != -1){
System.out.print((char)data);
}
} catch (Exception e) {
}finally {
//4.关闭流
try {
if(fileReader != null) {
fileReader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileWriter()
从内存中写出数据到硬盘的文件中
对应的file可以不存在,会自动创建
构造器:FileWriter(file,true) 第二个参数为true表示追加内容,不会覆盖原来内容,不写默认为false
public class FileWriter1 {
public static void main(String[] args) throws IOException {
//1.创建file类对象,指明读入和写出文件
File fileOld = new File("hello.txt");
File fileNew = new File("hello1.txt");
//2,创建输入流和输出流对象
FileReader in = new FileReader(fileOld);
FileWriter out = new FileWriter("hello1.txt");
//3,数据的读入和写出操作
char[] cbuf = new char[5];
int len; //记录每次读入cbuf数组的个数
while((len = in.read(cbuf)) != -1) {
out.write(cbuf, 0, len);
System.out.println("写入成功");
}
//4.关闭流资源
out.close();
in.close();
}
}
FileInputStream()和 FileOutputStream()
public class FileInputStream1 {
public static void main(String[] args) {
FileInputStream in = null;
FileOutputStream out = null;
try {
File fileOld = new File("1133.jpg");
File fileNew = new File("1133333.jpg");
in = new FileInputStream(fileOld);
out = new FileOutputStream("1133333.jpg");
byte[] bbuf = new byte[5];
int len;
while((len = in.read(bbuf)) != -1) {
out.write(bbuf, 0, len);
System.out.println("写入成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
out.close();
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
缓存流
- 作用:提高流的读取,写入速度
- 原因:内部提供了一个缓冲区
BufferedInputStream 和 BufferedOutputStream 的使用
public class BufferOutputStream1 {
public static void main(String[] args) {
//2.2 造缓冲流
BufferedInputStream bufIn = null;
BufferedOutputStream bufOut = null;
try {
//1.造文件
File fileOld = new File("1133.jpg");
File fileNew = new File("11.jpg");
//2.1 造节点流
FileInputStream in = new FileInputStream(fileOld);
FileOutputStream out = new FileOutputStream(fileNew);
bufIn = new BufferedInputStream(in);
bufOut = new BufferedOutputStream(out);
//3.读取,写入
byte[] buffer = new byte[5];
int len;
while((len = bufIn.read(buffer)) != -1) {
bufOut.write(buffer, 0, len);
System.out.println("成功");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
//4.资源关闭
try {
if(bufOut != null) {
bufOut.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(bufIn != null) {
bufIn.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
BufferedReader 和 BufferedWriter 的使用
public class BufferedReader1 {
public static void main(String[] args) throws IOException {
//指明文件 造流
BufferedReader bIn = new BufferedReader(new FileReader(new File("ww.txt")));
BufferedWriter bOut = new BufferedWriter(new FileWriter(new File("ee.txt")));
//读入,读出
// char[] cbuf = new char[1024];
// int len;
// while((len = bIn.read(cbuf)) != -1) {
// bOut.write(cbuf, 0, len);
// }
//方式2
String data;
while((data = bIn.readLine()) != null) {
bOut.write(data);
bOut.newLine(); //换行
}
bOut.close();
bIn.close();
}
}
转换流
- InputStreamReader:将一个字节的输入流转换为字符的输入流。对应解码。
OutputStreamWriter:将一个字符的输出流转换为字节的输出流。对应编码。 - 作用:提供字节和字符流之间的转换。
- 解码:字节,字节数组—>字符数组,字符串
编码:字符数组,字符串—>字节,字节数组 - 文件编码的方式 ,决定了解析时使用的字符集
InputStreamReader
public class InputStreamReader1 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("ee.txt");
//参数2指明了字符集,具体使用哪个字符集,取决于文件ee.txt保持时用的字符集
// InputStreamReader inputStreamReader = new InputStreamReader(in,"utf-8");
//不写使用系统默认字符集
InputStreamReader inputStreamReader = new InputStreamReader(in);
char[] ch = new char[20];
int len;
while((len = inputStreamReader.read(ch)) != -1) {
String string = new String(ch,0,len);
System.out.println(string);
}
inputStreamReader.close();
}
}
OutputStreamWriter 和 InputStreamReader
//解码:字节,字节数组--->字符数组,字符串
//编码:字符数组,字符串--->字节,字节数组
public class InputStreamReaderWriter {
public static void main(String[] args) throws IOException {
//1.造文件,造流
FileInputStream in = new FileInputStream("ee.txt");
FileOutputStream out = new FileOutputStream("aa.txt");
InputStreamReader isr = new InputStreamReader(in);
OutputStreamWriter osw = new OutputStreamWriter(out,"utf-8");
//2.读写操作
char[] ch = new char[20];
int len;
while((len = isr.read(ch)) != -1) {
osw.write(ch, 0, len);
}
//3.关闭资源
osw.close();
isr.close();
}
}