Java中的IO流
Java中的4大IO抽象类
InputStream/OutputStream 为字节输入输出流
Reader/Writer 为字符输入输出流
InputStream
OutputStream
Reader
Writer
Java中流的概念细分
二进制文件(图片、影音)用字节流
文本信息用字符流
IO流的体系
练手案例
public class FirstDemo {
public static void main(String[] args) throws IOException {
// 创建字节输入流对象
InputStream fileInputStream = new FileInputStream("D:\\桌面\\新建文本文档.txt");
int s1;
while ((s1 = fileInputStream.read())!=-1){
System.out.println(s1);
System.out.println((char)s1);
}
fileInputStream.close();
}
}
File类
针对文件操作的方法
针对目录的方法
常用流对象
文件字节流
实现文件拷贝
public class Test {
public static void main(String[] args) throws IOException {
InputStream stream = new FileInputStream("D:\\桌面\\a.png");
OutputStream outputStream = new FileOutputStream("D:\\桌面\\aa.jpg");
int tem = 0;
while((tem = stream.read()) != -1){
System.out.println(tem);
outputStream.write(tem);
}
outputStream.flush();
stream.close();
outputStream.close();
}
}
通过缓冲区提高读写效率
方式一:
public class Test {
public static void main(String[] args) throws IOException {
InputStream stream = new FileInputStream("D:\\桌面\\a.png");
OutputStream outputStream = new FileOutputStream("D:\\桌面\\aa.jpg");
int tem = 0;
byte[] buffer = new byte[1024];
while((tem = stream.read(buffer)) != -1){ // 数组装满返回长度,装不满返回-1
outputStream.write(buffer,0,tem);// 将buffer中0到tem的数据写入
}
outputStream.flush();
stream.close();
outputStream.close();
}
}
方式二:
public class Test {
public static void main(String[] args) throws IOException {
InputStream stream = new FileInputStream("D:\\桌面\\a.png");
OutputStream outputStream = new FileOutputStream("D:\\桌面\\aa.png");
int tem = 0;
byte[] buffer = new byte[stream.available()]; // stream.available()是该文件的预估大小
stream.read(buffer);
outputStream.write(buffer);
outputStream.flush(); // 将内存转入磁盘
stream.close();
outputStream.close();
}
}
通过字节缓冲流提高效率
缓冲流为处理流
缓冲区中,默认有一个数组,默认为8192
public class Test {
public static void main(String[] args) throws IOException {
FileInputStream fis = new FileInputStream("D:\\桌面\\a.png");
FileOutputStream fos = new FileOutputStream("D:\\桌面\\aa.png");
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int tem = 0;
while ((tem = bis.read()) != -1){
bos.write(tem);
}
bos.flush();
bos.close();
bis.close();
fis.close();
fos.close();
}
}
定义文件拷贝工具类
public static void copyFile(String src,String des) throws IOException {
FileInputStream fis = new FileInputStream(src);
FileOutputStream fos = new FileOutputStream(des);
BufferedInputStream bis = new BufferedInputStream(fis);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int temp = 0;
while ((temp = bis.read()) != -1){
bos.write(temp);
}
bos.flush();
bis.close();
bos.close();
fis.close();
fos.close();
}
文件字符流
文件字节流可以处理所有的文件,但是处理文本文件时,我们也可以使用文件字符流,他以字符为单位进行操作。
实现文件拷贝
public class readerwriter {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\桌面\\a.txt");
FileWriter fileWriter = new FileWriter("D:\\桌面\\aa.txt");
int temp = 0;
while ((temp = fileReader.read()) != -1){
fileWriter.write(temp);
}
fileWriter.write("\r\n");// 回车换行
fileWriter.write("hhhhhhhhhhhhhhhh");
fileWriter.flush();
FileWriter fileWriter2 = new FileWriter("D:\\桌面\\aa.txt",true); // 不加true默认覆盖,加了true为在尾部添加
fileWriter2.write("houyiming");
fileWriter2.flush();
fileReader.close();
fileWriter.close();
fileWriter2.close();
}
}
缓冲区提高效率
public class readerwriter {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\桌面\\a.txt");
FileWriter fileWriter = new FileWriter("D:\\桌面\\aa.txt");
int temp = 0;
char[] buffer = new char[1024];
while ((temp = fileReader.read(buffer)) != -1){
fileWriter.write(buffer,0,1024);
}
fileWriter.flush();
fileReader.close();
fileWriter.close();
}
}
字符缓冲流
实现文件拷贝:
public class readerwriter {
public static void main(String[] args) throws IOException {
FileReader fileReader = new FileReader("D:\\桌面\\a.txt");
FileWriter fileWriter = new FileWriter("D:\\桌面\\aa.txt");
BufferedReader bufferedReader = new BufferedReader(fileReader);
BufferedWriter bufferedWriter = new BufferedWriter(fileWriter);
String temp = "";
while ((temp = bufferedReader.readLine()) != null){
bufferedWriter.write(temp);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
fileReader.close();
fileWriter.close();
}
}
转换流
public class ConvertStream {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
String s = bufferedReader.readLine();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(System.out));
bufferedWriter.write(s);
bufferedWriter.flush();
bufferedReader.close();
bufferedWriter.close();
}
}
字节流读取文件并添加行号
public class ConvertStream {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\桌面\\a.txt")));
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\桌面\\aa.txt")));
String s = "";
int i = 1;
while ((s = bufferedReader.readLine()) != null){
bufferedWriter.write(i+++": "+s);
bufferedWriter.newLine();
}
bufferedWriter.flush();
bufferedWriter.close();
bufferedWriter.close();
}
}
字符输出流
节点流。
加行号:
public class ConvertStream {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\桌面\\a.txt")));
PrintWriter printWriter = new PrintWriter("D:\\桌面\\aa.txt");
String s = "";
int i = 1;
while ((s = bufferedReader.readLine()) != null){
printWriter.println(i+++": "+s);
}
printWriter.close();
bufferedReader.close();
}
}
字节数组流
public static void main(String[] args) throws IOException {
byte[] bytes = "abcdefg".getBytes();
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
StringBuilder stringBuilder = new StringBuilder();
//stringBuilder可以使用append的方法不断地添加元素创建字符串
int temp = 0;
while ((temp = byteArrayInputStream.read()) != -1){
stringBuilder.append((char)temp);
}
System.out.println(stringBuilder.toString());
}
public static void main(String[] args) throws IOException {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byteArrayOutputStream.write('a');
byteArrayOutputStream.write('b');
byteArrayOutputStream.write('c');
byte[] arr = byteArrayOutputStream.toByteArray();
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i]);
}
}
数据流
public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("D:\\桌面\\a1.txt")));
dos.writeInt(1);
dos.writeChar('a');
dos.writeDouble(9.99);
dos.writeUTF("houyiming");
dos.flush();
dos.close();
DataInputStream dis = new DataInputStream(new BufferedInputStream(new FileInputStream("D:\\桌面\\a1.txt")));
// 注意:读取的顺序要与写入的顺序一致。
System.out.println(dis.readInt());
System.out.println(dis.readChar());
System.out.println(dis.readDouble());
System.out.println(dis.readUTF());
}
对象流
对象的序列化和反序列化
序列化:将java对象转化为字节序列
反序列化:将字节序列还原为java对象
操作基本数据类型
之前学的数据流只能对基本数据类型进行操作,但是对象流不仅可以操作基本数据类型,还可以操作Java对象。
public static void main(String[] args) throws IOException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream("D:\\桌面\\a1.txt")));
objectOutputStream.writeInt(1);
objectOutputStream.writeBoolean(true);
objectOutputStream.writeChar('a');
objectOutputStream.writeUTF("aaaaaaaaaaa");
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(new BufferedInputStream(new FileInputStream("D:\\桌面\\a1.txt")));
// 读出的顺序要与写入的顺序一致
System.out.println(objectInputStream.readInt());
System.out.println(objectInputStream.readBoolean());
System.out.println(objectInputStream.readChar());
System.out.println(objectInputStream.readUTF());
objectInputStream.close();
objectOutputStream.close();
}
操作对象
public static void main(String[] args) throws IOException, ClassNotFoundException {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream("D:\\桌面\\a2.txt"));
Users users = new Users(1,"hym","22");
objectOutputStream.writeObject(users); // 序列化到外部
objectOutputStream.flush();
ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream("D:\\桌面\\a2.txt"));
// 反序列化
Users users1 = (Users) objectInputStream.readObject();
System.out.println(users1.toString());
objectInputStream.close();
objectOutputStream.close();
}
随机访问流
3个核心的方法:
public static void main(String[] args) throws IOException, ClassNotFoundException {
RandomAccessFile randomAccessFile = new RandomAccessFile("D:\\桌面\\aa.txt","rw");
// 将数据写入文件
int[] ints = new int[]{1,2,3,4,5,6,7,8,9,10};
for (int i = 0; i < ints.length; i++) {
randomAccessFile.writeInt(ints[i]);
}
randomAccessFile.seek(4);//整数占4个字节,将指针移动到4,为2的第一个字节。
System.out.println(randomAccessFile.readInt());
// 在第八个字节位置替换
randomAccessFile.seek(8);
randomAccessFile.writeInt(33);
randomAccessFile.close();
}
ApacheIO包
下载与添加commons-IO包
下载地址:Commons IO – Download Apache Commons IO
选择commons-io-2.11.0.jar加入即可
这个地方显示即为成功
FileUtils的使用
读取txt文件并以字符串形式输出:
public static void main(String[] args) throws IOException {
String s = FileUtils.readFileToString(new File("D:\\桌面\\a.txt"),"utf-8");
System.out.println(s);
}
IOUtils的使用
toString方法的使用:
public static void main(String[] args) throws IOException {
String s = IOUtils.toString(new FileInputStream("d:\\桌面\\a.txt"),"utf-8");
System.out.println(s);
}