import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintStream;
import java.io.RandomAccessFile;
import java.io.Serializable;
import org.junit.Test;
public class TestOtherStream {
/**
*RandomAccessFile:支持随机访问
*1.可以充当一个输入流,又可以充当输出流(test3)
*2.支持文件的开头读取,写入,
*3.也可以支持从任意位置的的读取,写入(test4)
*
*支持随机
* @throws Exception
*/
@Test
public void test3() throws Exception{
File file = new File("hello.txt");
File file1 = new File("hello1.txt");
RandomAccessFile raf = new RandomAccessFile(file, "r");
RandomAccessFile raf1 = new RandomAccessFile(file1,"rw");
byte[] b = new byte[20];
int len;
while((len = raf.read(b)) != -1){
raf1.write(b, 0, len);
}
raf1.close();
raf.close();
}
//实现了覆盖的操作
@Test
public void test4() throws Exception{
File file = new File("hello.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(3);
raf.write("xy".getBytes());
}
//实现插入
@Test
public void test5() throws Exception{
File file = new File("hello1.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(4);
String str = raf.readLine();
raf.seek(4);
raf.write("xy".getBytes());
raf.write(str.getBytes());
}
//实现插入(复杂文件,也就是有多行)
@Test
public void test6() throws Exception{
File file = new File("hello1.txt");
RandomAccessFile raf = new RandomAccessFile(file, "rw");
raf.seek(4);
byte[] b = new byte[20];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf.read(b)) != -1){
sb.append(new String(b,0,len));
}
raf.seek(4);
raf.write("xy".getBytes());
raf.write(sb.toString().getBytes());
}
//反序列号,将硬盘中的文件通过ObjectIntputStream转为相应的对象,存储到硬盘中
@Test
public void testObject1() throws Exception{
FileInputStream fis = new FileInputStream("/Users/lixiuming/Desktop/node/person.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person p1 = new Person();
p1 = (Person) ois.readObject();
Person p2 = new Person();
p2 = (Person) ois.readObject();
System.out.println(p1+"*******"+p2);
}
// 对象的序列化过程,讲内存中的对象通过ObjectOutputStream转化二进制流,存在硬盘中
@Test
public void testObject() {
Person p1 = new Person("小米", 20);
Person p2 = new Person("红米", 23);
ObjectOutputStream oos = null;
try {
FileOutputStream fos = new FileOutputStream("/Users/lixiuming/Desktop/node/person.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(p1);
oos.flush();
oos.writeObject(p2);
oos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
// 用数据流来解析文件
@Test
public void testData1() throws Exception {
File file = new File("data.txt");
FileInputStream fis = new FileInputStream(file);
DataInputStream dis = new DataInputStream(fis);
// int len;
// byte[] b = new byte[20];
// while((len = dis.read(b)) != -1){
// String str = new String(b,0,len);
// System.out.println(str);
// }
String str = dis.readUTF();
Boolean b = dis.readBoolean();
long l = dis.readLong();
System.out.println("str:" + str + "," + "Bollean:" + b + "," + "long:" + b);
}
/**
* 数据流 ,用来处理基本数据类型,String , 字节数组的数据:DatainputStream,DataOutputStream
*
* @throws Exception
*/
@Test
public void testData() throws Exception {
FileOutputStream fos = new FileOutputStream("data.txt");
DataOutputStream dos = new DataOutputStream(fos);
dos.writeUTF("ksdflskdfhslkdf");
dos.writeBoolean(true);
dos.writeLong(123123);
dos.close();
fos.close();
}
/**
* 打印流(处理流),字节流,PrintStream 字符流,printWriter
*
* @throws Exception
*
*
*/
@Test
public void printSrtreamWriter() throws Exception {
File file = new File("hello7.txt");
FileOutputStream fos = new FileOutputStream(file);
// 设置打印位置
PrintStream ps = new PrintStream(fos, true);
System.setOut(ps);
for (int i = 0; i < 255; i++) {
System.out.print((char) i);
if (i % 50 == 0) {
System.out.println();// 换行
}
}
ps.close();
}
/**
* 标注的输入输出流 标准的输出流(字节流),System.in, 标准的输入流(字节流) System.out
* 键盘输入,把输入内容转化成大写,输出,当直接输入“e”或者“exit”时,退出程序
*
* @throws Exception
*/
@Test
public void test2() throws Exception {
InputStream is = System.in;
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String str = null;
while (true) {
System.out.print("请输入字符串:");
str = br.readLine();
if (str.equalsIgnoreCase("e") || str.equalsIgnoreCase("exit")) {
break;
} else {
String str1 = str.toUpperCase();
System.out.println(str1);
}
}
br.close();
isr.close();
is.close();
}
/**
* 转换流,InputStreamReader OutputStreamWriter 编码:将字符串转化为字节数组 解码:字节数组转化为字符串
*
*/
@Test
public void test1() throws Exception {
// 解码
File file = new File("hello3.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis, "utf-8");
BufferedReader br = new BufferedReader(isr);
// 编码
File file2 = new File("hello6.txt");
FileOutputStream fos = new FileOutputStream(file2);
OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");
BufferedWriter bw = new BufferedWriter(osw);
int len;
char[] c = new char[100];
while ((len = br.read(c)) != -1) {
// String str = new String(c,0,len);
// System.out.println(str);
bw.write(c, 0, len);
bw.flush();
}
bw.close();
osw.close();
fos.close();
br.close();
isr.close();
fis.close();
}
}
/**
* 要实现序列号的类, 要求是可序列化的:实现两个接口之一,Serializable
* 提供一个序列号,作用是:防止出错
* 不可序列号:static和transient修饰的成员变量
*/
class Person implements Serializable {
private static final long serialVersionUID = 1L;
String name;
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(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Person() {
super();
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
}