1つ、シーケンスフロー
- シーケンスストリームとは何ですか?シーケンスストリームは、
複数のバイト入力ストリームを1つに統合できます。シーケンスストリームからデータを読み取る場合、統合される最初のストリームから読み取りを開始し、1つを読み取った後も2番目のストリームを読み取り続けます。等々。
統合2の使用方法:SequenceInputStream(InputStream、InputStream)
ケース:シーケンスフローを使用する前
FileInputStream fileInputStream1 =new FileInputStream("a.txt");
//创建字节输入流关联a.txt
FileOutputStream fileOutputStream =new FileOutputStream("c.txt");
//创建字节输出流关联c.txt
int b1;
while ((b1=fileInputStream1.read())!=-1) {
fileOutputStream.write(b1);
}
fileInputStream1.close();
int b2;
FileInputStream fileInputStream2 =new FileInputStream("b.txt");
while ((b2=fileInputStream2.read())!=-1) {
fileOutputStream.write(b2);
}
fileInputStream2.close();
fileOutputStream.close();
SquenceInputStreamを使用すると、2つのストリームを1つのストリームに統合できます
FileInputStream fis1 =new FileInputStream("a.txt");
FileInputStream fis2 =new FileInputStream("b.txt");
SequenceInputStream sis =new SequenceInputStream(fis1,fis2);
FileOutputStream fos =new FileOutputStream("c.txt");
int b;
while ((b=sis.read())!=-1) {
fos.write(b);
}
sis.close();
fos.close();
効果は次のとおりです。
2つ目は、シーケンスフローが複数を統合することです。
アプリケーションシナリオでは、曲の串を作ることができます。好きな曲を1つの曲に統合します。
FileInputStream fis1=new FileInputStream("a.txt");
FileInputStream fis3=new FileInputStream("c.txt");
Vector<FileInputStream> vector =new Vector<FileInputStream>();
vector.add(fis1);
vector.add(fis2);
vector.add(fis3);
Enumeration<FileInputStream> en =vector.elements();
SequenceInputStream sis =new SequenceInputStream(en);
FileOutputStream fos =new FileOutputStream("d.txt");
int b;
while ((b=sis.read())!=-1) {
fos.write(b);
}
sis.close();
fos.close();
効果は次のとおりです。
3、メモリ出力ストリーム
- メモリ出力ストリームとは何ですか?
この出力ストリームは、メモリをバッファとして使用してメモリにデータを書き込むことができます。書き込み後、すべてのデータを一度に取得できます。 - 使用方法
Createobject:new ByteArrayOutputStream()
Write out data:write(int)、write(byte [])
Get data:toByteArray()
FileInputStream fileInputStream =new FileInputStream("e.txt");
//在内存中创建了可以增长的内存数组
ByteArrayOutputStream baos =new ByteArrayOutputStream();
int b;
while ((b=fileInputStream.read())!=-1) {
baos.write(b);
}
//将缓冲区的庶几乎全部获取出来,并赋值给arr数组
byte []arr=baos.toByteArray();
System.out.println(new String(arr));
//上面两句可以直接转换直接用下面的语句进行赋值
System.out.println(baos.toString());
//将缓冲区的内容转换为字符串,可以省略调用toString方法
fileInputStream.close();
注意バイト[] arr = baos.toByteArray();
System.out.println(new String(arr));
== System.out.println(baos.toString()); ==
この関数は、チャットレコードを呼び出し、データベースファイルを使用してローカルデータを呼び出し、コンテンツに保存して一度に呼び出すことができます。
第四に、オブジェクト操作ストリームObjecOutputStream-シリアル化
- オブジェクト操作フローとは何ですか?
フローは、オブジェクトをプログラムに書き込んだり、プログラムに読み込んだりすることができます。つまり、シリアル化および逆シリアル化操作が実行されます。ゲームのプレイ方法と同様に、ゲームキャラクターのレベルと装備をオブジェクトの形式でデータベースまたはサーバーに保存します。 - 使用方法
:new ObjectOutputStream(OutputStream), writeObject()
ケース:
ステップ1:
新しいドメインクラスを作成します。シリアル化を実現する場合は注意してください。Serializableインターフェースを参照する必要があります
public class Person implements Serializable {
private String name;
private int 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;
}
public Person() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
}
ステップ2:データを読み取る
Person p1=new Person("张三",12);
Person p2=new Person("李四",13);
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("e.txt"));
oos.writeObject(p1);
oos.writeObject(p2);
oos.close();
効果は次のとおりです。
5、オブジェクト操作ストリームObjectInputStream-逆シリアル化
読んだ: new ObjectInputStream(InputStream), readObject()
public static void main(String[] args) throws IOException, ClassNotFoundException{
ObjectInputStream ois =new ObjectInputStream(new FileInputStream("e.txt"));
Person p1=(Person)ois.readObject();
Person p2=(Person)ois.readObject();
System.out.println(p1);
System.out.println(p2);
ois.close();
}
効果は次のとおり
です。もう1つのオブジェクトが書き込まれると、エラーが報告されます。
6、オブジェクト操作フローの最適化
オブジェクトをコレクションに保存して書き出し、コレクションを1回だけ書き込み、最後の読み取りで1つのコレクションのみを読み取ってから、コレクションをトラバースします。
最初に書く
Person p1=new Person("张三",12);
Person p2=new Person("李四",13);
Person p3=new Person("王五",14);
Person p4=new Person("赵六",15);
List<Person> list =new ArrayList<Person>();
list.add(p1);
list.add(p1);
list.add(p3);
list.add(p4);
ObjectOutputStream oos =new ObjectOutputStream(new FileOutputStream("e.txt"));
oos.writeObject(list);
oos.close();
読み取り操作
ObjectInputStream ois =new ObjectInputStream(new FileInputStream("e.txt"));
ArrayList<Person>list=(ArrayList<Person>)ois.readObject();
for (Person person : list) {
System.out.println(person);
}
ois.close();
上記の2つの操作を実行して記述します。