通过JAVA——IO流 篇一、JAVA——IO流 篇二两篇文章可以了解到关于字符流的操作。
下面是对字节流的操作。InputStream、OutputStream分别是字节输入流和字节输出流的基类。在基类下面有很多子类是对于文件等流进行操作,例如FileOutputStream是用来将数据写入到输出流文件。用于写入诸如图像数据的原始字节流。
例如:将字符通过字节流写入bytedemo.txt文件。
private static void writeTest() throws Exception
{
//1.创建字节输出流对象,用于操作文件
FileOutputStream fileOutputStream = new FileOutputStream("bytedemo.txt");
//2.写数据,直接写入到了目的地中
fileOutputStream.write("abcdefghijklmn".getBytes());
//fileOutputStream.flush();
//3.关闭资源
fileOutputStream.close();
}
字节流不需要编解码,可以直接进行存储。因此不需要通过flush()方法进行刷新。但是需要close()方法进行关闭,以释放与此流相关联的任何系统资源。
void write(byte[] b) 将 b.length个字节从指定的字节数组写入此文件输出流。
void write(byte[] b, int off, int len) 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
void write(int b) 将指定的字节写入此文件输出流。
FileInputStream从文件获取输入字节,用于读取诸如图像数据的原始字节流。
例如:从bytedemo.txt文件中读出数据。
private static void readTest() throws Exception
{
//1.创建字节读取流对象,和指定文件相关联
FileInputStream fileInputStream = new FileInputStream("bytedemo.txt");
//2.读取数据 (无法读取中文)
int ch = 0;
while((ch = fileInputStream.read()) != -1)
{
System.out.println((char)ch);
}
//关闭资源
fileInputStream.close();
}
第二步读取数据的形式多种多样,如下也可
byte[] by = new byte[1024];
int len;
while((len = fileInputStream.read(by)) != -1)
{
System.out.println(new String(by));
}
byte[] by = new byte[fileInputStream.available()];
int len;
while((len = fileInputStream.read(by)) != -1)
{
System.out.println(new String(by));
}
fileInputStream.available();用于获取文件的字节数,但是对于大文件,不适宜将byte数字建的和其一样大,而是一部分一部分读取。
字节流的重点不是读取一个文本文件,而是用于读取如图像数据的原始字节流。下面例程是用于复制一个MP3文件。
private static void copyMp3()
{
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
//创建读取Mp3的字节流对象
fileInputStream = new FileInputStream("F:\\tiny-heart-master\\birthday-basic\\music\\1.mp3");
//创建写入Mp3的字节流对象
fileOutputStream = new FileOutputStream("F:\\1.mp3");
//创建字节数组
byte[] b = new byte[1024];
int len;
//读取MP3文件
while((len = fileInputStream.read(b)) != -1)
{
//将读取的字节写入输出流
fileOutputStream.write(b,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭流
if(fileOutputStream != null)
{
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fileInputStream != null)
{
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("操作完成");
}
}
和上面的一样读取数据的形式多种多样,也可如下:
private static void copyMp3Low() throws Exception
{
//创建读取Mp3的字节流对象
FileInputStream fileInputStream = new FileInputStream("F:\\tiny-heart-master\\birthday-basic\\music\\1.mp3");
//创建写入Mp3的字节流对象
FileOutputStream fileOutputStream = new FileOutputStream("F:\\1.mp3");
int ch;
while((ch = fileInputStream.read()) != -1)
{
fileOutputStream.write(ch);
}
fileInputStream.close();
fileOutputStream.close();
System.out.println("操作完成");
}
通过read()读取的效率太低。
private static void copyMp3Availavle() throws Exception
{
//创建读取Mp3的字节流对象
FileInputStream fileInputStream = new FileInputStream("F:\\tiny-heart-master\\birthday-basic\\music\\1.mp3");
//创建写入Mp3的字节流对象
FileOutputStream fileOutputStream = new FileOutputStream("F:\\1.mp3");
int size = fileInputStream.available();
byte[] b = new byte[size];
fileInputStream.read(b);
fileOutputStream.write(b);
fileInputStream.close();
fileOutputStream.close();
System.out.println("操作完成");
}
通过 fileInputStream.available()的大小定义byte[]大小不适合读取大文件。
前面字具有缓冲区,那么相应的字节流也具有其缓冲区,输入流的缓冲区为BufferedInputStream、输出流的缓冲区为BufferedOutputStream
。
给上面的复制MP3文件添加缓冲区对象则如下:
private static void copyMp3()
{
BufferedInputStream bufferedInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
//创建读取Mp3的字节流对象
FileInputStream fileInputStream = new FileInputStream("F:\\tiny-heart-master\\birthday-basic\\music\\1.mp3");
//创建读取Mp3的字节流缓冲区对象
bufferedInputStream = new BufferedInputStream(fileInputStream);
//创建写入Mp3的字节流对象
FileOutputStream fileOutputStream = new FileOutputStream("F:\\1.mp3");
//创建写入Mp3的字节流缓冲区对象
bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
//创建字节数组
byte[] b = new byte[1024];
int len;
//读取MP3文件
while((len = bufferedInputStream.read(b)) != -1)
{
//将读取的字节写入输出流
bufferedOutputStream.write(b,0,len);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
//关闭流
if(bufferedOutputStream != null)
{
try {
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bufferedInputStream != null)
{
try {
bufferedInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("操作完成");
}
}