ByteArrayStream
package iootherstudy;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
public class ByteArrayStream {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
/**
* 从内存缓冲数组读取数据
*/
byte[] but = "中国".getBytes();
ByteArrayInputStream bais = new ByteArrayInputStream(but);
int len = bais.available();
byte[] buffer = new byte[len];
bais.read(buffer);
String str = new String(buffer);
System.out.println(str);
/**
* 往内存中写数据
*/
ByteArrayOutputStream baos = new ByteArrayOutputStream();
System.out.println("baos.size()" + baos.size());
baos.write("中国人国好啊".getBytes());
System.out.println("baos.size()" + baos.size());
byte[] buta = baos.toByteArray();
System.out.println("buta.length" + buta.length);
System.out.println(new String(buta));
/**
* 结合数据格式化使用
*/
DataOutputStream dos = new DataOutputStream(new ByteArrayOutputStream());
dos.write(98);
}
}
DataStream
package iootherstudy;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class DataStreamTest {
public static void main(String[] args) throws IOException {
DataOutputStream dos = new DataOutputStream(new FileOutputStream(
"day16/a.txt"));
int num = 98;
String str = "abc中国人";
// dos.write(num);
dos.writeUTF(str);
dos.writeInt(num);
dos.close();
DataInputStream dis = new DataInputStream(new FileInputStream(
"day16/a.txt"));
// int numNow = dis.readInt();
// System.out.println(numNow);
String s = dis.readUTF();
System.out.println(s);
}
}
PipedStream使用
package iootherstudy;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class PipedStreamTest {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream(pis);
pos.write("欢迎访问本网站~~~~".getBytes());
int len = pis.available();
byte[] but = new byte[len];
pis.read(but);
System.out.println(new String(but));
pis.close();
pos.close();
}
}
RandomAccessFile使用
package iootherstudy;
import java.io.IOException;
import java.io.RandomAccessFile;
public class RandomAccessFileTest {
/**
* 软件30次到期,提示
* @throws IOException
* @throws NumberFormatException
*/
public static void main(String[] args) throws NumberFormatException, IOException {
RandomAccessFile raf = new RandomAccessFile("day16/a.txt", "rw");
/**
* 第一种
*/
/*
int count = Integer.parseInt(raf.readLine());
if (count == 30) {
System.out.println("过期~~~~");
return;
}
count++;
System.out.println("第" + count + "次使用");
raf.seek(0);
raf.write(String.valueOf(count).getBytes());
raf.close();
*/
/**
* 第二种
*/
// 首先写入格式化数据0
// raf.writeInt(0);
int count = raf.readInt();
if (count > 5) {
System.out.println("过期");
return;
}
count++;
System.out.println("第" + count);
raf.seek(0);
raf.writeInt(count);
raf.close();
}
}