//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. 三星Galaxy s4(i9505)得到完美root权限教程

    三星Galaxy s4(i9505)完美获取root权限教程 论坛上贴吧上关于三星s4 i9505 root的介绍有非常多,方法多种多样.今天小编来介绍一种使用root软件来实现三星i9505一键ro ...

  2. 网络资源(7) - JAX-WS视频

    2014_08_25 http://v.youku.com/v_show/id_XNjMzNDcyMTk2.html 基于JAX-WS编程模型的WebService 1. @WebService注释类 ...

  3. 向西项目管理工具Maven一片

    前言 相信仅仅要做过 Java 开发的童鞋们,对 Ant 想必都不陌生,我们往往使用 Ant 来构建项目,尤其是涉及到特别繁杂的工作量.一个 build.xml 可以完毕编译.測试.打包.部署等非常多 ...

  4. Unity3D的SerializeField 序列化域名

    SerializeField Inherits from Attribute Force Unity to serialize a private field. 强制Unity去序列化一个私有域. Y ...

  5. oracle_利用闪回功能恢复数据

    方便起见一般:执行如下即可不用往下看: ① 启用行移动功能 alter table tbl_a enable row movement; ② 闪回表数据到某个时间点 flashback table t ...

  6. IronPython和C#交互

    IronPython和C#交互 IronPython是一个.NET平台上的Python实现,包括了完整的编译器.执行引擎与运行时支持,能够与.NET已有的库无缝整合到一起. IronPython已经很 ...

  7. 教你发布Silverlight Bussiness Application(SQL Server 登录,局域网访问,以及使用ArcGIS Server服务需要注意的问题)

    原文:教你发布Silverlight Bussiness Application(SQL Server 登录,局域网访问,以及使用ArcGIS Server服务需要注意的问题) 之前发布过Silver ...

  8. C++中避免内存泄露常见的解决方式

    常见内存泄露及解决方式-选自ood启发录 new/delete, array new/arrray delete匹配 case 1: 在类的构造函数与析构函数中没有匹配地调用 new/delete! ...

  9. DevExpress的Web控件汉化方法

    原文:DevExpress的Web控件汉化方法 项目中用到devexpress的web控件,机器没有安装devexpress控件,直接在项目中引用的dev的dll,项目运行时发现都是英文界面,所以解决 ...

  10. Self referencing loop detected with type

    json.net namespace EFDAL{    using System;    using System.Collections.Generic;    using Newtonsoft. ...