IO流
字节流
字节输出流构造方法:
FileOutputStream
文件输出流是用于将数据写入 File 或 的输出流。
FileOutputStream(File file)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(File file, boolean append)
创建一个向指定 File 对象表示的文件中写入数据的文件输出流。
FileOutputStream(String name)
创建一个向具有指定名称的文件中写入数据的输出文件流。
FileOutputStream(String name, boolean append)
创建一个向具有指定 name 的文件中写入数据的输出文件流。
案例:
public class MyTest2 {
public static void main(String[] args) throws FileNotFoundException {
FileOutputStream fileOutputStream = new FileOutputStream("a.txt");
File file = new File("b.txt");
FileOutputStream fileOutputStream1 = new FileOutputStream(file);
}
}
FileOutputStream的三个write()写入方法
1. **public void write(int b):写一个字节 超过一个字节 砍掉前面的字节**
2. **public void write(byte[] b):写一个字节数组**
3. **public void write(byte[] b,int off,int len):写一个字节数组的一部分**
案例:
public class Test1 {
public static void main(String[] args) throws IOException {
FileOutputStream out = new FileOutputStream("a.txt");
//写入一个字节
out.write(97);
out.write(98);
out.write(99);
//写入换行符
out.write("\r\n".getBytes());
//写入一个字节数组
byte[] bytes = "安静是明白,无声了感慨。".getBytes();
out.write(bytes);
//写入换行符
out.write("\r\n".getBytes());
//写入一个字节数组的一部分
out.write(bytes,0,5);
//流使用完毕,记得要关闭。
out.close();
}
}
结果:
在相对路径下的"a.txt"文件中查看写入的内容:
abc
安静是明白,无声了感慨。
安静是明白
注意事项:
创建字节输出流对象了做了几件事情 ?
调用系统资源创建a.txt文件
创建了一个fos对象
把fos对象指向这个文件
为什么一定要close() ?
通知系统释放关于管理a.txt文件的资源
让Io流对象变成垃圾, 等待垃圾回收器对其回收
FileOutputStream的追加写入方法
案例:
public class Test2 {
public static void main(String[] args) throws IOException {
//在构造方法中,形参append:是否追加写入,true 就是追加写入,false 就是默认覆盖。
FileOutputStream out = new FileOutputStream("b.txt",true);
out.write("\r\n".getBytes());
out.write("锄禾日当午".getBytes());
out.write("\r\n".getBytes());
out.write("汗滴禾下土".getBytes());
out.write("\r\n".getBytes());
out.write("谁知盘中餐".getBytes());
out.write("\r\n".getBytes());
out.write("粒粒皆辛苦".getBytes());
out.write("\r\n".getBytes());
//释放资源
out.close();
}
}
结果:
在Test1中的"a.txt"文件中,追加写入:
abc
安静是明白,无声了感慨。
安静是明白
锄禾日当午
汗滴禾下土
谁知盘中餐
粒粒皆辛苦
流的异常处理
public class Test3 {
public static void main(String[] args) {
FileOutputStream out = null;
try {
out = new FileOutputStream("a.txt");
out.write("流的异常处理".getBytes());
//文件输入流可以为null;所以在关闭流的时候需要注意
out = null;
} catch (IOException e) {
e.printStackTrace();
//因为流在使用完毕后必须关闭,无论程序是否有异常皆需执行,所以使用finally。
} finally {
try {
//当文件输入流是null时,不需要执行关闭(close)操作,
//所以在声明close()时,需要判断。
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
FileInputStream读取数据方法
FileInputStream构造方法:
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
注意:
输入流所关联的文件如果不存在,就会报错。跟输出流正好相反。
FileInputStream的三个read()写入方法
- public void read(int b):读取一个字节 超过一个字节 砍掉前面的字节
- public void read(byte[] b):读取一个字节数组
- public void read(byte[] b,int off,int len):读取一个字节数组的一部分
案例:
public class Test4 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt");
//读取一个字节
int i = in.read();
i = in.read();
i = in.read();
i = in.read();
i = in.read();
System.out.println("单个字节读取到的数据: "+i);
//定义一个24字节长度的数组
byte[] bytes = new byte[24];
读取一个24字节的数组
int i1 = in.read(bytes);
System.out.println("将读取到的数据存入字节数组: "+i1);
//遍历打印。
for (byte aByte : bytes) {
System.out.println("遍历字节数组: "+aByte);
}
//将读取到的字节数组转换为字符串。
String s = new String(bytes, 0, i1);
System.out.println("将读取到的数据转换为字符串: "+s);
}
}
结果:
单个字节读取到的数据: 10
将读取到的数据存入字节数组: 24
遍历字节数组: -27
遍历字节数组: -82
遍历字节数组: -119
遍历字节数组: -23
遍历字节数组: -99
遍历字节数组: -103
遍历字节数组: -26
遍历字节数组: -104
遍历字节数组: -81
遍历字节数组: -26
遍历字节数组: -104
遍历字节数组: -114
遍历字节数组: -25
遍历字节数组: -103
遍历字节数组: -67
遍历字节数组: -17
遍历字节数组: -68
遍历字节数组: -116
遍历字节数组: -26
遍历字节数组: -105
遍历字节数组: -96
遍历字节数组: -27
遍历字节数组: -93
遍历字节数组: -80
将读取到的数据转换为字符串: 安静是明白,无声
Process finished with exit code 0
案例:
public class Test5 {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream("a.txt");
byte[] bytes = new byte[24];
//读取指定长度的字节数据
int read = in.read(bytes, 0, 12);
System.out.println(read);
for (byte aByte : bytes) {
System.out.println(aByte);
}
String s = new String(bytes, 0, read);
System.out.println(s);
in.close();
}
}
结果:
24
遍历字节数组: -27
遍历字节数组: -82
遍历字节数组: -119
遍历字节数组: -23
遍历字节数组: -99
遍历字节数组: -103
遍历字节数组: -26
遍历字节数组: -104
遍历字节数组: -81
遍历字节数组: -26
遍历字节数组: -104
遍历字节数组: -114
安静是
Process finished with exit code 0
文件复制操作
案例:
public class MyTest3 {
public static void main(String[] args) {
//流的异常处理
FileInputStream in=null;
FileOutputStream out=null;
try {
in= new FileInputStream("a.txt");
out= new FileOutputStream("b.txt");
//定义一个数组来充当缓冲区。
byte[] bytes = new byte[1024 * 8];
//定义一个变量,来记录实际读取到的字节个数
int len = 0;
while ((len = in.read(bytes)) != -1) {
out.write(bytes, 0, len);
out.flush();
}
}catch (IOException e){
e.printStackTrace();
}finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("复制完成");
}
}
结果:
复制完成
Process finished with exit code 0
查看相对路径下的"b.txt"文件数据,书否与"a.txt"一样。
字符流
由于字节流操作中文不是特别方便,所以,java就提供了字符流。
字符流 = 字节流 + 编码表
String类中的编码和解码问题
编码:把看得懂的变成看不懂的: String – byte[]
解码:把看不懂的变成看得懂的: byte[] – String
编码: 就是把字符串转换成字节数组
- 把一个字符串转换成一个字节数组
- public byte[] getBytes();使用平台的默认字符集将此 String编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
- public byte[] getBytes(String charsetName) 使用指定的字符集将此 String 编码为 byte 序列,并将结果存储到一个新的 byte 数组中。
-
- 解码: 把字节数组转换成字符串
- public String(byte[] bytes): 通过使用平台的默认字符集解码指定的 byte 数组,构造一个新的 String。
- public String(byte[] bytes, String charsetName) 通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String。
- 使用什么字符集进行编码,那么就是使用什么字符集进行解码
转换流OutputStreamWriter的使用
OutputStreamWriter的构造方法
OutputStreamWriter(OutputStream out):根据默认编码(UTF—8)把字节流的数据转换为字符流
OutputStreamWriter(OutputStream out,String charsetName):根据指定编码把字节流数据转换为字符流
字符流的5种写数据的方式
public void write(int c) 写一个字符
public void write(char[] cbuf) 写一个字符数组
public void write(char[] cbuf,int off,int len) 写一个字符数组的 一部分
public void write(String str) 写一个字符串
public void write(String str,int off,int len) 写一个字符串的一部分
案例:
package org.westos.demo5;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class Test5 {
public static void main(String[] args) throws IOException {
OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream("c.txt", true));
//一个字符写入
out.write('a');
out.write('b');
out.write('c');
out.write("\t\n");
//一次写入一个字符数组
out.write(new char[]{'d','e','f','g'});
out.write("\t\n");
//写入字符数组从指定起始索引和固定长度的字符。
out.write(new char[]{'d','e','f','g'},0,2);
out.write("\t\n");
//写入一个字符串
out.write("hijklmi");
out.write("\t\n");
//写入一个指定起始索引和长度的字符串
out.write("opqrst",0,5);
out.flush();
out.close();
}
}
结果:
打开相对路径下的"c.txt"文件查看写入数据:
abc
defg
de
hijklmi
opqrs
转换流InputStreamReader的使用
InputStreamReader的构造方法
InputStreamReader(InputStream is):用默认的编码(GBK)读取数据
InputStreamReader(InputStream is,String charsetName):用指定的编码读取数据
字符流的2种读数据的方式
public int read() 一次读取一个字符,如果没有读到 返回-1
public int read(char[] cbuf) 一次读取一个字符数组 如果没有读到 返回-1
案例:
package org.westos.demo5;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
public class Test6 {
public static void main(String[] args) throws IOException {
//"c.txt"文件中的数据“abcdefgdehijklmiopqrsabcdefgdehijklmiopqrs”;
InputStreamReader in = new InputStreamReader(new FileInputStream("c.txt"));
//一次读取一个字符
int i = in.read();
System.out.println(i);
i = in.read();
System.out.println(i);
i = in.read();
System.out.println(i);
//一次读取一个字符数组,返回值是读取到的有效的字符个数
char[] chars = new char[12];
int i1 = in.read(chars);
for (char aChar : chars) {
System.out.println("读取字符数组: "+aChar);
}
}
}
结果:
97
98
99
读取字符数组: d
读取字符数组: e
读取字符数组: f
读取字符数组: g
读取字符数组: d
读取字符数组: e
读取字符数组: h
读取字符数组: i
读取字符数组: j
读取字符数组: k
读取字符数组: l
读取字符数组: m
Process finished with exit code 0
使用字符流复制文件
案例:
package org.westos.demo5;
import java.io.*;
public class Test7 {
public static void main(String[] args) {
InputStreamReader reader=null;
OutputStreamWriter writer=null;
try {
reader = new InputStreamReader(new FileInputStream("c.txt"));
writer = new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\b.txt"));
char [] chars =new char[1000];
int len=0;
while ((len=reader.read(chars))!=-1){
writer.write(chars,0,len);
writer.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (reader != null) {
reader.close();
}
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
结果:
将相对路径下的"c.txt"文件复制到绝对路径"C:\\Users\\Administrator\\Desktop\\b.txt"文件下