Java IO流

1. File 类

在 java.io包下

File类:java程序中的此类的一个对象,就对应着硬盘中的一个文件或网络中的一个资源。

File file1 = new File("d:\\io\\helloworld.txt");

File file2 = new File("d:\\io\\io1");     // 在UNIX中,此字段为‘/’,在Windows中,为‘\\

     >1.File既可以表示一个文件(.doc  .xls   .mp3  .avi   .jpg  .dat),也可以表示一个文件目录

     >2.File类的对象是与平台无关的。

     >3.File类针对于文件或文件目录,只能进行新建、删除、重命名、上层目录等等的操作。如果涉及到访问文件的内容,File是无能为力的,只能使用IO流下提供的相应的输入输出流来实现。

     >4.常把File类的对象作为形参传递给相应的输入输出流的构造器中!

File 类的一些方法有:


2. IO 流的结构


3. IO流的分类

   1) 按照流的流向的不同:输入流   输出流  (站位于程序的角度)

   2) 按照流中的数据单位的不同:字节流 (8 bit)   字符流 (16 bit)  (纯文本文件使用字符流 ,除此之外使用字节流)

   3) 按照流的角色的不同:节点流   处理流   (流直接作用于文件上是节点流(4个),除此之外都是处理流)


4. 重点的有

* 抽象基类                        节点流(文件流)                                                 缓冲流(处理流的一种,可以提升文件操作的效率)
 * InputStream                FileInputStream (int read(byte[] b))               BufferedInputStream  (int read(byte[] b))
 * OutputStream             FileOutputStream (void write(b,0,len))               BufferedOutputStream  (flush())  (void write(b,0,len))
 * Reader                         FileReader (int read(char[] c))                             BufferedReader  (readLine())  (int read(char[] c))或String readLine()
 * Writer                           FileWriter (void write(c,0,len))                               BufferedWriter  (flush()) (void write(c,0,len)或void write(String str))

注意:

       1.从硬盘中读入一个文件,要求此文件一定得存在。若不存在,报FileNotFoundException的异常

       2.从程序中输出一个文件到硬盘,此文件可以不存在。若不存在,就创建一个实现输出。若存在,则将已存在的文件覆盖

       3.多使用缓冲流来代替节点流

       4.主要最后要关闭相应的流。先关闭输出流,再关闭输入流。将此操作放入finally

 例子:复制文件

public void copyFile(String src,String dest){
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//1.提供读入、写出的文件
			File file1 = new File(src);
			File file2 = new File(dest);
			//2.想创建相应的节点流:FileInputStream、FileOutputStream
			FileInputStream fis = new FileInputStream(file1);
			FileOutputStream fos = new FileOutputStream(file2);
			//3.将创建的节点流的对象作为形参传递给缓冲流的构造器中
			bis = new BufferedInputStream(fis);
			bos = new BufferedOutputStream(fos);
			//4.具体的实现文件复制的操作
			byte[] b = new byte[1024];
			int len;
			while((len = bis.read(b)) != -1){
				bos.write(b, 0, len);
				bos.flush();
			}
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			//5.关闭相应的流
			if(bos != null){
				try {
					bos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			if(bis != null){
				try {
					bis.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
			
		}
	}

5. 其他的流

    1.转换流:实现字节流与字符流之间的转换

       InputStreamReader:输入时,实现字节流到字符流的转换,提高操作的效率(前提是,数据是文本文件)   ===>解码:字节数组--->字符串

      OutputStreamWriter:输出时,实现字符流到字节流的转换。 ===>编码:  字符串---->字节数组

    例子:

public void test1(){
        BufferedReader br = null;
        BufferedWriter bw = null;
        try {
            //解码
            File file = new File("dbcp.txt");
            FileInputStream fis = new FileInputStream(file);
            InputStreamReader isr = new InputStreamReader(fis, "GBK");
            br = new BufferedReader(isr);
            //编码
            File file1 = new File("dbcp4.txt");
            FileOutputStream fos = new FileOutputStream(file1);
            OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
            bw = new BufferedWriter(osw);
            String str;
            while((str = br.readLine()) != null){
                bw.write(str);
                bw.newLine();
                bw.flush();
            }
        }catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            if(bw != null){
                try {
                    bw.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                
            }
        }
        
    }

     常见编码表:

       ASCII:美国标准信息交换码 :用一个字节的7位可以表示。

       ISO8859-1:拉丁码表。欧洲码表: 用一个字节的8位表示。

       GB2312:中国的中文编码表。

      GBK:中国的中文编码表升级,融合了更多的中文文字符号。

      Unicode:国际标准码,融合了多种文字。所有文字都用两个字节来表示,Java语言使用的就是unicode

      UTF-8:最多用三个字节来表示一个字符。

   2.标准的输入输出流

    System.in: The "standard" input stream:从键盘输入数据 ;   类型是InputStream

    System.out:The "standard" output stream:从显示器输出数据 ;类型是PrintStream

   3.打印流 (都是输出流)  PrintStream(处理字节)  PrintWriter(处理字符)

       可以使用System.setOut(PrintStream p)重新设置一下输出的位置。 

      PrintStream p = new PrintStream(new FileOutputStream("hello.txt"),true);

  4.数据流(处理基本数据类型、String类、字节数组)

      DataInputStream  DataOutputStream

      分别“套接”在 InputStream 和 OutputStream 节点流上。

  5.对象流(用来处理对象的)

      >对象的序列化机制:允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种二进制流持久地保存在磁盘上,

        或通过网络将这种二进制流传输到另一个网络节点。当其它程序获取了这种二进制流,就可以恢复成原来的Java对象

       ObjectInputStream(Object readObject();)   ObjectOutputStream  (void writeObject(Object obj))

       如何创建流的对象:ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("person.txt")));

                                                  调用 ObjectOutputStream 对象的 writeObject(对象) 方法输出可序列化对象。注意写出一次,操作flush()

                                          ObjectInputStream ois = new ObjectInputStream(new FileInputStream(new File("person.txt")));

                                                  调用 readObject() 方法读取流中的对象
          
      实现序列化机制的对象对应的类的要求:①要求类要实现Serializable接口

                                                                              ②同样要求类的所有属性也必须实现Serializable接口

                                                                              ③ 要求给类提供一个序列版本号:private static final long serialVersionUID;

                                                                              ④属性声明为static 或transient的,不可以实现序列化

   6.随机存取文件流:RandomAccessFile

      6.1既可以充当一个输入流,又可以充当一个输出流:public RandomAccessFile(File file, String mode)

      6.2支持从文件的开头读取、写入。若输出的文件不存在,直接创建。若存在,则是对原有文件内容的覆盖。

      6.3 支持任意位置的“插入”。

    例子:在文件指定位置插入数据。(直接写会覆盖原数据)

public void test4(){
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(new File("hello1.txt"),"rw");
			
			raf.seek(4);
			byte[] b = new byte[10];
			int len;
			StringBuffer sb = new StringBuffer();
			while((len = raf.read(b)) != -1){
				sb.append(new String(b,0,len));
			}
			raf.seek(4);
			raf.write("xy".getBytes());
			raf.write(sb.toString().getBytes());
		}catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			if(raf != null){
				try {
					raf.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}
	}

 

posted @ 2016-04-19 21:11  岳灵珊  阅读(236)  评论(0编辑  收藏  举报