IO流_5(その他のストリーム)
PrintStream
特徴は、印刷方法 を提供する。彼はIOException を捨てません。
SequenceInputStream
特徴
シーケンスストリームは複数のストリームをマージします。
例
1.txt、2.txt、3.txtの中のデータを4.txtの中に合併します。
オブジェクトを操作するフロー
コード
Serializable:は、プログレッシブされたクラスにID番号 を追加するために使用される。は、クラスとオブジェクトが同じバージョンかどうかを判断するために使用される 。
特徴このオブジェクトは読めますし、 も書けます。このオブジェクトの内部は1つのbyte配列を維持し、配列内の要素 をポインタで操作することができる。は、get FilePointer方式によりポインタの位置を取得し、seek方式によりポインタを設定する位置 と、実はこのオブジェクトはバイト入力ストリームと出力ストリームをカプセル化した である。このオブジェクトのソースと目的はファイルだけです。 コード
入出力は直接接続できます。マルチスレッドを組み合わせて使用します。
操作基本データタイプはこのストリームを使います。
特徴
print()
が複数のデータタイプの値を印刷し、データの表現形式を維持することができるが、データのサイズ(すなわち、入力と出力のものがそっくりに見える)を保証しないSequenceInputStream
特徴
シーケンスストリームは複数のストリームをマージします。
例
1.txt、2.txt、3.txtの中のデータを4.txtの中に合併します。
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
al.add(new FileInputStream("D:\\1.txt"));
al.add(new FileInputStream("D:\\2.txt"));
al.add(new FileInputStream("D:\\3.txt"));
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream("D:\\4.txt");
byte[] b = new byte[1024];
int len = 0;
while((len=sis.read(b)) != -1) {
fos.write(b,0,len);
}
sis.close();
fos.close();
}
}
ファイル切断public class SplitFile {
private static final int SIZE = 1024*1024;
public static void main(String[] args) throws IOException {
//File file = new File("D:\\IOtest\\1.png");
File file = new File("E:\\Pictures\\ .jpg");
splitFile(file);
}
private static void splitFile(File file) throws IOException {
//
FileInputStream fis = new FileInputStream(file);
// 1M
byte[] buf = new byte[SIZE];
//
FileOutputStream fos = null;
int len = 0;
int count = 1;
File dir = new File("D:\\IOtest\\part");
if(!dir.exists()) {
dir.mkdirs();
}
while((len = fis.read(buf)) != -1) {
fos = new FileOutputStream(new File(dir,count++ + ".part"));
fos.write(buf,0,len);
}
//
Properties prop = new Properties();
prop.setProperty("partCount", count+"");
prop.setProperty("fileName", file.getName());
prop.store(new FileOutputStream(new File(dir,"my.properties")), "save file info");
fis.close();
fos.close();
}
}
ファイルをマージpublic class MergeFile {
public static void main(String[] args) throws IOException {
File dir = new File("D:\\IOtest\\part");
mergeFile(dir);
}
private static void mergeFile(File dir) throws IOException {
//
Properties prop = getConfig(dir);
String partCount = (String) prop.get("partCount");
int count = Integer.parseInt(partCount);
//
validateConfig(count,dir);
ArrayList<FileInputStream> al = new ArrayList<FileInputStream>();
for (int i = 1; i < count; i++) {
al.add(new FileInputStream(new File(dir,i+".part")));
}
Enumeration<FileInputStream> en = Collections.enumeration(al);
SequenceInputStream sis = new SequenceInputStream(en);
FileOutputStream fos = new FileOutputStream(new File(dir,prop.getProperty("fileName")));
byte[] buf = new byte[1024];
int len = 0;
while((len = sis.read(buf)) != -1) {
fos.write(buf, 0, len);
}
sis.close();
fos.close();
}
private static void validateConfig(int count, File dir) {
String[] list = dir.list(new FilenameFilter() {
public boolean accept(File arg0, String arg1) {
boolean endsWith = arg1.endsWith(".part");
return endsWith;
}
});
if (count - 1 != list.length) {
throw new RuntimeException(" ");
}
}
private static Properties getConfig(File dir) throws IOException {
Properties prop = new Properties();
File file = new File(dir,"my.properties");
if(!file.exists())
throw new RuntimeException(" ");
prop.load(new FileInputStream(file));
return prop;
}
}
Object Streamオブジェクトを操作するフロー
コード
public class ObjectOutputStreamDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
//writeObj();
readerObj();
}
private static void readerObj() throws IOException, ClassNotFoundException {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IOtest\\obj.object"));
Person p = (Person) ois.readObject();
System.out.println(p.getName()+":"+p.getAge());
}
private static void writeObj() throws IOException {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IOtest\\obj.object"));
oos.writeObject(new Person(" ",18));
oos.close();
}
}
オブジェクトの順序付けSerializable:
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private int age;
public Person(String name, int age) {
super();
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;
}
}
Random Access File特徴
public class RandomAccessFileDemo {
public static void main(String[] args) throws IOException {
/*
* :
* 1. ,
* 2. byte ,
* 3. getFilePointer , seek
* 4.
* 5. 。
*/
writerFile();
readerFile();
}
private static void readerFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:\\IOtest\\random.txt", "r");
// seek
raf.seek(0*8);// 。
byte[] buf = new byte[4];
raf.read(buf);
String name = new String(buf);
int age = raf.readInt();
System.out.println("name="+name);
System.out.println("age="+age);
System.out.println("pos:"+raf.getFilePointer());
raf.close();
}
// RandomAccessFile ,
private static void writerFile() throws IOException {
RandomAccessFile raf = new RandomAccessFile("D:\\IOtest\\random.txt", "rw");
raf.write(" ".getBytes());
raf.writeInt(97);
raf.write(" ".getBytes());
raf.writeInt(32);
raf.close();
}
}
パイプ?フロー入出力は直接接続できます。マルチスレッドを組み合わせて使用します。
public class PipedStreamDemo {
public static void main(String[] args) throws IOException {
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
in.connect(out);
new Thread(new Input(in)).start();
new Thread(new Output(out)).start();
}
}
class Input implements Runnable{
PipedInputStream in;
Input(PipedInputStream in){
this.in = in;
}
@Override
public void run() {
try {
byte[] buf = new byte[1024];
int len = in.read(buf);
String s = new String(buf,0,len);
System.out.println(s);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Output implements Runnable{
PipedOutputStream out;
Output(PipedOutputStream out){
this.out = out;
}
@Override
public void run() {
try {
Thread.sleep(5000);
out.write(" !".getBytes());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
DataStream操作基本データタイプはこのストリームを使います。
public class DataStreamDemo {
public static void main(String[] args) throws IOException {
writeDate();
readDate();
}
@SuppressWarnings("resource")
private static void readDate() throws IOException {
DataInputStream dis = new DataInputStream(new FileInputStream("D:\\IOtest\\data.txt"));
String s = dis.readUTF();
System.out.println(s);
dis.close();
}
@SuppressWarnings("resource")
private static void writeDate() throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream("D:\\IOtest\\data.txt"));
dos.writeUTF(" ");
dos.close();
}
}