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

FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式

时间:2014-07-06 16:50:09      阅读:143      评论:0      收藏:0      [点我收藏+]

标签:文件   数据   os   io   new   file   

//FileOutputStream

public class FileOutputStreamDemo {
 /**字节流:适用于任何文件,以字节为单位,进行读写操作
  *字节流操作步骤:
  *1、创建文件对象
  *2、创建字节流
  *3、读写操作
  *4、关闭流
  */
 //字节流(写操作)
 public static void main(String[] args) {

String messageString = "hello world";
  byte[] bytes = messageString.getBytes();
  //1、创建文件对象
  File file = new File("abc.txt");  
  FileOutputStream fos = null;
  try {
   //2、创建字节流
   fos = new FileOutputStream(file,true);//参数一:file对象(也可以是一个文件路径) 参数二:true表示追加模式 false表示覆盖模式
   //3、写操作
   fos.write(bytes);
   System.out.println("写入成功!");
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    //4、关闭流
    if(fos!=null){
     fos.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

}

}

 

//FileInputStream

public class FileInputStreamDemo {
 //字节流(读小文件操作)
// public static void main(String[] args) {

//  //1、创建文件对象
//  File file = new File("abc.txt");
//  FileInputStream fis = null;
//  try {
//   //2、创建字节流
//   fis = new FileInputStream(file);
//   byte[] bytes = new byte[(int)file.length()];//创建存储从文件读取到的字节数据
//   //3、读操作
//   fis.read(bytes);
//   String str = new String(bytes);//将字节数组转换为字符串
//   System.out.println(str);
//  } catch (FileNotFoundException e) {
//   e.printStackTrace();
//  } catch (IOException e) {
//   e.printStackTrace();
//  }finally{
//   try {
//    //4、关闭流
//    if(fis!=null){
//     fis.close();
//    }
//   } catch (IOException e) {
//    e.printStackTrace();
//   }
//  }
// }

 

//字节流(读大文件操作)
 public static void main(String[] args) {

File file = new File("abc.txt");
  FileInputStream fis = null;

  //一般byte数组的大小为512,,1024,2048
  byte[] bytes = new byte[1024];//存储每次读取的字节数据
  int len = 0;//存储每次读取的字节数
  try {
   fis = new FileInputStream(file);
   while((len=fis.read(bytes))!=-1){
    //参数一:字节数组   参数二:从数组的哪个下标开始转换为字符串   参数三:要转换的字节数
    String str = new String(bytes,0,len);
    System.out.println(str);
   }
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   try {
    if(fis!=null){
     fis.close();
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

}

}

FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式,布布扣,bubuko.com

FileOutputStream字节输出流和FileInputStream输入流(切记:out是输出到本地中,in是输入到程序中)这里介绍大文件和小文件的读取方式

标签:文件   数据   os   io   new   file   

原文地址:http://www.cnblogs.com/danmao/p/3825239.html

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