Java培训-IO流补充

1.RandomAccessFile:

支持文件读写。

构造器

RandomAccessFile(String file,String mode)

Mode:

r  读

rw 读写

read()….

Write()….

Seek(long pos) 跳过pos字节,pos+1开始读取或写入

skipBytes(int n)丢弃n个字节,不抛异常

public void rw(){  
        try {  
            RandomAccessFile raf=new RandomAccessFile("c:/a.txt","rw");  
            raf.writeUTF("今天是个好日子啊,人民欢欣鼓舞!");  
            //将指针跳到文件开头位置  
            raf.seek(0);  
            String s;  
            s=raf.readUTF();  
            System.out.println(s);  
//          while((s=raf.readLine())!=null){  
//              System.out.println(s);  
//          }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
          
    }

2.DataInputStream和DataOutputStream

DataInputStream能以一种与机器无关(当前操作系统等)的方式,直接从地从字节输入流读取JAVA基本类型和String类型的数据,常用于网络传输等(网络传输数据要求与平台无关)

public void dataInput(){  
        FileInputStream fis = null;  
        FileOutputStream fos = null;  
        DataInputStream dis = null;  
        DataOutputStream dos = null;  
        try {  
            fis = new FileInputStream("c:/1.txt");  
            dis=new DataInputStream(fis);  
            fos=new FileOutputStream("c:/b.txt");  
            dos=new DataOutputStream(fos);  
            byte[] buff=new byte[1024];  
            String str;  
            while((str=dis.readLine())!=null){  
                System.out.println(str);  
                  
                dos.writeChars(str);  
                dos.flush();  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            closeOutput(dos);  
            closeOutput(fos);  
            closeInput(dis);  
            closeInput(fis);  
        }  
    }

3.ObjectInputStream和ObjectOutputStream

创建学生对象:

public class Student implements Serializable{  
      
    private String Sname;  
    private String Ssex;  
    private int age;  
    public static String nativ="中国";  
    public String getSname() {  
        return Sname;  
    }  
    public void setSname(String sname) {  
        Sname = sname;  
    }  
    public String getSsex() {  
        return Ssex;  
    }  
    public void setSsex(String ssex) {  
        Ssex = ssex;  
    }  
    public int getAge() {  
        return age;  
    }  
    public void setAge(int age) {  
        this.age = age;  
    }  
  
}

用ObjectOutputStream保存输入的学生数据:

public class ObjectOutputDemo {  
      
    FileOutputStream fos=null;  
    ObjectOutputStream oos=null;  
      
    public void outputStudent(){  
        try {  
            fos=new FileOutputStream("c:/student");  
            oos=new ObjectOutputStream(fos);  
            Student student=new Student();  
            student.setSname("刘浩");  
            student.setSsex("男");  
            student.setAge(22);  
            oos.writeObject(student);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            closeOutput(oos);  
            closeOutput(fos);  
        }  
          
    }

用ObjectInputStream读取保存的学生数据:

public class ObjectInputDemo {  
      
    FileInputStream fis=null;  
    ObjectInputStream ois=null;  
      
    public void getStudent(){  
        try {  
            fis=new FileInputStream("c:/student");  
            ois=new ObjectInputStream(fis);  
            Student student=(Student) ois.readObject();  
            System.out.println(student.getSname());  
            System.out.println(student.getSsex());  
            System.out.println(student.getAge());  
            System.out.println(student.nativ);  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        }finally{  
            closeInput(ois);  
            closeInput(fis);  
        }  
    }

4.压缩流

4.1  java 压缩流

zipinputstream 输入流

zipoutputstream 输出流

压缩:

先读取被压缩的文件,在通过压缩输出流 写入到压缩包中。

首先在压缩包中,建立该文件(putNextEntry)。压缩算法不需要关心。

解压缩:

先利用zipinputstream 读取压缩包中的文件,在利用fileoutputstream 写入到磁盘

ZipFile 压缩文件类

可以获取entry,获取压缩文件name

4.2 ZipEntry的方法

构造方法:public ZipEntry(String name) 创建对象并指定要创建的ZipEntry名称

方法:public  boolean isDirectory()  判断此ZipEntry是否是目录

在压缩文件中,每一个压缩的内容都可以用一个ZipEntry 表示,所以在进行压缩之前必须通过putNextEntry 设置一个ZipEntry 。

// 获取文件输入流  
    FileInputStream fis = null;  
    // 获取文件输出流  
    FileOutputStream fos = null;  
    // 获取压缩输出流  
    ZipOutputStream zos = null;  
  
    //压缩指定文件到指定路径  
    public void zipFile() {  
        try {  
            fis = new FileInputStream("c:/1.log");  
            fos = new FileOutputStream("c:/1.zip");  
            zos = new ZipOutputStream(fos);  
  
            ZipEntry zipEntry = new ZipEntry("1.log");  
            zos.putNextEntry(zipEntry);  
            byte[] buff = new byte[1024];  
            int len;  
            while ((len = fis.read(buff)) != -1) {  
                zos.write(buff, 0, len);  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {  
            CloseIO ci = new CloseIO();  
            ci.closeOutput(zos);  
            ci.closeOutput(fos);  
            ci.closeInput(fis);  
        }  
    }

4.4 压缩文件夹

/**压缩文件夹 
     * @param srcPath 
     *            被压缩文件路径 
     * @param targetPath 
     *            压缩文件路径 
     */  
    public void zipDirectory(String srcPath,String targetPath){  
        try {  
            File srcFile=new File(srcPath);  
            fos=new FileOutputStream(targetPath);  
            zos=new ZipOutputStream(fos);  
            if(srcFile.isDirectory()){  
                File[] srcFiles=srcFile.listFiles();  
                for (int i = 0; i < srcFiles.length; i++) {  
                    if(srcFiles[i].isFile()){  
                        fis=new FileInputStream(srcFiles[i]);  
                        ZipEntry zipEntry=new ZipEntry(srcFile.getName()+  
                                File.separator+  
                                srcFiles[i].getName());  
                        zos.putNextEntry(zipEntry);  
                        byte[] buff = new byte[1024];  
                        int len;  
                        while ((len = fis.read(buff)) != -1) {  
                            zos.write(buff, 0, len);  
                            zos.flush();  
                        }  
                    }  
                }  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }finally{  
            CloseIO ci=new CloseIO();  
            ci.closeOutput(zos);  
            ci.closeInput(fis);  
            ci.closeOutput(fos);  
        }  
    }

4.5 解压缩

//解压缩只有单个文件的  
    public void unZipFile(String srcPath){  
        try {  
            File srcFile=new File(srcPath);  
            fis=new FileInputStream(srcFile);  
            zis=new ZipInputStream(fis);  
            zipFile=new ZipFile(srcFile);  
              
            zipEntry=zis.getNextEntry();  
            String name=zipEntry.getName();  
            is=zipFile.getInputStream(zipEntry);  
            //只能自动创建文件,而不能创建不存在的文件夹  
            fos=new FileOutputStream("d:/"+name);  
            byte[] buff=new byte[1024];  
            int len;  
            while((len=is.read(buff))!=-1){  
                fos.write(buff, 0, len);  
            }  
        } catch (FileNotFoundException e) {  
            e.printStackTrace();  
        } catch (ZipException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
    }
时间: 2024-08-11 07:43:05

Java培训-IO流补充的相关文章

Java培训-IO流

流的概念: 流是一组有顺序的,有起点和终点的字节集合,是对数据传输的总称或抽象.即数据在两设备间的传输称为流,流的本质是数据传输,根据数据传输特性将流抽象为各种类,方便更直观的进行数据操作. 字符流和字节流的区别: (1)读写单位不同:字节流以字节(8bit)为单位,字符流以字符为单位,根据码表映射字符,一次可能读多个字节. (2)处理对象不同:字节流能处理所有类型的数据(如图片.avi等),而字符流只能处理字符类型的数据. (3)字节流在操作的时候本身是不会用到缓冲区的,是文件本身的直接操作的

java常用IO流数据流小结

  类名 常用方法 说明 输入流 InputStream int read(); 只能读字节流,虽然返回值是int,但只有低8位起作用. DataInputStream Type readType(); 可以读二进制流,可以读byte,short,int,long,double等二进制流. BufferedReader String readLine(); 可以读文本行. 输出流 OutputStream void write(int); 只能写字节流,虽然形参是int,但只有低8为起作用. D

【Java】IO流简单分辨

转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/5827509.html Java的IO流体系十分庞大,并且体系层次稍复杂,很容易记混或记错.在此,我把平时经常用到的几个流类的简易区分以及体系层次整理出来,方便记忆与辨析.本人对IO了解尚浅,文章如有错漏,恳请前辈指正,感激不尽! 字节流体系: 基类:InputStream/outputStream(抽象类,不能new) 子类: 文件IO字节流:FileInputStream/FileoutputStream

JAVA中IO流总结

本文是在学习中的总结,欢迎转载但请注明出处:http://write.blog.csdn.net/postedit/42119261 我想你对JAVA的IO流有所了解,平时使用的也比较的多,但是对于其具体分类和继承体系可能知道的并不多,可能也很少去看相关的API文档,找出其中的关系和各自的应用情形.本文简单对常用的IO流进行分类整理,并简单举例说明其应用.希望本文对你有所帮助. (A)IO流大致分为两种: (1)字节流:对应抽象类为InputStream(输入流)和 OutputStream(输

【JAVA的 IO流之FileInputStream和FileOutputStream】

java的 IO流即输入输出流,流是一组有顺序的,有起点和终点的字节结合,是对数据传输的总称.即数据在两设备间的传输称为流,流的本质是数据传输. IO流可以分为字节流和字符流.给出相应的IO结构图: 在接下来的一段时间里,将会慢慢介绍各种流的使用,本篇博客先介绍字节流的FileOutputStream和相对应的FileInputStream. 一.FileOutputStream(文件输出流) OutputStream是一个抽象类,抽象类必须通过子类实现.现在要向文件里输出就要用FileOutp

java的IO流,字节流和字符流

java操作文件都是通过流来处理的,(其实其他很多语言也是这样) 第一:java的IO流,分为:输入流 和 输出流(这真是废话,这是从流向的角度来说的) 第二:java的所有IO流,只分为:字节流 和 字符流(其实就是传输的颗粒,传输的基本单位) 总结:凡是处理纯文本的优先考虑字符流:其他的才考虑使用字节流

Java之IO流---字节流

1.1 IO流的引入 IO流在很多语言已有体现,诸如C语言的stdio.h,C++中的iostream.Java中的IO流大抵是用于在控制台.磁盘.内存上进行数据的读写操作,完成数据的传递. 我们可以对它进行如下分类: 按处理的数据类型可分为字节流与字符流 按流的流向可分为输入流(in)与输出流(out) 按流的功能可分为节点流(Node)和过滤流(Filter) 本篇侧重于梳理字节流相关的知识,毕竟作为字符流的前辈,它还是非常重要的.下篇继续梳理字符流. 1.2 IO流的继承体系图 大概描述了

Java笔记-IO流的运用

1.InputStream和System.in(Scanner) InputStream 输出流以字节为单位来获取数据,且需要复杂的判断并创建字节数组作为缓冲 另外字节转换为字符时容易出现中文乱码的情况:Scanner Java扫描器类,可以从输入流中读取指定类型的数据或字符串. 对于字符数据的读取,应该使用Scanner扫描器进行封装,然后获取字符串类型的数据 2. out和err out和err是System类的两个static类成员变量: out:主要是输出调试信息的输出流,以黑色显示 e

java的Io流学习

Java中io流的学习(一)File:https://blog.csdn.net/qq_41061437/article/details/81672859 Java中io流的学习(二)FileInputStream和FileOutputStream:https://blog.csdn.net/qq_41061437/article/details/81742175 Java中io流的学习(三)BuffereInputStream和BuffereOutputStream:https://blog.