黑马程序员----------IO流字节流、字符流

来源:互联网 发布:网络危机公关流程 编辑:程序博客网 时间:2024/04/27 22:51

------- android培训、java培训、期待与您交流! ----------

字节流+字符流

字节流:InputStrean   OutputStrean

将数据写入到文件中:

Filedir=new File(“tempfile”)//将文件封装成对象

if(!dir.exists()){dir.mkdir();}//判断文件是否存在,如果不存在就创建

FileOutputStramfos=new FileOutputStrean(“tempfile\\fos.txt”)//创建字节输出流对象

fos.write(“你好”,getBytes());//写入数据fos.close;//数据关流

将文件中的数据读出来

FileInputStreamfis=null;

Try{fis=newFileInputStream(“tempfile\\fos.txt”);

Byte[]buf=new byte[1024];/创建一个缓冲区int len=0;

While((len=fis.read(buf))!=-1){syso(newString(buf,0,len))}}

finally{if(fis!=null){try{fis.close;}}}

字符流:基类是Reader(输入)   Writer(输出)

字节流字符流转换桥梁

如何通过字符流读取中文数据,是将字节流转换成字符流InputStreamReader

FileInputStreamfis=new FileInputStream();

InputStreamReaderisr=new InputStreamReader(fis);

intch=isr.read();syso((char)ch);isr.close;

如何将数据写入到指定位置,是将字符流转成字节流OutputStreamWriter

FileoutputStreamfos=new FileoutputStream();

OutputStreamWriterosw=new OutputStreamWriter(fos);

Osw.write();osw.flush();//刷新osw.close();

转换流的子类:用于更方便的操作字符文件

写入:FileWriter

FileWriterfw=new FielWriter();fw.writer();fw.flush();fw.close();

读取 FielReader

FileReaderfr=new FileReader();char[] buf=new char(1024);int len=0;

While((len=fr.read())!=-1){syso(newString(buf,0,len))}

字符流缓冲区:

BufferedReader:是给字符输出流提高效率用的,那就意味着,缓冲区对象建立时,必须要先有流对象。明确要提高具体的流对象的效率。

 BufferedWriter: 是给字符输入流提高效率用的,那就意味着,缓冲区对象建立时,必须要先有流对象。明确要提高具体的流对象的效率。

缓冲区:效率高

写入:FileWriter fw=newFileWriter();BufferedWriter bufw=new BufferedWriter(fw);

bufw.writer(“abc”);bufw.readLine();//换行bufw.flush();bufw.close();

读取:FileReader fr=newFileReader();BufferedReader bufr=new BufferedReader(fr);

Stringline=null;while((line=bufr.readLine())!=-1){syso(line)bufr.close()}

装饰设计模式:

解决的问题:可以给对象提供额外的功能(职责)比继承这种方式更为灵活

装饰类与被装饰类都所属于同一个体系

同时装饰类中持有被装饰类的引用

流操作的规律:

明确源和目的:数据源需要读取用到IutputStream  Reader

目的需要写入用到Outputstream   Writer

是否是纯文本数据:如果是数据源读取用Reader,目的写入用writer  

            如果不是数据源用InputStream   目的用OutputStream

明确操作的设备:数据源对应的设备是硬盘(File)内存(数组)键盘(System.in)

数据目的对应的设备是硬盘(File)内存(数组)控制台(System.out)

需要其他额外功能吗?高效就BufferedReader    BufferedWriter,或是转换流

Properties:持久化存储,

Hashtable的子类,map集合中的方法都可以用

没有泛型,键值都是字符串,键值可以存储到集合中,也可以存储到持久化的设备上

Properties的基本读取

Propertiesprop=new Properties()

存:prop.setProperties(“zhangsan”,”22”)

取:Set<String>set=prop.StringPropertiesNames()

for(Setname:set){String value=prop.getProperties(name);syso(value+name)}

例子:将集合中的数据持久的加载到设备上

Propertiesprop=new Properties();//创建properties对象

Prop.setProperties(“zhangdan”,”22”);//添加元素

FileoutputStreamfos=new FileOutputStream(“tempfile\\info.properties”)//关联文件

Prop.stroe(fos,”zhushi”)//加载到文件中;fos.close();//数据关流

如果想修改其中的数据,先将设备上的数据读出来,修改,在加载回设备上

Propertiesprop=new Properties()

FileInputStreamfis=new FileInputStream(“tempfile\\info.properties”);

Prop.load(fis);//加载到内存

Prop.getProperties(“wangwu”);//修改数据,键值对的形式,覆盖键

FileOutputStreamfos=new FileOutputStream(“tempfile\\info.properties”)

Prop.store(fos,”zhushi”);fos.close();fis.close();


0 0
原创粉丝点击