Java学习笔记28(IO字节流)

时间:2023-03-09 08:33:37
Java学习笔记28(IO字节流)

IO定义:

  写:Output操作:将内存上的数据持久化 到设备上,这个动作称为输出;

  读:Input操作:把硬盘上的东西读取到内存,这个动作称为输入;              这两种操作称为IO流

IO流可以分为两类:字节流   字符流

字节流

  输入和输出:参照物,都是java程序来参照 

  字节时输入流类:InputStream   字节输出流类:OutputStream

  字节输入流类和字节输出流类都是抽象类,所以只能通过子类来创建实例化对象。示例:输出流

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; public class Demo2 {
public static void main(String[] args) throws IOException {
//通过子类FileOutputStream类建立输出流对象
OutputStream out=new FileOutputStream("d:\\a.txt");
//调用OutputStream的write(int b)方法,写入字节数据到磁盘上的文件
out.write();
byte[] b={'A','B','C'};
//写入字节数组write(byte[] b)
out.write(b);
//write(byte[] b,int off,int len)写入字节数据,从x下标开始,写入数据的个数
out.write(b,,);
//释放资源
out.close();
}
}

输入流:

package com.zs.Demo;

import java.io.FileInputStream;
import java.io.IOException; public class Demo3 {
public static void main(String[] args) {
try {
fun1();
fun2();
fun3();
} catch (IOException e) {
e.printStackTrace();
}
} private static void fun3() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
byte[] b=new byte[1024];
int len=0;
while((len=in.read(b,0,2))!=-1){
//字符数组转字符串
System.out.println(new String(b,0,len));
/* dA
BC
C*/
}
in.close();
} private static void fun2() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
byte[] b=new byte[1024];
int len=0;
while((len=in.read(b))!=-1){
//字符数组转字符串
System.out.println(new String(b,0,len));//dABCC
}
in.close();
}
private static void fun1() throws IOException {
FileInputStream in=new FileInputStream("d:\\a.txt");
//read()从输入流中读取数据的下一个字节;当没有字节时返回-1;
int len=0;
while((len=in.read())!=-1){
System.out.println((char)len);//d A B C C
}
in.close();
}
}

续写:

public class Demo4 {
public static void main(String[] args) throws IOException {
File f=new File("d:\\a.txt");
FileOutputStream fou=new FileOutputStream(f,true);
fou.write("hello\r\n".getBytes());// \r\n回车换行,getBytes()将字符串转字节数组
fou.close();
}
}//如果文件存在,不会覆盖,会把内容追加到文件内容后面

异常处理:

package com.zs.Demo;

import java.io.FileOutputStream;
import java.io.IOException; public class Demo5 {
public static void main(String[] args) {
FileOutputStream fou=null;
try {
fou=new FileOutputStream("d:\\a.txt");
fou.write("java".getBytes());
} catch (IOException e) {
System.out.println(e);
throw new RuntimeException("文件写入失败");
}finally {
try {
if (fou != null) {
fou.close();
}
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}

复制文件:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFile {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null;
try {//在main方法中,尽量使用try语句,其他方法中用throw
fin=new FileInputStream("d:\\a.txt");
fou=new FileOutputStream("e:\\a.txt");
//首先读取d盘下的a文件内容到内存
int len=0;
while((len=fin.read())!=-1){
fou.write(len);
}
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
}finally {
if (fou!=null){
try {
fou.close();//先打开的后关闭,后打开的先关闭,这里先关闭输出流,
} catch (IOException e) {
throw new RuntimeException("输出流资源释放失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("输入流资源释放失败");
} }
}
}
}
}
}

上一个方法中,一次只复制一字节的文件,当文件太大时,复制速度太慢,如下:用上面的代码复制一个视频,一字节一字节复制

package com.zs.Demo2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFile {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null;
try {//在main方法中,尽量使用try语句,其他方法中用throw
fin=new FileInputStream("d:\\1.mp4");
fou=new FileOutputStream("e:\\1.mp4");
//首先读取d盘下的a文件内容到内存
int len=0;
long l = System.currentTimeMillis();
while((len=fin.read())!=-1){
fou.write(len);
}
long l1 = System.currentTimeMillis();
System.out.println(l1-l);//158014 可以发现,复制了158秒,速度很慢
} catch (IOException e) {
throw new RuntimeException("文件复制失败");
}finally {
if (fou!=null){
try {
fou.close();//先打开的后关闭,后打开的先关闭,这里先关闭输出流,
} catch (IOException e) {
throw new RuntimeException("输出流资源释放失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("输入流资源释放失败");
}
}
}
}
}
}
}

为了提高速度,采用字节数组,

package com.zs.Demo2;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyFileByByte {
public static void main(String[] args) {
FileInputStream fin=null;
FileOutputStream fou=null; try {
fin=new FileInputStream("d:\\1.mp4");
fou=new FileOutputStream("e:\\1.mp4");
int len=0;
byte[] b=new byte[1024];//这里的意思是一次复制1kb,也可以把数组大小改变1MB(1024*1024)
long l = System.currentTimeMillis();
while((len=fin.read(b))!=-1){
fou.write(b);
}
long l1 = System.currentTimeMillis();
System.out.println(l1-l);//325 只要325毫秒就复制好了
} catch (IOException e) {
throw new RuntimeException("复制失败");
}finally {
if (fou!=null){
try {
fou.close();
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}finally {
if (fin!=null){
try {
fin.close();
} catch (IOException e) {
throw new RuntimeException("释放资源失败");
}
}
}
}
}
}
}