字节流
字节流和字符流的用法几乎完全一样,但字节流操作的单元是数据单元是8位的字节。它不仅可以操作字符,还可以操作其他媒体文件。
字节流主要是由InputStream和outPutStream作为基类,以下是比较常用的字节流:
文件输入流----FileInputStream
文件输出流----FileOutputStream
缓冲输入流----BufferedInputStream
缓冲输出流----BufferedOutputStream
FileInputStream
定义
FileInputStream从文件系统中的文件获取输入字节。
FileInputStream用于读取诸如图像数据的原始字节流。
构造方法
文件输入流读取数据的方式
文件输入流读取数据
package javaIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileInputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("E:\\text.txt");
FileOutputStream os = new FileOutputStream(file);
//向文件中写入数据
os.write("abc".getBytes(), 0, 3);
if(!file.exists()){
System.out.println("文件不存在!");
}
/*
* 创建文件输入流
*/
//FileInputStream fis = new FileInputStream(file);
//FileInputStream fis = new FileInputStream("E://text");
/* 文件输入流读取数据
* int read()
* 从该输入流读取一个字节的数据。
* 即:
* 一次读取一个字节
*/
FileInputStream fs1 = new FileInputStream(file);
for(int i=0;i<file.length();i++){
int b = fs1.read();
System.out.println(b);//输出的是字母a,b,c在ASCII码中对应的数字
}
fs1.close();
/* 文件输入流读取数据
* int read(byte[] b)
* 从该输入流读取最多 b.length个字节的数据为字节数组。
* 即:
* 一次读取一个字节数组
*/
FileInputStream fs2 = new FileInputStream(file);
byte[] b = new byte[10];
fs2.read(b);
System.out.println(new String(b));
fs2.close();
/* 文件输入流读取数据
* int read(byte[] b, int off, int len)
* 从该输入流读取最多 len字节的数据为字节数组。
* 即:
* 一次读取一个字节数组的一部分
*/
FileInputStream fs3 = new FileInputStream(file);
byte[] b1 = new byte[10];
fs3.read(b1, 0, 2);//读取两个字节的数据到数组
System.out.println(new String(b1));
fs3.close();
/*
* 注意:不建议使用这种方式,因为可能会有内存溢出异常。
*/
}
}
FileOutputStream
定义
文件输出流是用于将数据写入到输出流File或一个FileDescriptor的。
FileOutputStream用于写入诸如图像数据的原始字节流。
构造方法
文件输出流写出数据的方式
文件输出流写入数据
package javaIO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileOutputStream {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
File file = new File("E://text.txt");
/*
* 创建文件输出流
*/
//FileOutputStream fos = new FileOutputStream(file);
FileOutputStream fos1 = new FileOutputStream("E://text.txt");
/* 文件输出流写出数据
* void write(byte[] b)
* 将 b.length个字节从指定的字节数组写入此文件输出流。
* 即:
* 一次写一个字节数组
*/
String s1 = "abcde";
byte[] b1 = s1.getBytes();
fos1.write(b1);
fos1.close();
/* 文件输出流写出数据
* void write(byte[] b, int off, int len)
* 将 len字节从位于偏移量 off的指定字节数组写入此文件输出流。
* 即:
* 一次写一个字节数组的一部分
*/
FileOutputStream fos2 = new FileOutputStream(file);
byte[] b2 = new byte[]{97,98,99,100,101};//数字是字母abcde对应ASCII码值
fos2.write(b2, 0, 2);
fos2.close();
/* 文件输出流写出数据
* void write(int b)
* 将指定的字节写入此文件输出流。
* 即:
* 一次写一个字节
*/
FileOutputStream fos3 = new FileOutputStream(file);
fos3.write(97);//数字是字母a对应的ASCII码值
fos3.close();
}
}
文件输出流追加数据
package javaIO;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestFileOutputStream1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//判断文件是否存在
File file = new File("E://text.txt");
if(!file.exists()){
System.out.println("文件不存在!");
}
FileOutputStream fos = null;
try {
/*
* FileOutputStream(File file, boolean append)
* 传入true,实现续写
*/
fos = new FileOutputStream(file,true);
fos.write("abcde".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
字节流的复制操作
复制图片
package javaIO;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyPicture {
public static void main(String[] args) {
// TODO Auto-generated method stub
File picture = new File("E://1.png");
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(picture);
fos = new FileOutputStream("E:\\2.png");
byte[] b = new byte[1024];//创建缓冲区
int len = 0;//定义读取次数
try {
//循环读写,读多少次,写多少次
while((len=fis.read(b))!=-1){
fos.write(b, 0, len);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fis.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
复制文件
package javaIO;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFile {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = new FileInputStream("E:\\text.txt");
FileOutputStream fos = new FileOutputStream("E:\\text(1).txt");
/*
* 读一个字节,写一个字节
*/
int i = 0;
while((i=fis.read())!=-1){
fos.write(i);
}
fis.close();
fos.close();
/*
* 批量操作字节
* 一次读写一个数组
*/
FileInputStream fis1 = null;
FileOutputStream fos1 = null;
fis1 = new FileInputStream("E://text.txt");
fos1 = new FileOutputStream("E://text(2).txt");
//定义一个缓冲区
byte[] b = new byte[1024];
//定义读取的次数
int n = 0;
//循环读取
while((n=fis1.read(b))!=-1){
fos1.write(b, 0, n);
}
fis1.close();
fos1.close();
}
}
字节缓冲流
缓冲输入流----BufferedInputStream
缓冲输出流----BufferedOutputStream
package javaIO;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBuffered {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
FileInputStream fis = new FileInputStream("E://1.png");
BufferedInputStream bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream("E://1(1).png");
BufferedOutputStream bos = new BufferedOutputStream(fos);
byte[] b = new byte[1024];
int len = 0;
while((len=bis.read(b))!=-1){
bos.write(b, 0, len);
}
bis.close();
bos.close();
}
}