码迷,mamicode.com
首页 > 编程语言 > 详细

java的IO总结(一)

时间:2015-01-05 23:27:58      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:

      Java的IO流是实现输入输出的基础,这里所说的流就是数据流,大部分的与流相关的类放在Java.io包下。

一,Java的流分类

     按照不同的分类方式可以把流分成不同的类型,下面我们从不同的角度对流进行分类。

     1,按照流的方向可以分为输入流与输出流

      输入流:只能从中读取数据,而不能向其写出数据

      输出流:只能向其写出数据,而不能从中读数据。     2,字节流与字符流

    字节流主要是以InputStream和OutputStream为基类,其对应子类有FileInputStreamFileOutputStream实现文件读写,BufferedInputStreamBufferedOutputStream提供缓冲区功能,操作的最小单位数据是8位的字节。字符流主要是以Reader和Writer为基类,其对应子类FileWriterFileReader可实现文件的读写操作,BufferedWriterBufferedReader能够提供缓冲区功能,用以提高效率,操作的最小的单位数据是16位的字符。

    3,节点流与处理流

    节点流:可以从/向一个特定的IO设备中读/写数据流。程序直接和数据源(物理节点)相连接。

    处理流:对节点流进行包装,他可以隐藏底层设备之间的差异,对外提供了更好的输入/输出方法。

字符流

实例1:字符流的写入

public class FileInputStreamTest {

    public static void main(String[] args)  throws IOException
    {
        FileWriter fw = null;
        try
        {
            fw = new FileWriter("D:\\javaPro\\ioJava\\xin.txt");
            fw.write("你说的话,我都不听\r\n");
            fw.write("让着故事延长\r\n");
        }  catch(IOException e)
        {
            e.printStackTrace();
        }  finally
        {
            if(fw!=null)
            {
                fw.close();
            }
        }
    }
}

实例2:字符流的读取

public class ReadStreamTest {

    public static void main(String[] args) throws  IOException
    {
        FileReader fr= null;
        try
        {
            fr = new FileReader("D:\\javaPro\\ioJava\\ha.txt");
            char[] ch = new char[1024];
            int num;
            while ((num = fr.read(ch))!= -1)
            {
                System.out.println(new String(ch,0,num));
            }
        }   catch (IOException e)
        {
              e.printStackTrace();
        }  finally {
            if(fr!=null)
            {
                fr.close();
            }
        }
    }
}

实例3:字节流的写入

public class OutPutTest {

    public static void main(String[] args) throws IOException
    {
        FileInputStream fi = null;
        FileOutputStream fo = null;
        try
        {
             fi = new FileInputStream("D:\\javaPro\\ioJava\\ha.txt");
            fo= new FileOutputStream("D:\\javaPro\\ioJava\\xin.txt");
            byte[] by = new byte[1024];
            int num;
            while((num = fi.read(by))!=-1)
            {
                  fo.write(by,0,num);
            }
        }  catch(IOException e)
        {
              e.printStackTrace();
        } finally
        {
            if(fi!=null)
            {
                fi.close();
            }
            if(fo!=null)
            {
               fo.close();
            }
        }
    }
}

实例4:字节流的读取

public class Test {
    public static void main(String[] args) throws IOException
    {
        String str = "D:\\javaPro\\ioJava\\ha.txt";
        FileInputStream f;
        f = new FileInputStream(str);
        byte[] temp1 = new byte[1024];
        int num;
        while ((num = f.read(temp1))>0)
        {
            System.out.println(num);
            System.out.println(temp1);
            System.out.println(new String(temp1,0,num));
        }
        f.close();;
    }
}

以上使用的都是节点流的例子,下面来看一个处理流的例子

实例5:处理流

public class FileInputStreamTest {

    public static void main(String[] args)  throws IOException
    {
         PrintStream ps = null;
        try
        {
            //  创建一个节点流
            FileOutputStream fs = new FileOutputStream("D:\\javaPro\\ioJava\\xin.txt");
            //   用 PrintStream来包装 FileOutputStream输出流
            ps = new PrintStream(fs);
            //  使用  PrintStream来执行输出
            ps.println("普通字符");
            //  直接使用 PrintStream输出对象
            ps.println(new FileInputStreamTest());

        }  catch(IOException e)
        {
            e.printStackTrace();
        }  finally
        {
            if(ps!=null)
            {
                ps.close();
            }
        }
    }
}

从上面可以看出来程序使用处理流非常简单,只需要在创建处理流时传入一个节点流作为构造函数的参数,这样创建的处理流就是包含了节点流的处理流。。。

 

java的IO总结(一)

标签:

原文地址:http://www.cnblogs.com/duhuo/p/4204809.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!