/** * 文件压缩 ZipOutputStream类 * @throws IOException */ public void ZipOutputStreamFile() throws IOException { File file = new File("d:" + File.separator + "hello.txt"); File zipFile = new File("d:" + File.separator + "hello.zip"); InputStream input = new FileInputStream(file); ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream( zipFile)); zipOut.putNextEntry(new ZipEntry(file.getName())); // 设置注释 zipOut.setComment("hello"); int temp = 0; while ((temp = input.read()) != -1) { zipOut.write(temp); } input.close(); zipOut.close(); } /** * 多个文件的压缩 * @throws IOException */ public void ZipOutputStreamFiles() throws IOException { // 要被压缩的文件夹 File file = new File("d:" + File.separator + "temp"); //temp文件夹下面的所有文件 File zipFile = new File("d:" + File.separator + "zipFile.zip"); InputStream input = null; ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFile)); zipOut.setComment("hello"); if (file.isDirectory()) { File[] files = file.listFiles(); for (int i = 0; i < files.length; ++i) { input = new FileInputStream(files[i]); zipOut.putNextEntry(new ZipEntry(file.getName() + File.separator + files[i].getName())); int temp = 0; while ((temp = input.read()) != -1) { zipOut.write(temp); } input.close(); } } zipOut.close(); } /** * ZipFile演示 * @throws IOException * @throws ZipException */ public void ZipFileDemo() throws ZipException, IOException{ File file = new File("d:" + File.separator + "hello.zip"); ZipFile zipFile = new ZipFile(file); System.out.println("压缩文件的名称为:" + zipFile.getName()); } /** * 解压缩文件(压缩文件中只有一个文件的情况) * @throws IOException * @throws ZipException */ public void ZipFileOutOne() throws ZipException, IOException{ File file = new File("d:" + File.separator + "hello.zip"); File outFile = new File("d:" + File.separator + "unZipFile.txt"); ZipFile zipFile = new ZipFile(file); ZipEntry entry = zipFile.getEntry("hello.txt"); InputStream input = zipFile.getInputStream(entry); OutputStream output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } /** * ZipInputStream解压多个文件 * @throws IOException * @throws ZipException */ public void ZipInputStreams() throws ZipException, IOException{ File file = new File("d:" + File.separator + "zipFile.zip"); File outFile = null; ZipFile zipFile = new ZipFile(file); ZipInputStream zipInput = new ZipInputStream(new FileInputStream(file)); ZipEntry entry = null; InputStream input = null; OutputStream output = null; while((entry = zipInput.getNextEntry()) != null){ System.out.println("解压缩" + entry.getName() + "文件"); outFile = new File("d:" + File.separator + entry.getName()); if(!outFile.getParentFile().exists()){ outFile.getParentFile().mkdir(); } if(!outFile.exists()){ outFile.createNewFile(); } input = zipFile.getInputStream(entry); output = new FileOutputStream(outFile); int temp = 0; while((temp = input.read()) != -1){ output.write(temp); } input.close(); output.close(); } } /** * PushBackInputStream退回流 * @throws IOException */ public void PushBackInputStreamFile() throws IOException{ String str = "hello,rollenholt"; PushbackInputStream push = null; ByteArrayInputStream bat = null; bat = new ByteArrayInputStream(str.getBytes()); push = new PushbackInputStream(bat); int temp = 0; while((temp = push.read()) != -1){ if(temp == ','){ push.unread(temp); temp = push.read(); System.out.print("(回退" + (char) temp + ") "); }else{ System.out.print((char) temp); } } /** * 取得本地的默认编码 * */ System.out.println("系统默认编码为:" + System.getProperty("file.encoding")); } } /** * 文件序列化 */ public void SserializeFile() throws FileNotFoundException, IOException, ClassNotFoundException { // 打开是乱码必须使用ObjectInputStream查看 File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream( file)); oos.writeObject(new SerializableDemo("rollen", 20)); oos.close(); /** * ObjectInputStream示范 * */ ObjectInputStream input = new ObjectInputStream(new FileInputStream( file)); Object obj = input.readObject(); input.close(); System.out.println(obj); } /** *****序列化与反序列化*********** */ // 序列化 public void ser() throws Exception { File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(new Person("rollen", 20)); out.close(); } // 反序列化 public void dser() throws Exception { File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream(file)); Object obj = input.readObject(); input.close(); System.out.println(obj); } /** * 实现具有序列化能力的类 * */ class SerializableDemo implements Serializable { public SerializableDemo() { } public SerializableDemo(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "姓名:" + name + " 年龄:" + age; } private String name; private int age; } class Person implements Externalizable { public Person() { } public Person(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "姓名:" + name + " 年龄:" + age; } // 复写这个方法,根据需要可以保存的属性或者具体内容,在序列化的时候使用 public void writeExternal(ObjectOutput out) throws IOException { out.writeObject(this.name); out.writeInt(age); } // 复写这个方法,根据需要读取内容 反序列话的时候需要 public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { this.name = (String) in.readObject(); this.age = in.readInt(); } private String name; // 加上transient关键字可以不序列化 // private transient String name; private int age; /** * 序列化一组对象 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { Student[] stu = { new Student("hello", 20), new Student("world", 30), new Student("rollen", 40) }; ser(stu); Object[] obj = dser(); for (int i = 0; i < obj.length; ++i) { Student s = (Student) obj[i]; System.out.println(s); } } // 序列化 public static void ser(Object[] obj) throws Exception { File file = new File("d:" + File.separator + "hello.txt"); ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream( file)); out.writeObject(obj); out.close(); } // 反序列化 public static Object[] dser() throws Exception { File file = new File("d:" + File.separator + "hello.txt"); ObjectInputStream input = new ObjectInputStream(new FileInputStream(file)); Object[] obj = (Object[]) input.readObject(); input.close(); return obj; } } class Student implements Serializable { public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } @Override public String toString() { return "姓名: " + name + " 年龄:" + age; } private String name; private int age; }

