IO

来源:互联网 发布:vm14安装ubuntu 教程 编辑:程序博客网 时间:2024/05/17 03:34

IO流分类:


InputStream(字节输入流),OutputStream(字节输出流)

InputStream被看成一个输入管道,OutputStream被看成一个输出管道,数据通过InputStream从源设备输入到程序,通过OutputStream从程序输出到目标设备。

InputStream的常用方法:

(1)      int read()  从输入流读取一个8位的字节,把它转换成0-255之间的整数,并返回这一整数,如果返回-1,则读到了末尾。

(2)      int read(byte[] b)  从输入流读取若干字节,把它们保存到b中,返回的整数表示读取的字节数

(3)      int read(byte[] b,int off,int len)  从输入流读取若干字节,把它们保存到b中,从下标为off的地方开始读,读取的字节数为len.

(4)      void close()   关闭流


OutputStream:

 (1)voidwrite(int b)  向输出流写入一个字节

 (2) voidwrite(byte[] b)  把b指定的字节数组的所有字节写到输出流

 (3) voidwrite(byte[] b,int off,int len)  把b指定的字节数组从偏移量off开始的len个字节写入到输出流

 (4)voidflush()  刷新此输出流并强制写出所有缓冲的输出字节

 (5)voidclose()  关闭此输出流并释放与此流相关的所有系统资源


文件读写:将文件通过输入流读到程序中去read(),再从程序中写入到文件中write(),最后要记得关流

public class IOTest {

  StringBuffer sb=newStringBuffer();

  public void readFile(String srcPath){//读文件

     try{

     File file=new File(srcPath);//构造File对象(源文件)

     //创建一个字节输入流

     FileInputStream fis=new FileInputStream(file);

     int data=fis.read();//从输入流中读取数据,每次读一个字节(效率不高)

     while(data!=-1){

      sb.append((char)data);

       data=fis.read();

       System.out.println(sb);

     }

     fis.close();//关闭输入流

     }catch(Exceptione){

     e.printStackTrace();

     }

  }

  public void writeFile(String destPath){//写文件

     try{

       //构造文件输出流对象

       FileOutputStream fos = new FileOutputStream(destPath);

       //将字节数据写入到目标文件中

       String str = "abcdefg";

       byte[]buf =str.getBytes();

       fos.write(buf);//写入一个字节数组的数据

       fos.close();//关闭输出流

     } catch (Exceptione) {

       e.printStackTrace();

     }

  }

 //文件的复制粘贴(从源文件读取数据到程序中,再从程序写入到目标文件)

public void copyPaste(String srcPath,StringdestPath){

//在外面先声明流对象,便于关闭流

     FileInputStream fis=null;

     FileOutputStream fos=null;

     BufferedInputStream bis=null;

     BufferedOutputStream bos=null;

     try{

     fis=new FileInputStream(srcPath);

     fos=new FileOutputStream(destPath);

     int data=fis.read();

     while(data!=-1){

       fos.write(data);

       data=fis.read();

     }

     bis=new BufferedInputStream(fis);

     bos=new BufferedOutputStream(fos);

     int total;

     while((total=bis.read())!=-1){

       bos.write(total);

     }

     bis.close();

     bos.close();

     }catch(Exceptione){

       e.printStackTrace();

     }

     finally{//在最后关闭流,不管有没有异常,都会执行

       try {

         bis.close();

         bos.close();

         fis.close();

         fos.close();

       } catch (IOExceptione) {

         e.printStackTrace();

       }

     }

  }

为了提高读写效率,我们可以一次读若干个字节,一般为1024,这样会造成文件变大,因为最后不足1024个字节时,而每次都写入1024个字节会把前一次的读入。

还可以用缓冲流来提高读写效率

public void copyPaste(String srcPath,String destPath){
try{
File file = new File(srcPath);//根据源路径创建文件

//构建文件输入输出流对像

FileOutputStream fos = new FileOutputStream(destPath);

FileInputStream fis = new FileInputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(fos);//缓冲流

BufferedInputStream bis = new BufferedInputStream(fis);

int data = bis.read();

while(data != -1){

bos.write(data);

data = bis.read();
}

byte[] datas = new byte[1024];//定义一个长度为1024的字节数组

fis.read(datas);

fos.write(datas);

int total = fis.read(datas);

while(total != -1){

fos.write(datas,0,total);

total = fis.read(datas);
}

//关闭所有的流

bos.close();

bis.close();

fos.close();

fis.close();

}catch(Exception e){

e.printStackTrace();

}

}

0 0
原创粉丝点击