package com.qxtest.iostudy; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; 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.FileReader; import java.io.FileWriter; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import java.io.RandomAccessFile; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.util.Arrays; public class IOStudy { public static void main(String[] args) { // byteIO(); // charIO(); // charIOReadLine(); // convert(); // byteArrIO("自动化测试"); // dataIOWrite(); // dataIORead(); // objectWrite(); // objectRead(); rndIO(); } //字节流 public static void byteIO() { long startTime = System.currentTimeMillis();//获取当前时间 //建立联系 System.out.println("1、建立联系"); File src = new File("E:/movie/Visio2010-xdowns.rar"); File dest = new File("E:/updateDir/testTo.rar"); //选择流 System.out.println("2、选择流"); InputStream is = null; OutputStream os = null; try { //程序运行时间:1025ms is = new BufferedInputStream(new FileInputStream(src)); os = new BufferedOutputStream(new FileOutputStream(dest)); //程序运行时间:4386ms // is = new FileInputStream(src); // os = new FileOutputStream(dest); //循环读取 输出 System.out.println("3、循环读取 输出 强制刷出"); byte[] flush = new byte[1024]; int len = 0; while(-1!=(len=is.read(flush))){ // System.out.println(len); // System.out.println(Arrays.toString(flush)); // System.out.println(new String(flush)); os.write(flush, 0, len); } //强制刷出 os.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { //释放资源 System.out.println("4、释放资源"); try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } System.out.println("ok"); long endTime = System.currentTimeMillis(); System.out.println("程序运行时间:"+(endTime-startTime)+"ms"); } //字符流 public static void charIO(){ File src = new File("E:/updateDir/test.sql"); File dest = new File("E:/updateDir/testTo.sql"); Reader is = null; Writer os = null; try { is = new BufferedReader(new FileReader(src)); os = new BufferedWriter(new FileWriter(dest)); char[] flush = new char[2014]; int len = 0; while(-1 != (len=is.read(flush))){ os.write(flush, 0, len); } os.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //缓冲流readLine()方法 public static void charIOReadLine(){ File src = new File("E:/updateDir/test.sql"); File dest = new File("E:/updateDir/testTo.sql"); BufferedReader is = null; BufferedWriter os = null; try { is = new BufferedReader(new FileReader(src)); os = new BufferedWriter(new FileWriter(dest)); // char[] flush = new char[2014]; // int len = 0; // while(-1 != (len=is.read(flush))){ // os.write(flush, 0, len); // } String line = null; while(null != (line=is.readLine())){ os.write(line); os.newLine(); } os.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //转换流 public static void convert(){ File src = new File("E:/updateDir/test.sql"); File dest = new File("E:/updateDir/testTo.sql"); BufferedReader br = null; BufferedWriter bw = null; try { br = new BufferedReader(new InputStreamReader(new FileInputStream(src), "GBK")); bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(dest), "UTF-8")); String line = null; while(null != (line=br.readLine())){ bw.write(line); bw.newLine(); } bw.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bw.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { br.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //字节数组流 public static void byteArrIO(String src){ // String src = "just a test"; InputStream is = new BufferedInputStream(new ByteArrayInputStream(src.getBytes())); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] flush = new byte[1024]; int len = 0; try { while(-1 != (len = is.read(flush))){ bos.write(flush, 0, len); } bos.flush(); byte[] res = bos.toByteArray(); System.out.println(new String(res)); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //基础数据流写 public static void dataIOWrite(){ double j = 1023.999; int i = 10; long k = 100L; String s = "自动化测试autotest"; File dest = new File("E:/updateDir/testTo.data"); DataOutputStream dos = null; try { dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); dos.writeDouble(j); dos.writeInt(i); dos.writeLong(k); dos.writeUTF(s); dos.flush(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //基础数据流读 public static void dataIORead(){ File src = new File("E:/updateDir/testTo.data"); DataInputStream dis = null; try { dis = new DataInputStream(new BufferedInputStream(new FileInputStream(src))); double j = dis.readDouble(); int i = dis.readInt(); long k = dis.readLong(); String s = dis.readUTF(); System.out.println(j+"-->"+i+"-->>"+k+"-->>"+s); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //对象流写 序列化 public static void objectWrite(){ //源 User u = new User(10433, "瞿小渣", "tester"); int[] arr ={1,2,3,45}; File dest = new File("E:/updateDir/testTO.obj"); //选择流 ObjectOutputStream oos = null; try { oos = new ObjectOutputStream(new BufferedOutputStream(new FileOutputStream(dest))); oos.writeObject(u); oos.writeObject(arr); oos.flush(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { oos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //对象流读 反序列化 public static void objectRead(){ File src = new File("E:/updateDir/testTO.obj"); ObjectInputStream ois = null; try { ois = new ObjectInputStream(new BufferedInputStream(new FileInputStream(src))); Object o = ois.readObject(); if(o instanceof User){ User u = (User)o; //job没有序列化 为null System.out.println(u.getId()+"-->"+u.getName()+"-->"+u.getJob()); } o = ois.readObject(); int[] arr=(int[])o; System.out.println(Arrays.toString(arr)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { ois.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //随机读取流 public static void rndIO(){ File src = new File("E:/updateDir/test.py"); RandomAccessFile rnd = null; try { rnd = new RandomAccessFile(src, "r"); rnd.seek(100); byte[] flush = new byte[100]; int len = 0; while(-1 != (len=rnd.read(flush))){ System.out.println(">>"+new String(flush,0,len)); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }