字节流的使用
文件字节输入流:每次读取一个字节
- 文件字节输入流:FileInputStream
- 作用:以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去。
构造器 | 说明 |
---|---|
public FileInputStream(File file) | 创建字节输入流管道与源文件对象接通 |
public FileInputStream(String pathname) | 创建字节输入流管道与源文件路径接通 |
方法名称 | 说明 |
---|---|
public int read() | 每次读取一个字节返回,如果返回-1,表示读取到了输入流的末尾 |
public int read(byte[] b) | 将数据读入一个字节数组,如果返回-1,表示读取到了输入流的末尾 |
public int read(byte[] b,int off,int len) | 将数据读入一个字节数组,off 指定在数组b 中存放数据的起始偏移位置,len 指定读取的最大字节数。如果返回-1,表示读取到了输入流的末尾。 |
- 每次读取一个字节存在什么问题?
- 性能较慢
- 读取中文字符输出无法避免乱码问题。
文件字节输入流:每次读取一个字节数组
- 作用:以内存为基准,把磁盘文件中的数据以字节的形式读取到内存中去。
方法名称 | 说明 |
---|---|
public int read() | 每次读取一个字节返回,如果返回-1,表示读取到了输入流的末尾 |
public int read(byte[] b) | 将数据读入一个字节数组,如果返回-1,表示读取到了输入流的末尾 |
public int read(byte[] b,int off,int len) | 将数据读入一个字节数组,off 指定在数组b 中存放数据的起始偏移位置,len 指定读取的最大字节数。如果返回-1,表示读取到了输入流的末尾。 |
- 每次读取一个字节数组存在什么问题?
- 读取的性能得到了提升
- 读取中文字符输出无法避免乱码问题。
文件字节输入流:一次读完全部字节
-
如何使用字节输入流读取中文内容输出不乱码呢?
- 定义一个与文件一样大的字节数组,一次性读取完文件的全部字节。
-
直接把文件数据全部读取到一个字节数组可以避免乱码,是否存在问题?
- 如果文件过大,字节数组可能引起内存溢出。
-
官方为字节输入流InputStream提供了如下API可以直接把文件的全部数据读取到一个字节数组中
-
public byte[] readAllBytes() throws IOException:直接将当前字节输入流对应的文件对象的字节数据装到一个字节数组返回
文件字节输出流:写字节数据到文件
- 作用:以内存为基准,把内存中的数据以字节的形式写出到磁盘文件中去的流。
构造器 | 说明 |
---|---|
public FileOutputStream(File file) | 创建字节输出流管道与源文件对象接通 |
public FileOutputStream(File file,boolean append) | 创建字节输出流管道与源文件对象接通,可追加数据 |
public FileOutputStream(String filepath) | 创建字节输出流管道与源文件路径接通 |
public FileOutputStream(String filepath,boolean append) | 创建字节输出流管道与源文件路径接通,可追加数据 |
- 文件字节输出流(FileOutputStream)写数据出去的API
方法名称 | 说明 |
---|---|
public void write(int a) | 写一个字节出去 |
public void write(byte[] buffer) | 写一个字节数组出去 |
public void write(byte[] buffer , int pos , int len) | 写一个字节数组的一部分出去。 |
- 流的关闭与刷新
方法 | 说明 |
---|---|
flush() | 刷新流,还可以继续写数据 |
close() | 关闭流,释放资源,但是在关闭之前会先刷新流。一旦关闭,就不能再写数据 |
-
字节输出流如何实现写出去的数据能换行
os.write(“\r\n”.getBytes())
-
如何让写出去的数据能成功生效?
- flush()刷新数据
- close()方法是关闭流,关闭包含刷新,关闭后流不可以继续使用了。
文件拷贝
import java.io.*;
//复制文件
public class FileCopy {
public static void main(String[] args) {
InputStream input = null;
OutputStream out = null;
try {
input = new FileInputStream("F:\\java练习\\LOL\\英雄联盟\\hero\\金克丝.txt");
out = new FileOutputStream("F:\\java练习\\LOL\\英雄联盟\\gold\\test.txt");
int len;
byte[] bytes = new byte[1024];
while ((len=input.read(bytes))>0) {
out.write(bytes,0,len);
//Thread.sleep(3000);//没循环一次停止3s
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//先开的后关闭
try {
if (out!=null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (input!=null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
import java.io.*;
//复制图片
public class PicCopy {
public static void main(String[] args) {
InputStream input = null;
OutputStream out = null;
try {
input = new FileInputStream("D:\\picture\\恶灵.jpg");
out = new FileOutputStream("F:\\java练习\\LOL\\英雄联盟\\gold\\el.jpg");
byte[] b = new byte[1024];
int len;
while ((len = input.read(b))>0) {
out.write(b,0,len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (out!=null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (input!=null) {
input.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 上述代码中,使用try catch是因为在IO流中,使用完后必须手动的回收,即关闭流,具体的原因可以查看该博客:IO流为什么必须手动关闭,不能像其他的方法坐等GC处理。