再介绍一下其他一些流的使用
数据操作流,数据输入流允许应用程序以独立于机器的方式从底层输入流读取原始Java数据类型,意思就是平台无关,相关的两个类DataInputStream、DataOutputStream,使用如下:
public class DataInputStreamTest {
public static void main(String[] args) {
DataInputStreamTest test = new DataInputStreamTest();
test.testDataOutputStream();
// test.testDataInputStream();
}
public void testDataInputStream() {
DataInputStream dataInputStream = null;
try {
dataInputStream = new DataInputStream(new FileInputStream("E:\\test\\input.doc"));
byte []b = new byte[1024];
dataInputStream.read(b);
System.out.println(new String(b));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if (dataInputStream != null) {
try {
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
public void testDataOutputStream() {
DataOutputStream dataOutputStream = null;
try {
byte []by = new byte[2048];
ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream(2048);
dataOutputStream = new DataOutputStream(byteArrayInputStream);
dataOutputStream.writeInt('5');
dataOutputStream.writeChars("hahaha");
dataOutputStream.writeInt(6);
byte[] bs = byteArrayInputStream.toByteArray();
for (byte b : bs) {
System.out.print (b+" ");
}
//存入非字符型数据乱码,还不知为何
//0 0 0 53 0 104 0 97 0 104 0 97 0 104 0 97 0 0 0 6
} catch (Exception e) {
}finally {
try {
if (dataOutputStream != null) {
dataOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
打印流,可用于将对象的格式表示打印到文本输出流,相关的类PrintWriter,使用如下:
public void testPrintWriter() {
PrintWriter printWriter = null;
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("E:\\test\\input.txt"));
printWriter = new PrintWriter(new File("E:\\test\\printwriter.txt"));
String s = null;
while((s = bufferedReader.readLine()) != null) {
printWriter.write(s);
}
} catch (Exception e) {
}finally {
if (printWriter != null) {
printWriter.close();
}
if (bufferedReader!=null) {
try {
bufferedReader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
标准输入输出流,相关操作如下:
/**
* 标准输入流使用
*/
private void testStandInputStream() {
//标准输入流,可用于键盘录入
Scanner scanner = new Scanner(System.in);
System.out.println("=======课题选择========");
System.out.println("\t计算机原理:001");
System.out.println("\t信息系统论:002");
System.out.println("\t计算机网络:003");
System.out.println("\t数据库原理:004");
System.out.println("====================");
System.out.println("请输入要选择的课题代号,按回车键确定");
String nextLine = scanner.nextLine();
if ("001".equals(nextLine)) {
System.out.println("选择的课程是:计算机原理");
}else if ("002".equals(nextLine)) {
System.out.println("选择的课程是:信息系统论");
}else if ("003".equals(nextLine)) {
System.out.println("选择的课程是:计算机网络");
}else if ("004".equals(nextLine)) {
System.out.println("选择的课程是:数据库原理");
}
System.out.println("系统关闭!");
System.exit(-1);
}
//标准输出流
private void testStandOutputStream() {
PrintStream out = System.out;
out.println("打印信息到控制台");
}
对象输入输出流,也叫做序列化流,可用于对象的序列化和反序列化,就是可以实现将对象写入文本、从文本中读取信息到对象中(这里是读取数据到对象),常用于网络信息传输和远程方法调用,实例如下:
/**
* 测试对象输入输出流也叫做序列化流
* 通过对象输出流可以进行序列化,将对象写入到文本,通过对象输入流可以实现反序列化,从文本中读取对象
*/
public class ObjectStreamTest {
public static void main(String[] args) {
ObjectStreamTest test = new ObjectStreamTest();
test.testObjectOutputStream();
test.testObjectInputStream();
}
/**
* 通过对象输出流可以进行序列化,将对象写入到文本
*/
public void testObjectOutputStream() {
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(new FileOutputStream("E:\\test\\user.txt"));
User user = new User("小王", 13);
objectOutputStream.writeObject(user);
objectOutputStream.flush();
objectOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 通过对象输入流可以实现反序列化,从文本中读取对象
*/
public void testObjectInputStream() {
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(new FileInputStream("E:\\test\\user.txt"));
Object readObject = objectInputStream.readObject();
System.out.println(readObject);//User [name=小王, age=0]
objectInputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* 对象一定要实现序列化接口
*/
class User implements Serializable{
private static final long serialVersionUID = -1796903937206347177L;
private String name;
private transient int age;//可以防止成员被序列化
public User(String name,int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
介绍最后一个,合并流,可用于将多个输入流中数据合并到一个流中,使用如下:
public class AddStreamTest {
/**
* 将两个流中的数据同时写到一个文件中
*/
public static void main(String[] args) throws IOException {
FileOutputStream fileOutputStream = new FileOutputStream("E:\\test\\input3.txt");
FileInputStream fileInputStream1 = new FileInputStream("E:\\test\\input.txt");
FileInputStream fileInputStream2 = new FileInputStream("E:\\test\\input2.txt");
SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
byte []b = new byte[1024];
int len = 0;
while((len = sequenceInputStream.read(b, 0, b.length)) != -1) {
fileOutputStream.write(b, 0, len);
}
fileOutputStream.close();
sequenceInputStream.close();
fileInputStream1.close();
fileInputStream2.close();
}
}
还有一些其他的流,在这里就不做介绍了。