转载于:https://www.cnblogs.com/JPAORM/archive/2012/04/29/2509853.html

Java IO实战操作(四)相关推荐

  1. Java IO实战操作(三)

    /** * IO管道处理集合 */ public void OutFile() throws FileNotFoundException { /** * 使用PrintStream进行输出 */ Pr ...

  2. Java IO实战操作(一)

    /** * 创建一个新文件 */ public void NewFiles() { File file = new File("D:\\IO.txt"); try { file.c ...

  3. Java IO实战操作(二)

    /** * 向文件中追加新内容 * @throws IOException */ public void NewInserNum() throws IOException { String fileN ...

  4. Java IO流学习总结四:缓冲流-BufferedReader、BufferedWriter

    Java IO流学习总结四:缓冲流-BufferedReader.BufferedWriter 转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/det ...

  5. java io基础知识(四)

    一.File文件类 1.1.常用方法 1.路径问题 绝对路径:该文件在硬盘上的完整路径.绝对路径一般都是以盘符开头的. 相对路径:相对路径就是资源文件相对于当前程序所在的路径. ".&quo ...

  6. 【十四】Java IO框架思维导图

    知识章节参考:[十四]Java IO框架

  7. Java IO流中 File文件对象与Properties类(四)

    File类 用来将文件或目录封装成对象 方便对文件或目录信息进行处理 File对象可以作为参数传递给流进行操作 File类常用方法 创建 boolean createNewFile():创建新文件,如 ...

  8. Java IO学习笔记(四)打印流

    1.只有输出流才有打印流:PrintWriter和PrintStream分别针对字符和字节,提供了重载的print,Println方法用于多种数据类型的输出.PrintWriter和PrintStre ...

  9. Java io字符流读入英文_Java IO 系列教程(四)-字符输入流(2)

    本文介绍字符输入流 在前面一节中,我们向一个文件中写入了一些字符,通过图片可以看出总共是6个中文字符和一个换行,总共是20个字节,可以推算出字符编码是utf-8,每个汉子占3三个字节.本文就用字符输入 ...

最新文章

  1. 【SpringCloud】Eureka-实例
  2. 007 Android之Broadcast Receiver
  3. c语言地图导航代码大全,C语言实训—电子地图导航系统源代码.docx
  4. CF767C Garland
  5. jMeter的Cookie Manager用法
  6. 【MySQL】4、Select查询语句
  7. kep server 6.4 激活_轻松一点,一触屏蔽!5G和未来显示的福音——低压激活,敏感元器件专用保护方案...
  8. 关于Apt注解实践与总结【包含20篇博客】
  9. [渝粤教育] 西安工业大学 汉语文字学 参考 资料
  10. VSCode 修改界面字体 代码字体 终端字体
  11. 基于生长的棋盘格角点检测方法 代码介绍
  12. 数据、程序、文件区别
  13. The server time zone value ‘锟叫癸拷锟斤拷\u05FC时锟斤拷‘ is unrecognized or represents more than one time zone
  14. 世界的下一个主宰——人工智能
  15. 如何靠代码发家致富?——10种可以赚钱的途径
  16. Dolphin Scheduler 2.x版本部署篇
  17. 《学生问题行为矫正实践研究》立项申请
  18. Viewpager+Fragment+webview中的输入框不弹出软键盘的坑
  19. 助力泵嗡嗡响解决方法_怎么解决转向助力泵嗡嗡响
  20. 【Python】difflib 文本比较,差异对比库

热门文章

  1. 极客先锋 如何生成git的公钥和私钥
  2. C#中使用正则表达式验证电话号码、手机号、身份证号、数字和邮编
  3. 30个Oracle语句优化规则详解(1)
  4. windowoPhone7.1 Socket编程-实现手机与电脑通信
  5. [转]浅析GPU计算——cuda编程
  6. 配置csrf_django 入门第一课 配置文件
  7. 低代码工具让人人都是程序员?别再吐槽了,它能做这些事
  8. IDEA导入Eclipse项目的方法步骤(图文教程)
  9. api postmain 鉴权_API鉴权
  10. 100以内的偶数的个数_10以内数字的奇偶性认识