package IOTest;

 import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.RandomAccessFile; public class IOMain { public static void main(String[] args) throws IOException { /*
* 1
*/
// try (FileReader fr = new FileReader("E:/Workspase/MyEclipse 2015 CI/Shulie/src/A2.java")) {
// char[] cbuf = new char[32];
// int hasRead = 0;
// while((hasRead = fr.read(cbuf))>0){
// System.out.println(new String(cbuf, 0, hasRead));
// }
//
// } catch (FileNotFoundException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } /*
* 2 节点流
*/
// File f = new File("newfile.txt");
//
// try(
// FileInputStream fis = new FileInputStream("E:/Workspase/MyEclipse 2015 CI/Shulie/src/A2.java");
// FileOutputStream fos = new FileOutputStream(f))
// {
// byte[] buf = new byte[1024];
// int hasRead = 0;
// while((hasRead = fis.read(buf))>0){
// fos.write(buf, 0, hasRead);
// }
//
// }catch(Exception e){
// e.printStackTrace();
// } /*
* 3 转换流
*/
// try (InputStreamReader reader = new InputStreamReader(System.in);
// BufferedReader bf = new BufferedReader(reader) ){
// String line = null;
// while((line = bf.readLine()) !=null){
// if("exit".equals(line)){
// System.exit(1);
// }
// System.out.println(line);
// }
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } /*
* 4 重定向输出
*/
// try (PrintStream ps = new PrintStream(new FileOutputStream("out.txt"))) {
//
// System.setOut(ps);
// System.out.println(new Scanner(System.in).next());
// System.out.print(new IOMain());
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
// /*
* 5 RandomAccessFile
*/
//
// try (RandomAccessFile raf = new RandomAccessFile("newfile.txt", "rw");) {
// System.out.println("指针初始位置:"+raf.getFilePointer());
// raf.seek(300);
// byte[] bbuff = new byte[1024];
// int hasRead = 0;
// while((hasRead = raf.read(bbuff)) > 0){
// System.out.println(new String(bbuff, 0, hasRead));
// }
// raf.seek(raf.length());
// raf.write("\r\n追加的内容!\r\n".getBytes());
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
//
//
// insert("out.txt", 3, "\r\n插入的内容\r\n");
// FileInputStream fis = new FileInputStream(new File("out.txt"));
// byte[] bbuff = new byte[1024];
// int hasRead = 0;
// while((hasRead = fis.read(bbuff)) > 0){
// System.out.println(new String(bbuff, 0, hasRead));
// }
//
//
/*
* 6 序列化
*/ // try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.txt"));) {
// Person p1 = new Person("Jeff", 23);
// oos.writeObject(p1);
// } catch (Exception e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } try (ObjectInputStream oip = new ObjectInputStream(new FileInputStream("object.txt"));) {
Person p2 = (Person) oip.readObject();
System.out.println("name:"+p2.getName()+"\n"+"age:"+p2.getAge());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
public static void insert(String fileName, long pos, String insertContent) throws IOException{
File temp = File.createTempFile("temp", null);
temp.deleteOnExit();
try (
RandomAccessFile raf = new RandomAccessFile(fileName, "rw");
FileOutputStream fos = new FileOutputStream(temp);
FileInputStream fis = new FileInputStream(temp);) {
raf.seek(pos);
byte[] buff = new byte[64];
int hasRead = 0;
while((hasRead = raf.read(buff))>0){
fos.write(buff, 0, hasRead);
}
raf.seek(pos);
raf.write(insertContent.getBytes());
while((hasRead = fis.read(buff))>0){
raf.write(buff, 0, hasRead);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
class Person implements java.io.Serializable{
private String name;
private int age;
public Person(String name, int age){
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;
} }
class PersonNew implements java.io.Externalizable{
private String name;
private int age;
public PersonNew(String name, int age){
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;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
out.writeObject(new StringBuffer(name).reverse());
out.writeInt(age); }
@Override
public void readExternal(ObjectInput in) throws IOException,
ClassNotFoundException {
this.name = ((StringBuffer)in.readObject()).reverse().toString();
this.age = in.readInt();
} }
05-11 13:16