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()

  • 相关阅读:
    HDOJ 4747 Mex
    HDU 1203 I NEED A OFFER!
    HDU 2616 Kill the monster
    HDU 3496 Watch The Movie
    Codeforces 347A A. Difference Row
    Codeforces 347B B. Fixed Points
    Codeforces 372B B. Hungry Sequence
    HDU 1476 Sudoku Killer
    HDU 1987 How many ways
    HDU 2564 词组缩写
  • 原文地址:https://www.cnblogs.com/tiantongtong/p/12996425.html
Copyright © 2011-2022 走看看