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

lo流

时间:2020-05-31 11:09:09      阅读:55      评论:0      收藏:0      [点我收藏+]

标签:class   doc   ade   red   dash   inpu   关闭   常用方法   byte   

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

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

Pathname路径C:\\test.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()

lo流

标签:class   doc   ade   red   dash   inpu   关闭   常用方法   byte   

原文地址:https://www.cnblogs.com/tiantongtong/p/12996425.html

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