论坛首页 入门技术论坛

corejava辅导(12--3)

浏览 952 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-03  

 

基本输入输出所使用的类的介绍

 

FileInputStreamFileOutputStream (文件输入输出流)

 

以上两个是字节流

1) 结点流,可以对磁盘文件进行操作。

2) 要构造一个FileInputStream, 所关联的文件必须存在而且是可读的。

3) 要构造一个FileOutputStream而输出文件已经存在,则它将被覆盖。

     

       FileInputStream infile = new FileInputStream("myfile.dat");

   FIleOutputStream outfile = new FileOutputStream("results.dat");

     FileOutputStream outfile = new FileOutputStream(“results.dat”,true);

       参数为true时输出为添加,为false时为覆盖。

 

import java.io.*;

public class FileCopy {

     public static void main(String[] args) {

         FileInputStream fi = null;

         FileOutputStream fo = null;

         try {

              fi = new FileInputStream(args[0]);

              fo = new FileOutputStream("copy_"+args[0]);

              byte[] bs=new byte[1024];

              int i;

              while((i=fi.read(bs))!=-1){

                   fo.write(bs,0,i);

              }

         } catch (FileNotFoundException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

         } catch (IOException e) {

              // TODO Auto-generated catch block

              e.printStackTrace();

         }

    

         finally{

              if(fi!=null)

                   try {

                       fi.close();

                   } catch (IOException e) {

                       // TODO Auto-generated catch block

                       e.printStackTrace();

                   }

              if(fo!=null)

                   try {

                       fo.close();

                   } catch (IOException e) {

                       // TODO Auto-generated catch block

                       e.printStackTrace();

                   }

              }

        

     }

}

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics