您的位置:首页 > 编程语言 > Java开发

DAY20:尚学堂高琪JAVA(156~164)其他流及 IO的设计模式

2019-03-19 16:39 183 查看

其他流

一、节点流
1.字节数组 节点流
输入流 ByteArrayInputStream-----read(byte[] b,int off,int len)+close()
输入流 ByteArrayOutputStream----write(byte[] b,int off,int len)+toByteArray() 不能使用多态

package otherio;
/*
* 156 字节数组流的读写
* */
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrayDemo1 {
public static void main(String[] args) throws IOException {
read(write());
}
//输入流操作与文件输入流操作一致
public static void read(byte[] src) throws IOException {
//数据源

//选择流
InputStream is1=new BufferedInputStream(
new ByteArrayInputStream(src));
//操作
byte[] car=new byte[1024];
int len=0;
while (-1!=(len=is1.read(car))) {
System.out.println(new String(car,0,len));
}
//
is1.close();
}
//输出流操作与文件输出流操作有些不同,有新增方法,不能使用多态
public static byte[] write() throws IOException{
//目的地
byte[] dest;
//选择流 不同
ByteArrayOutputStream b1=new ByteArrayOutputStream();
//操作 写出
String s1="输出流操作与文件输出流操作有些不同,有新增方法,不能使用多态";
byte[] info =s1.getBytes();
b1.write(info);
//获取数据
dest=b1.toByteArray();
//
b1.close();
return dest;
}

}

package otherio;
/*
* 156 字节数组流与文件流的对接
* 文件--程序--字节数组
*   a.文件输入流
*   b.字节数组输出流
*  字节数组 --程序--文件
*   a.字节数组输入流
*   b.文件输出流
* */
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class ByteArrayDemo2 {
public static void main(String[] args) throws IOException {
byte[] test=getBytesFromFile("H:/1/1.jpg");
System.out.println(new String(test));
toFileFromByteArray(test, "H:/1/2.jpg");//也可以是其他格式
}
public static byte[] getBytesFromFile(String srcPath) throws IOException {
//创建源文件和目的地
File f1=new File(srcPath);
byte[] dest=null;
//选择流
InputStream is1=new BufferedInputStream(new FileInputStream(f1)) ;
ByteArrayOutputStream bos1=new ByteArrayOutputStream();
//操作  不断读取文件,写出到字节数组流中
byte[] car=new byte[1024];
int len=0;
while (-1!=(len=is1.read(car))) {
//写出到数组流中
bos1.write(car,0,len);
}
bos1.flush();
//获取数据
dest=bos1.toByteArray();

bos1.close();
is1.close();
return dest;
}
public static byte[] toFileFromByteArray(byte[] src,String destPath) throws IOException {
//创建源
//目的地
File dest=new File(destPath);
//选择流
InputStream is1=new BufferedInputStream(new ByteArrayInputStream(src));
OutputStream os1=new BufferedOutputStream(new FileOutputStream(dest));
////操作  不断读取数组,写出到文件输出流中
byte[] car=new byte[1024];
@SuppressWarnings("unused")
int len=0;
while(-1!=(len=is1.read(car))){
os1.write(car,0, car.length);
}
os1.flush();

os1.close();
is1.close();
return null;
}
}

二、数据流
1.基本类型+String 保留数据+类型
输入流:DataInputStream readxxx
输出流:DataOutputStream writexxx

package otherio;
//158 基本类型(基本+String)处理流
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataDemo1 {

public static void main(String[] args) throws IOException {
write("H:/1/1.txt");
read("H:/1/1.txt");
}
//数据+类型输出到文件
public static void write(String destPath) throws IOException {
double point=2.5;
long num=100L;
String s1="数据类型";
//创建源
File dest=new File(destPath);
//选择流 DataOutputStream
DataOutputStream dos1=new DataOutputStream(
new BufferedOutputStream(
new FileOutputStream(dest)));
//操作 写出的顺序为读取准备
dos1.writeDouble(point);
dos1.writeLong(num);
dos1.writeUTF(s1);

dos1.flush();
dos1.close();
}
//从文件中读取数据+类型
@SuppressWarnings("resource")
public static void read(String destPath) throws IOException {

File src=new File(destPath);
//选择流 DataInputStream
DataInputStream dis1=new DataInputStream(
new BufferedInputStream(
new FileInputStream(src)));
//操作 读取顺序必须与写出一致
double num1=dis1.readDouble();
Long num2=dis1.readLong();
String s2=dis1.readUTF();

System.out.println(num1);
System.out.println(num2);
System.out.println(s2);
}
}


三、引用类型(对象) 保留数据+类型
反序列化 输入流:ObjectInputStream readObjec()
序列化 输出流:ObjecOutputStream writeObjec()
注意:1.先序列化,再反序列化。反序列化顺序必须与序列化一致。
2.不是所有的对象都可以序列化,java.io.Serializable
不是所有的属性都需要序列化 transient

package otherio;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Array;
import java.util.Arrays;
//159
public class EmployeeDemo1 {

public static void main(String[] args) throws IOException, ClassNotFoundException {
// TODO Auto-generated method stub
seri("H:/1/1.txt");
read("H:/1/1.txt");
}
//序列化
public static void seri(String destPath) throws IOException{
Employee e1=new Employee("lzk", 600);
File dest=new File(destPath);
int[] arr1={1,2,3,4,5};//数组也可以序列化
//选择流 ObjectOutputStream
ObjectOutputStream dos1=new ObjectOutputStream(
new BufferedOutputStream(
new FileOutputStream(dest)));
//写入流
dos1.writeObject(e1);
dos1.writeObject(arr1);

dos1.flush();
dos1.close();
}
//反序列化
public static void read(String destPath) throws IOException, ClassNotFoundException{
File src=new File(destPath);
//选择流
ObjectInputStream dis1=new ObjectInputStream(
new BufferedInputStream(
new FileInputStream(src)));
//操作
Object o1=dis1.readObject();
if (o1 instanceof Employee) {
Employee e1=(Employee)o1;
System.out.println(e1.getName());
System.out.println(e1.getSalary());
}
Object o2=dis1.readObject();
int[] arr1=(int[])o2;
System.out.println(Arrays.toString(arr1));

dis1.close();
}
}
package otherio;
//159
public class Employee implements java.io.Serializable {

private static final long serialVersionUID = 1L;
private transient String name;
private double salary;
public Employee(String name, double salary) {
super();
this.name = name;
this.salary = salary;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSalary() {
return salary;
}
public void setSalary(double salary) {
this.salary = salary;
}
}


四:打印流
PrintStream(属于处理流)

package otherio;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
//161
public class PrintStreamDemo01 {

public static void main(String[] args) thr
20000
ows FileNotFoundException {
// TODO Auto-generated method stub
PrintStream ps1=System.out;
ps1.println(false);
//输出到文件
File src=new File("H:/1/1.txt");

ps1=new PrintStream(new BufferedOutputStream(
new FileOutputStream(src)));

ps1.print("IO12346");

ps1.close();
}

}

System的三个常量及重定向

package otherio;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.Scanner;
/*
* 161 三个常量
* 1.System.in 输入流   键盘,文件输入
* 2.System.out 输出流  控制台输出
* 3.System.err 输出流  控制台输出
*
* 重定向:
* setIn()
* setOut()
* setErr()
*
* FileDescriptor.in
* FileDescriptor.out
* FileDescriptor.err
* */
public class SystemDemo1 {

public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
test2();
//重定向  控制台---》文件
System.setOut(new PrintStream(new BufferedOutputStream(
new FileOutputStream("H:1/1.txt")),true));
System.out.println("重定向到文件");
//重定向  文件---》控制台
System.setOut(new PrintStream(new BufferedOutputStream(
new FileOutputStream(FileDescriptor.out)),true));
System.out.println("重定向到控制台");
}
public static void test2() throws FileNotFoundException{
InputStream is1=System.in;
//从文件中输入,而不是键盘 (is1已经不再是System.in,而是FileInputStream)
is1=new BufferedInputStream(new FileInputStream("H:/1/1.txt"));

Scanner sc1=new Scanner(is1);
System.out.println(sc1.nextLine());
}

}

IO设计模式

package patterio;
/*
* 162 IO里的设计模式:装饰设计模式
* 类与类之间的关系:
* 1.依赖:形参或者局部变量
* 2.关联:属性(本例中Amplifier类中的voice对象就被看为属性)
*     聚合:属性 整体与部分 不一致的生命周期
*     组合:属性 整体与部分 一致的生命周期
* 3.继承:父子类关系
* 4.实现:接口与实现类的关系
* */
public class App {
public static void main(String[] args) {
Voice v1=new Voice();
v1.say();
Amplifier a1=new Amplifier(v1);
a1.say();
}
}
package patterio;
//162
public class Voice {
private int voice=10;

public Voice() {
}
public Voice(int voice) {
super();
this.voice = voice;
}

public int getVoice() {
return voice;
}

public void setVoice(int voice) {
this.voice = voice;
}
public void say() {
System.out.println("音量:"+voice);
}
}
package patterio;
//162
public class Amplifier {
private Voice voice;

public Amplifier() {
// TODO Auto-generated constructor stub
}
public Amplifier(Voice voice) {
super();
this.voice = voice;
}
public void say() {
System.out.println("音量:"+voice.getVoice()*100);;
}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: