码迷,mamicode.com
首页 > 其他好文 > 详细

16 IO操作文件读写

时间:2016-09-04 19:05:39      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

IO的分类

第一种分法:

  1.输入流

  2.输出流

第二种分法:

  1.字节流

  2.字符流

第三种分法:

  1.节点流

  2.处理流

I/O当中的核心类:

  InputStream  <--------FileInputStream

  OutputStream <-------FileOutputStream

核心类的方法:

  InputStream:

    int read(byte[] b,int off,int len)

     OutputStream:

    void write(byte[] b,int off,int len)

1.I/O系统主要目标是为了对数据进行读写操作。

2.数据的流向以java程序为参照物

3.I/O流可以有三种分类方法。

4.read方法和write方法

 

class Test

{
  public static void main(String args[])

  {

    //声明输入流引用

    FileInputStream fis=null;

    //声明输出流的引用

    FileOutputStream fos=null;

    try{

      //生成代表输入流的对象

      fis=new FileInputStream("e:/src/from.txt");

      //生成代表输出流的对象

      fos=new FileOutputStream("e:/src/to.txt");

      //生成一个字节数组

      byte[] buffer=new byte[100];

      //调用输入流对象的read方法,读取数据

      int len=fis.read(buffer,0,buffer.length);

      for(int i=0;i<buffer.length;i++)

      {

        System.out.println(buffer[i]);

      }

      String s=new String(buffer);

      //调用一个String对象的trim方法,将会去除掉这个字符串中的

      //首尾空格和空字符

      s=s.trim();

      System.out.println(s);

      //将读到的数据写入to.txt文件

      fos.write(buffer,0,len);

    }

    catch(Exception e)

    {

      System.out.println(e);

    }

  }
}

 

from.txt内容

abcd

 

16 IO操作文件读写

标签:

原文地址:http://www.cnblogs.com/ansen312/p/5839966.html

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