IO

来源:互联网 发布:三星note8画图软件 编辑:程序博客网 时间:2024/06/03 21:58
/* *IO:输入、输出的一些简单实例。 */package classfile;import java.io.*;import java.util.*;public class IODemo{public static void main(String[] args){method_InputAndOutput();method_ReaderAndWriter();method_InputAndWriter();method_MyReadLine();method_KeyboardAndPrint();method_TraversalDirectory();method_SequenceAndSplit();method_ObjectStream();method_RandomAccessFile();method_Charset();}public static void method_InputAndOutput(){//复制一个图片文件BufferedInputStream bis = null;BufferedOutputStream bos = null;try{bis = new BufferedInputStream(new FileInputStream("SecondaryFile\\IODemoOne.jpg"));bos = new BufferedOutputStream(new FileOutputStream("SecondaryFile\\IODemoOneCopy.jpg"));byte[] arr = new byte[1024];int length = 0;while((length=bis.read(arr))!=-1){bos.write(arr,0,length);}}catch (Exception e){e.printStackTrace();}finally{if(bis!=null)try{bis.close();}catch (Exception e){e.printStackTrace();}if(bos!=null)try{bos.close();}catch (Exception e){e.printStackTrace();}}}public static void method_ReaderAndWriter(){//复制一个文本文件BufferedReader br = null;BufferedWriter bw = null;try{br = new BufferedReader(new FileReader("SecondaryFile\\IODemoTwo.txt"));bw = new BufferedWriter(new FileWriter("SecondaryFile\\IODemoTwoCopy.txt"));char[] arr = new char[1024];int length = 0;while((length=br.read(arr))!=-1){bw.write(arr,0,length);}}catch (Exception e){e.printStackTrace();}finally{if(br!=null)try{br.close();}catch (Exception e){e.printStackTrace();}if(bw!=null)try{bw.close();}catch (Exception e){e.printStackTrace();}}}public static void method_InputAndWriter(){//将一个文本文件输出在控制台上BufferedReader br = null;PrintWriter pw = null;try{br = new BufferedReader(new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoTwo.txt")));pw = new PrintWriter(System.out,true);char[] arr = new char[1024];int length = 0;while((length=br.read(arr))!=-1){//pw.write(arr,0,length);pw.println(String.valueOf(arr,0,length));}}catch (Exception e){e.printStackTrace();}finally{if(br!=null)try{br.close();}catch (Exception e){e.printStackTrace();}/*此处不关闭输出流,是为了后续的使用if(pw!=null)try{pw.close();}catch (Exception e){e.printStackTrace();}*/}System.out.println("---------method_InputAndWriter run over------------");}public static void method_MyReadLine(){/** *方法内部类,装饰了一个文件读取流,具备读一行的功能。 */class MyFileReader{private Reader r = null;/** *对象一被初始化,就有了一个文件读取流与之相对应。 * *param r *一个文件读取流 */public MyFileReader(Reader r){this.r = r;}/** *传递文件读取流的读方法 */public int read()throws IOException{return r.read();}/** *可以按行读取文本的方法。对读一个字符方法的装饰。 */public String readLine()throws IOException{//定义一个字符串缓冲区,用于存储读取到的一行文本StringBuilder sr = new StringBuilder();//定义一个变量,用于接收读取到的字符数据int ch = 0;while((ch=r.read())!=-1){if(ch=='\r')//适用于window系统{continue;}if(ch=='\n'){return sr.toString();}else{sr.append((char)ch);}}if(sr.length()!=0)//避免最后一行没有换行符,造成的数据丢失。{return sr.toString();}return null;}/** *传递文件读取流的关闭方法。 */public void close()throws IOException{r.close();}}MyFileReader mfr = null;PrintWriter pw = null;try{mfr = new MyFileReader(new FileReader("SecondaryFile\\IODemoTwo.txt"));pw = new PrintWriter(System.out,true);String str = null;while((str=mfr.readLine())!=null){pw.println(str);}}catch (Exception e){e.printStackTrace();}finally{if(mfr!=null)try{mfr.close();}catch (Exception e){e.printStackTrace();}/*此处不关闭输出流,是为了后续的使用if(pw!=null)try{pw.close();}catch (Exception e){e.printStackTrace();}*/}System.out.println("---------method_MyReadLine run over------------");}public static void method_KeyboardAndPrint(){BufferedReader br = null;PrintWriter pw = null;try{br = new BufferedReader(new InputStreamReader(System.in));pw = new PrintWriter(System.out,true);String str = null;while((str=br.readLine())!=null){if(!"over".equals(str)){pw.println(str.toUpperCase());}else{return;}}}catch (Exception e){e.printStackTrace();}finally{if(br!=null)try{br.close();}catch (Exception e){e.printStackTrace();}/*此处不关闭输出流,是为了后续的使用。if(pw!=null)try{pw.close();}catch (Exception e){e.printStackTrace();}*/}}public static void method_TraversalDirectory(){StringBuilder sr = new StringBuilder();/** *方法内部类,用于提供遍历目录文件的功能。 */class TraversalDirectory{/** *遍历目录文件,并记录其中的文件名称。 * *param dir *一个目录文件 */public void traversalDirectory(File dir){sr.append(dir+"\r\n");File[] files = dir.listFiles();for(int i=0;i<files.length;i++){if(files[i].isDirectory()){traversalDirectory(files[i]);}else{if(files[i].getName().endsWith(".avi")){sr.append(files[i]+"\r\n");}}}}}new TraversalDirectory().traversalDirectory(new File("D:\\学习相关资料\\黑马程序员_毕向东最新经典Java基础视频"));PrintWriter pw = null;try{pw = new PrintWriter("SecondaryFile\\IODemoThree_TraversalDirectory.txt");pw.println(sr.toString());}catch (Exception e){e.printStackTrace();}finally{if(pw!=null)try{pw.close();}catch (Exception e){e.printStackTrace();}}}public static void method_SequenceAndSplit(){BufferedInputStream bis = null;BufferedOutputStream bos = null;int count = 1;try{bis = new BufferedInputStream(new FileInputStream("SecondaryFile\\IODemoOne.jpg"));byte[] arr = new byte[1024*32];int length = 0;while((length=bis.read(arr))!=-1){bos = new BufferedOutputStream(new FileOutputStream("SecondaryFile\\Split_"+(count++)+".partfile"));bos.write(arr,0,length);}}catch (Exception e){e.printStackTrace();}finally{if(bis!=null)try{bis.close();}catch (Exception e){e.printStackTrace();}if(bos!=null)try{bos.close();}catch (Exception e){e.printStackTrace();}}//---------在本方法中--以上为文件切割--以下为文件合并------------------List<InputStream> list = new ArrayList<InputStream>();SequenceInputStream sis = null;try{for(int i=1;i<count;i++){list.add(new BufferedInputStream(new FileInputStream("SecondaryFile\\Split_"+i+".partfile")));}Iterator<InputStream> iterator = list.iterator();sis = new SequenceInputStream(new Enumeration<InputStream>(){public boolean hasMoreElements(){return iterator.hasNext();}public InputStream nextElement(){return iterator.next();}});bos = new BufferedOutputStream(new FileOutputStream("SecondaryFile\\IODemoSequence.jpg"));byte[] arr = new byte[1024];int length = 0;while((length=sis.read(arr,0,arr.length))!=-1){bos.write(arr,0,length);}}catch (Exception e){e.printStackTrace();}finally{if(sis!=null)try{sis.close();}catch (Exception e){e.printStackTrace();}if(bos!=null)try{bos.close();}catch (Exception e){e.printStackTrace();}}}public static void method_ObjectStream(){class Person implements Serializable{private String name;//如果变量被transient修饰,则不被序列化private transient int age;//给类添加序列化版本号,当该版本号固定之后,可以对类进行修改,依然可以读取原来的被序列化对象。private static final long serialVersionUID = 42L;public Person(String name,int age){this.name = name;this.age = age;}public String toString(){return name+"..."+age;}}ObjectOutputStream oos = null;try{oos = new ObjectOutputStream(new FileOutputStream("SecondaryFile\\IODemoObjectStream.obj"));oos.writeObject(new Person("张三",20));oos.writeObject(new Person("李四",21));oos.writeObject(new Person("王五",22));}catch (Exception e){e.printStackTrace();}finally{if(oos!=null)try{oos.close();}catch (Exception e){e.printStackTrace();}}//---------本方法中--以上为对象的持久化存储--以下为读取被持久化的对象-------ObjectInputStream ois = null;try{ois = new ObjectInputStream(new FileInputStream("SecondaryFile\\IODemoObjectStream.obj"));System.out.println(ois.readObject().toString());System.out.println(ois.readObject().toString());System.out.println(ois.readObject().toString());}catch (Exception e){e.printStackTrace();}finally{if(ois!=null)try{ois.close();}catch (Exception e){e.printStackTrace();}}}public static void method_RandomAccessFile(){//其内部封装了一个byte数组,既能读又能写。RandomAccessFile raf =null;try{//此流只能操作文件,并且要指定读写模式raf = new RandomAccessFile("SecondaryFile\\IODemoRandomAccess.txt","rw");raf.write("张三".getBytes());raf.writeInt(97);raf.write("李四".getBytes());raf.writeInt(98);raf.write("王五".getBytes());raf.writeInt(99);byte[] arr = new byte[4];for(int i=0;i<3;i++){//可以随机访问文件中的数据,数据必须是有规律的。raf.seek(8*i);raf.read(arr);String name = new String(arr);int age = raf.readInt();System.out.println("name = "+name+",age = "+age);}}catch (Exception e){e.printStackTrace();}finally{if(raf!=null)try{raf.close();}catch (Exception e){e.printStackTrace();}}}public static void method_Charset(){OutputStreamWriter osw = null;InputStreamReader isr = null;try{osw = new OutputStreamWriter(new FileOutputStream("SecondaryFile\\IODemoCharsetUTF-8.txt"),"utf-8");osw.write("你好");}catch (Exception e){e.printStackTrace();}finally{if(osw!=null)try{osw.close();}catch (Exception e){e.printStackTrace();}}try{osw = new OutputStreamWriter(new FileOutputStream("SecondaryFile\\IODemoCharsetGBK.txt"),"gbk");osw.write("你好");}catch (Exception e){e.printStackTrace();}finally{if(osw!=null)try{osw.close();}catch (Exception e){e.printStackTrace();}}//-------本方法中--以上为输出数据--以下为读取数据--一个文件不能够同时关联两个不同的流----------------------try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetUTF-8.txt"),"gbk");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用UTF-8编码,用GBK解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetUTF-8.txt"),"utf-8");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用UTF-8编码,用UTF-8解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetGBK.txt"),"utf-8");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用GBK编码,用UTF-8解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetGBK.txt"),"gbk");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用GBK编码,用GBK解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}//---------用ISO8859-1解码错误,可以反编译,在重新用正确的方式解码-----------try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetUTF-8.txt"),"iso8859-1");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用UTF-8编码,用ISO8859-1解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}try{isr = new InputStreamReader(new FileInputStream("SecondaryFile\\IODemoCharsetGBK.txt"),"iso8859-1");char[] arr = new char[1024];int length = isr.read(arr);System.out.println("用GBK编码,用ISO8859-1解码:"+String.valueOf(arr,0,length));}catch (Exception e){e.printStackTrace();}finally{if(isr!=null)try{isr.close();}catch (Exception e){e.printStackTrace();}}}}

0 0