zoukankan      html  css  js  c++  java
  • lo流

    流:是一组有序的数据序列,以先进先出方式发送信息的通道

    流读写文件操作步骤:创建File类File file=new File(String Pathname)——创建合适的流——读操作——关闭流(File指物理文件或目录)

    Pathname路径C: est.txt或C:/test.txt

    lo流

     *   按照流向分:   输入流   VS  输出流
     *   按照单位分:    字节流	VS  字符流
     *   按照角色分:   缓冲流    VS  处理流
     *   
     *   
     流的基类(抽象类)		处理流			        缓冲流
     * InputStream		   FileInputStream    BufferedInputStream	OutputStream		FileOutputStream  BufferedOutputStream	  Reader			FileReader          BufferedReader
    	Writer 			  FileWriter	     BufferedWriter  
    

      

    字节流

    主要处理:MP3, MP4 ,doc, ipg,pgn............

    一.字节输入流InputStream和FileLnputStream(站在程序角度)--读

    InputStream类常用方法(抽象类)

    int read( ) 从输入流一个字节一个字节的读,返回是该字节的整数表现形式, 如果读到了输入流末尾,返回-1.

    int read(byte[]b) 从输入流读取若干字节,把这些字节保存到数组b中。返回的是读取到的字节数,如果读到了输入流末尾,返回-1

    int read(byte[] b,int off,int len) 从输入流读取若干字节,把这些字节保存到数组b中.off指的是字节数组开始保存数据的起始下标。len指读取的字节数目返回的是实际读取到的字节数,如果读到了输入流末尾,返回-1

    void close( )

    int available()可以从输入流中读取的字节数目

    子类FileInputStream常用的构造方法

    new FileOutputStream (File file)

    new FileOutputStream(String name)

    new FileOutputStream(String name,boolean append)

    注意:

    1、前两种构造方法在向文件写数据时将覆盖文件中原有的内容

    2、创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件

    二.OutputStream字节输出流(抽象类-基类)---写

    write(int c):往输出流中写入一个个的字节

    write(byte[] buf):往输出流写入一个字节数组

    write(byte[] b,int off,int len):往输出流写入一个字节数组,off表示开始从字节数组的off位置开始往外写,len代表往外写len长度的字节

    close():关闭输出流

    flush():强制把缓冲区里的数据全部写到输出流中

    子类:FileOutputStream常用的构造方法

    new FileOutputStream(File file)

    new FileOutputStream(String name)

    new FileOutputStream(String name, boolean append) ----会在原来基础上增加

    注意:

    1前两种构造方法在向文件写数据时将覆盖文件中原有的内容

    2、创建FileOutputStream实例时,如果相应的文件并不存在,则会自动创建一个空的文件

    //常见错误:(1)少复制过来一个字节(2)多复制过来了很多空格
    		/*while(fis.read()!=-1){
    			fis.read(b);
    			fos.write(b);
    		}*/
    

      

    字符流

    主要处理:txt

    字符输入流(读)Read

    read( )(父)

    read(char[] c)

    read(char[] c,int off, int len)

    close( )

    InputerStreamReader(子)(可以指定字符码编码格式charSetName)

    new InputStreamReader(InputStream)

    new InputStreamReader(InputStream,String charSetName)

    FileReader类(子)

    new FileReader(File file)

    new FileReader(String path)

     

    子类BufferedReader常用的构造方法

    BufferedReader(Reader in)

    子类BufferedReader特有的方法

    readLine()

  • 相关阅读:
    [原创]存储过程,insert,case when then,处理性别问题 Virus
    [原创]c#,数据结构,栈 Virus
    [原创]软件自动化测试和.NET中的反射 Virus
    [原创]反射,.NET,委托 Virus
    《博客园精华集--NET3.x分册》第三轮结果
    (翻译)《Expert .NET 2.0 IL Assembler》 详要目录 更新到第8章
    MSIL翻译中的问题贴
    (翻译)《Expert .NET 2.0 IL Assembler》 第一章 简单示例 1.2 简单示例(二)
    (翻译)《Expert .NET 2.0 IL Assembler》 第一章 简单示例 1.2 简单示例(一)
    第三轮进度汇总
  • 原文地址:https://www.cnblogs.com/tiantongtong/p/12996425.html
Copyright © 2011-2022 走看看