字节流主要操作为byte类型数据,以byte数组为准,主要有OutputStream类、InputStream类
1.字节输出流:OutputStream
OutputStream是整个IO包中字节输出流的最大父类,定义如下:
public abstract class OutputStream extends Object implements Closeable,Flushable
OutputStream是一个抽象类,如果要使用它则需要通过子类实例化对象,如果现在是要操作一个文件,则使用FileOutputStream类实例化
public FileOutputStream(File file) throws FileNotFoundException
范例:向文件中写入字符串
<span style="font-size:18px;">public class OutputStreamDemo01{
public static void main(String[] args) throws Exception{
//第一步:通过File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//第二步:通过子类实例化父类对象
OutputStream out = null;
out = new FileOutputStream(f);
//第三步:进行写操作
String str = "Hello World!";
byte[] b = str.getBytes();
out.write(b);
//第四步:关闭输出流
out.close();
}
}</span>
以上操作是直接将一个字符串变为byte数组,然后将byte数组直接写入到文件中,也可以通过循环把每一个字节一个一个地写入到文件之中
<span style="font-size:18px;">public class OutputStreamDemo02{
public static void main(String[] args){
File f = new File("d:" + File.separator + "test.txt");
OutputStream out = null;
out = new FileOutputStream(f);
String str = "Hello World!";
byte b[] = str.getBytes();
for(int i = 0; i < b.length; i ++){
out.write(b[i]);
}
out.close();
}
}</span>
除此之外,还可以通过FileOutputStream向文件追加内容:
public FileOutputStream(File file,boolean append) throws FileNotFoundException{}
<span style="font-size:18px;">public class OutputStreamDemo02{
public static void main(String[] args){
File f = new File("d:" + File.separator + "test.txt");
OutputStream out = null;
out = new FileOutputStream(f,true);
String str = "Hello World!";
byte b[] = str.getBytes();
for(int i = 0; i < b.length; i ++){
out.write(b[i]);
}
out.close();
}
}</span>
2.字节输入流InputStream
既然可以向文件中写入内容,则可以通过InputStream从文件中把内容读出来
InputStream类的定义如下:
public abstract class InputStream extends Object implesments Closeable
同样的,抽象类InputStream本身也是一个抽象类,必须依靠子类实例化,对应的子类为FileInputStream
范例:
<span style="font-size:18px;">public class InputStreamDemo01{
public static void main(String[] args){} throws Exception{
//使用File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//通过子类实例化父类对象
InputStream input = null;
input = new FileInputStream(f);
//进行读操作
byte b[] = new byte[1024];
input.read(b);
//关闭输入流
input.close();
System.out.println("内容为:" + new String(b));
}
}</span>
程序运行结果:
Hello World!_____
后面有很多空格,此处省略
如果要想输出实际长度的内容,则要注意到read方法的返回值,返回值表示向数组中写入了多少个数据
<span style="font-size:18px;">public class InputStreamDemo02{
public static void main(String[] args){} throws Exception{
//使用File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//通过子类实例化父类对象
InputStream input = null;
input = new FileInputStream(f);
//进行读操作
byte b[] = new byte[1024];
int len = input.read(b);
//关闭输入流
input.close();
System.out.println("内容为:" + new String(b,0,len));
}
}</span>
程序输出结果:
Hello World!
虽然指定了byte数组的范围,但是程序已然开辟了很多的无用空间,这样会造成资源的浪费,此时可以根据文件的数据量来选择开辟的空间大小要想完成这样的操作,则可以使用File类中的length()方法,取得文件的大小
<span style="font-size:18px;">public class InputStreamDemo03{
public static void main(String[] args){} throws Exception{
//使用File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//通过子类实例化父类对象
InputStream input = null;
input = new FileInputStream(f);
//进行读操作
byte b[] = new byte[(int)f.length()];
int len = input.read(b);
//关闭输入流
input.close();
System.out.println("内容为:" + new String(b));
}
}</span>
同理,还可以一个一个字节读取
<span style="font-size:18px;">public class InputStreamDemo04{
public static void main(String[] args){} throws Exception{
//使用File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//通过子类实例化父类对象
InputStream input = null;
input = new FileInputStream(f);
//进行读操作
byte b[] = new byte[(int)f.length()];
for(int i = 0; i < b.length; i++){
b[i] = (byte)input.read();
}
//关闭输入流
input.close();
System.out.println("内容为:" + new String(b));
}
}</span>
以上程序是知道了具体数组大小的前提下开展的,如果此时不知道输入的内容有多大,则只能通过判断是否读到末尾的方式来读取文件
<span style="font-size:18px;">public class InputStreamDemo05{
public static void main(String[] args){} throws Exception{
//使用File类找到一个文件
File f = new File("d:" + File.separator + "test.txt");
//通过子类实例化父类对象
InputStream input = null;
input = new FileInputStream(f);
//进行读操作
int len = 0;
byte b[] = new byte[1024];
int temp = 0; //接收读取的每一个内容
while((temp = input.read()) != -1){
b[len] = (byte) temp;
len++;
}
//关闭输入流
input.close();
System.out.println("内容为:" + new String(b,0,len));
}
}</span>
这里的判断是当文件读到末尾时,返回的内容为-1参考资料:《Java开发实战经典》