OutputStream类是一个抽象类,使用它,首先必须通过子类实例化对象。
如果操作的是一个文件,则使用FileOutputStream类,通过向上转型,为OutputStream实例化。
例1:向文件中写入字符串
import java.io.*;
public class OutputStreamDemo01{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
OutputStream out = null ; // 准备好一个输出的对象
out = new FileOutputStream(f) ; // 通过对象多态性,进行实例化
// 第3步、进行写操作
String str = "Hello World!!!" ; // 准备一个字符串
byte b[] = str.getBytes() ; // 只能输出byte数组,所以将字符串变为byte数组
out.write(b) ; // 将内容输出,保存文件
// 第4步、关闭输出流
out.close() ; // 关闭输出流
}
};
例2:使用write(int t)方式写入文件
import java.io.*;
public class OutputStreamDemo02{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
OutputStream out = null ; // 准备好一个输出的对象
out = new FileOutputStream(f) ; // 通过对象多态性,进行实例化
// 第3步、进行写操作
String str = "Hello World!!!" ; // 准备一个字符串
byte b[] = str.getBytes() ; // 只能输出byte数组,所以将字符串变为byte数组
for(int i=0;i<b.length;i++){ // 采用循环方式写入
out.write(b[i]) ; // 每次只写入一个内容
}
// 第4步、关闭输出流
out.close() ; // 关闭输出流
}
};
例3:修改之前的程序,追加文件内容
import java.io.*;
public class OutputStreamDemo03{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
OutputStream out = null ; // 准备好一个输出的对象
out = new FileOutputStream(f,true) ; // 此处表示在文件末尾追加内容
// 第3步、进行写操作
String str = "Hello World!!!" ; // 准备一个字符串
byte b[] = str.getBytes() ; // 只能输出byte数组,所以将字符串变为byte数组
for(int i=0;i<b.length;i++){ // 采用循环方式写入
out.write(b[i]) ; // 每次只写入一个内容
}
// 第4步、关闭输出流
out.close() ; // 关闭输出流
}
};
InputStream也是一个抽象类,必须依靠其子类实例化。若操作文件,则子类是FileInputStream。
通过InputStream从文件中把内容读取出来
例4:开辟指定大小的byte数组
import java.io.* ;
public class InputStreamDemo03{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
InputStream input = null ; // 准备好一个输入的对象
input = new FileInputStream(f) ; // 通过对象多态性,进行实例化
// 第3步、进行读操作
byte b[] = new byte[(int)f.length()] ; // 数组大小由文件决定
int len = input.read(b) ; // 读取内容
// 第4步、关闭输出流
input.close() ; // 关闭输出流\
System.out.println("读入数据的长度:" + len) ;
System.out.println("内容为:" + new String(b)) ; // 把byte数组变为字符串输出
}
};
例5:使用read()通过循环读取
import java.io.* ;
public class InputStreamDemo04{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
InputStream input = null ; // 准备好一个输入的对象
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() ; // 读取内容
}
// 第4步、关闭输出流
input.close() ; // 关闭输出流\
System.out.println("内容为:" + new String(b)) ; // 把byte数组变为字符串输出
}
};
例6:另一种读取方式
import java.io.*;
public class InputStreamDemo05{
public static void main(String args[]) throws Exception{ // 异常抛出,不处理
// 第1步、使用File类找到一个文件
File f= new File("d:" + File.separator + "test.txt") ; // 声明File对象
// 第2步、通过子类实例化父类对象
InputStream input = null ; // 准备好一个输入的对象
input = new FileInputStream(f) ; // 通过对象多态性,进行实例化
// 第3步、进行读操作
byte b[] = new byte[1024] ; // 数组大小由文件决定
int len = 0 ;
int temp = 0 ; // 接收每一个读取进来的数据
while((temp=input.read())!=-1){
// 表示还有内容,文件没有读完
b[len] = (byte)temp ;
len++ ;
}
// 第4步、关闭输出流
input.close() ; // 关闭输出流
System.out.println("内容为:" + new String(b,0,len)) ; // 把byte数组变为字符串输出
}
};
----------------------------------------------------------------------------------------本文参考《java开发实战经典》----------------------------------------------------