字节流主要操作byte类型数据,以byte数组为准,主要操作类是OutputStream类和InputStream类。
1、字节输出流:OutputStream
OutputStrem类是一个抽象类,要使用必须通过子类实例化对象,要操作文件,则使用FileOutputStream类转型,在实例化。
【OutputStream类常用方法】
* public void close() throws IOException 关闭输出流
* public void flush() throws IOException 刷新缓冲区
* public void write(byte[] b) throws IOException 将一个byte数组写入数据流
* public void write(byte[] b,int off,int len) throws IOException 将一个指定范围的byte数组写入数据流
* public void write(int b) throws IOException 将一个字节数据写入数据流
实例一、向文件中写入数据
public class OutputStreamDemo {
/*向文件中写入字符串*/
public static void main(String[] args) {
//在file类中找到一个文件
File f = new File("D:"+File.separator+"test.txt");
try {
//通过子类实例化父类对象
OutputStream out = new FileOutputStream(f);
String str = "Hello world !";
//直接将字符串变为byte数组,然后写入文件
byte b[] = str.getBytes();
//数据写入
out.write(b);
//关闭输出流
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
耶可以通过循环把每一个字节一个个的写入到文件
public class OutputStreamDemo {
/*向文件中写入字符串*/
public static void main(String[] args) {
//在file类中找到一个文件
File f = new File("D:"+File.separator+"test.txt");
try {
//通过子类实例化父类对象
OutputStream out = new FileOutputStream(f);
String str = "Hello world !";
//将字符串变为byte数组
byte b[] = str.getBytes();
//数据写入
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
//关闭输出流
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
实例二:向文件中追加新的数据
通过FileOutputStream向文件中追加新内容。通过构造方法实现:
public FileOutputStream(File file,boolean append) throws FileFoundException,在构造方法中奖append的值设置为true,则表示在文件的末尾追加内容。
public class OutputStreamDemo {
/*向文件中写入字符串*/
public static void main(String[] args) {
//在file类中找到一个文件
File f = new File("D:"+File.separator+"test.txt");
try {
//通过子类实例化父类对象
OutputStream out = new FileOutputStream(f,true);
String str = "\r\nHello world !";
//将字符串变为byte数组
byte b[] = str.getBytes();
//数据写入
for (int i = 0; i < b.length; i++) {
out.write(b[i]);
}
//关闭输出流
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2、字节输入流:InputStream
也是一个抽象类,需要通过FileInputStream子类进行实例化
* 【InputStream类的常用方法】
* public int available() throws IOException 可以取得输入文件的大小
* public void close() throws IOException 关闭输入流
* public abstract int read() throws IOExcrption 读取内容,以数组方式读取
* public int read(byte[] b) throws IOExcrption 将内容读到byte数组中同时返回读入的个数
实例:读取文件数据
public class InputStream {
public static void main(String[] args) {
/*1、使用File找到一个文件*/
File f = new File("D:"+File.separator+"test.txt");
/*2、通过子类实例化对象*/
try {
FileInputStream input = new FileInputStream(f);
/*3、开始读取操作*/
byte b[] = new byte[1024]; //将所有内容读取到数组
input.read(b);
/*关闭输入流*/
input.close();
/*把byte数字当做字符串输出*/
System.out.println("读取内容:"+new String(b));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上述代码在读取数据的时候会带有很多空格的,我们可以指定byte数组范围,如下修正
public class InputStream {
public static void main(String[] args) {
/*1、使用File找到一个文件*/
File f = new File("D:"+File.separator+"test.txt");
/*2、通过子类实例化对象*/
try {
FileInputStream input = new FileInputStream(f);
/*3、开始读取操作*/
byte b[] = new byte[1024]; //将所有内容读取到数组
int len = input.read(b);
/*关闭输入流*/
input.close();
/*把byte数字当做字符串输出*/
System.out.println("读取内容:"+new String(b,0,len));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
虽然不在出现空格,但是在数组中开辟了过多的无效内存,占用很多空间。我们可以根据文件的数量来开辟相应的内容空间。
public class InputStream {
public static void main(String[] args) {
/*1、使用File找到一个文件*/
File f = new File("D:"+File.separator+"test.txt");
/*2、通过子类实例化对象*/
try {
FileInputStream input = new FileInputStream(f);
/*3、开始读取操作*/
//将所有内容读取到数组,数组大小由文件内容大小决定
byte b[] = new byte[(int) f.length()];
input.read(b);
/*关闭输入流*/
input.close();
/*把byte数字当做字符串输出*/
System.out.println("读取内容:"+new String(b));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
上面代码是一次性读取出文件中的所有内容,我们也可以一个字节一个字节的来读取,修改如下代码:
在知晓数组具体大小的情况下:
public class InputStream {
public static void main(String[] args) {
/*1、使用File找到一个文件*/
File f = new File("D:"+File.separator+"test.txt");
/*2、通过子类实例化对象*/
try {
FileInputStream input = new FileInputStream(f);
/*3、开始读取操作*/
//将所有内容读取到数组,数组大小由文件内容大小决定
byte b[] = new byte[(int) f.length()];
for (int i = 0; i < b.length; i++) {
b[i]=(byte) input.read();
}
/*关闭输入流*/
input.close();
/*把byte数字当做字符串输出*/
System.out.println("读取内容:"+new String(b));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
在不知晓数组具体大小的情况下,只能通过判断是否读取到文件末尾来进行内容读取:
public class InputStream {
public static void main(String[] args) {
/*1、使用File找到一个文件*/
File f = new File("D:"+File.separator+"test.txt");
/*2、通过子类实例化对象*/
try {
FileInputStream input = new FileInputStream(f);
/*3、开始读取操作*/
//将所有内容读取到数组,数组大小由文件内容大小决定
byte b[] = new byte[1024];
int temp = 0; //接收读取的内容
int len = 0; //记录读取的数据个数
while ((temp=input.read())!=-1) {
//将每次读取的内容给temp变量,如过temp不等于-1.表示还没读取完
b[len]=(byte)temp;
len++;
}
/*关闭输入流*/
input.close();
/*把byte数字当做字符串输出*/
System.out.println("读取内容:"+new String(b,0,len));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}