//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是输入到程序中)这里介绍大文件和小文件的读取方式的更多相关文章

  1. 序列化(ObjectOutputStream和ObjectInputStream)(切记:out是输出到本地中,in是输入到程序中)

    注意:序列化自定义类必须实现一个接口Serializable,在c#中序列化自定义类是使用特性也就是[Serializable] //要实现序列化的类 public class Student imp ...

  2. DataInputStream(二进制输入流)和DataOutputStream二进制输出流(注意:in是从本地文件输入到程序中,out是从程序输出到本地种)

    //切记以数据类型输出就以什么数据类型读入, //例如: dos.writeInt(100);写入,读取:dis.readUTF()有时会出现意想不到的错误,所以要时刻记得以数据类型输出就以什么数据类 ...

  3. 一切皆为字节和字节输出流_OutputStream类&FileOutputStream类介绍

    一切皆为字节 一切文件数据(文本.图片.视频等)在存储时,都是以二进制数字的形式保存,都一个一个的字节,那么传输时一样如此.所以,字节流可以传输任意文件数据.在操作流的时候,我们要时刻明确,无论使用什 ...

  4. 编码占用的字节数 1 byte 8 bit 1 sh 1 bit 中文字符编码 2. 字符与编码在程序中的实现 变长编码 Unicode UTF-8 转换 在网络上传输 保存到磁盘上 bytes

    小结: 1.UNICODE 字符集编码的标准有很多种,比如:UTF-8, UTF-7, UTF-16, UnicodeLittle, UnicodeBig 等: 2 服务器->网页 utf-8 ...

  5. Java基础 FileInputStream/ FileOutputStream / 字节输入流 字节输出流实现文件的复制

    FileInputStream/FileOutputStream的笔记: /**(FileInputStream/FileOutputStream四个步骤: ①声明②加载地址③read/write④c ...

  6. java 20 - 9 带有缓冲区的字节输出流和字节输入流

    由之前字节输入的两个方式,我们可以发现,通过定义数组读取数组的方式比一个个字节读取的方式快得多. 所以,java就专门提供了带有缓冲区的字节类: 缓冲区类(高效类) 写数据:BufferedOutpu ...

  7. 字节输出流FileOutputStream

    #字节流 字节输出流FileOutputStream 创建输出流对象 OutputStream 流对象是一个抽象类,不能实例化.所以,我们要找一个具体的子类 :FileOutputStream. 查看 ...

  8. IO初步,字节输入流和字节输出流

    字节输出流 OutputStream(基类,抽象) 特点:写任意的文件 方法:写出数据的方法:write write(int b) 写出1个字节 -128~127之间,写的是一个ASCLL码的值 wr ...

  9. JAVA笔记11__File类/File类作业/字节输出流、输入流/字符输出流、输入流/文件复制/转换流

    /** * File类:文件的创建.删除.重命名.得到路径.创建时间等,是唯一与文件本身有关的操作类 */ public class Main { public static void main(St ...

随机推荐

  1. 记2014“蓝桥杯全国软件大赛"决赛北京之行

    5月29,30日 最终到了这一天.晚上有数据结构课,10点多的火车,我们就没有去上课,下午在宿舍里收拾东西,晚上8点左右从南校出发,9点半多到达火车站和老师学长学姐们会和. 第一次去北京,第一次买的卧 ...

  2. Elasticsearch教程

    Elasticsearch教程 摘要: 参考资料Elasticsearch中文参考文档思维导图阅读全文 posted @ 2015-08-05 11:49 xingoo 阅读(18) | 评论 (0) ...

  3. 模拟linux下的ls -l命令

    #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h&g ...

  4. php实现和c#一致的DES加密解密

    原文:php实现和c#一致的DES加密解密 php实现和c#一致的DES加密解密,可以从网上搜到一大堆,但是测试后发现都没法用.以下正确代码是我经过苦苦才找到的.希望大家在系统整合时能用的上. 注意: ...

  5. DevExpress XtraReports 入门五 创建交叉表报表

    原文:DevExpress XtraReports 入门五 创建交叉表报表 本文只是为了帮助初次接触或是需要DevExpress XtraReports报表的人群使用的,为了帮助更多的人不会像我这样浪 ...

  6. 经典算法题每日演练——第十六题 Kruskal算法

    原文:经典算法题每日演练--第十六题 Kruskal算法 这篇我们看看第二种生成树的Kruskal算法,这个算法的魅力在于我们可以打一下算法和数据结构的组合拳,很有意思的. 一:思想 若存在M={0, ...

  7. JavaScript模块化编程之require.js与sea.js

    为什么要模块化:当今,网站以不再是一个简单的页面,JavaScript也不再是做一些简单的脚本验证,随着WEB.20时代到来,前端工程师面临的必将是越来越庞大的JavaScript代码,越来越复杂的内 ...

  8. mac github工具将命令当下来的代码拖入macgithub中就可以

    mac github工具将命令当下来的代码拖入macgithub中就可以,刚開始傻傻的就知道点击那个加入button,总是在当下来的文件夹下创建个文件夹.并且代码不能同步

  9. ACM经典算法之字符串处理:字符串替换

    语法:replace(char str[],char key[],char swap[]); 參数: str[]:在此源字符串进行替换操作 key[]:被替换的字符串,不能为空串 swap[]:替换的 ...

  10. Impala源代码分析---1

    2.Impala源代码分析 參考链接:http://www.sizeofvoid.net/wp-content/uploads/ImpalaIntroduction2.pdf 本章開始进入源代码分析阶 ...