黑马程序员_javaIO流笔记

来源:互联网 发布:大数据与信息安全报告 编辑:程序博客网 时间:2024/06/07 17:40

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

java中的io流分为字符流和字节流。字符流是操作文本数据的,字节流操作任意数据。

字符流

读Reader

以FileReader为例
1. read()方法读
public static void readFile1(){//声明FileReader变量FileReader fr = null;try{//建立FileReader对象,用于读取helloworld.txt文件内容fr = new FileReader("helloworld.txt");//单个字符循环获取数据并打印int ch;while((ch = fr.read()) != -1){System.out.print((char)ch);}}catch(IOException e){e.printStackTrace();}finally{try{//关闭流资源if(fr != null)fr.close();}catch(IOException e){e.printStackTrace();}}}

2.read(char[])方法读
public static void readFile2(){//声明FileReader变量FileReader fr = null;try{//建立FileReader对象,用于读取helloworld.txt文件内容fr = new FileReader("helloworld.txt");//用数组批量获取字符并打印char[] ch = new char[1024];int num;while((num = fr.read(ch)) != -1){System.out.print(new String(ch,0,num));}}catch(IOException e){e.printStackTrace();}finally{try{//关闭流资源if(fr != null)fr.close();}catch(IOException e){e.printStackTrace();}}}

3. 用BufferedReader装饰类来读
public static void readFile3(){//声明缓冲区流变量BufferedReader bufr = null;try{//建立缓冲区流对象,传入参数为普通流对象bufr = new BufferedReader(new FileReader("helloworld.txt"));//用readLine方式获取数据String line = null;while((line = bufr.readLine()) != null){System.out.println(line);}}catch(IOException e){e.printStackTrace();}finally{try{//关闭流资源if(bufr != null)bufr.close();}catch(IOException e){e.printStackTrace();}}}

写Writer

以FileWriter为例

1. 普通写

public static void writeFile(){//声明流变量FileWriter fw = null;try{//创建流对象,写入目的地为helloworld.txtfw = new FileWriter("helloworld.txt");//写入字符串fw.write("helloworld");}catch(IOException e){e.printStackTrace();}finally{try{//关闭流资源if(fw != null){fw.close();}}catch(IOException e){e.printStackTrace();}}}


2.续写

fw = new FileWriter("helloworld.txt",true);

3.用BufferedWriter写

public static void writeFile2(){//声明流变量BufferedWriter bufw = null;try{//创建流对象,写入目的地为helloworld.txtbufw = new BufferedWriter(new FileWriter("helloworld.txt"));//写入字符串bufw.write("helloworld");//newLine可以跨平台换行bufw.newLine();//刷新写入文件bufw.flush();bufw.write("a new line");}catch(IOException e){e.printStackTrace();}finally{try{//关闭流资源if(bufw != null){bufw.close();}}catch(IOException e){e.printStackTrace();}}}

字节流

读InputStream

与字符流的过程基本相似,只是把FileReader换成FileInputStream,BufferedReader换成BuffedInputStream,char数组换成byte数组就行了。

写OutputStream

与字符流的过程基本相似,只是把FileWriter换成FileOutputStream,BufferedWriter换成BufferedOutputStream
再用BufferedOutputStream时,不需要flush

流转换

字节流转换为字符流

InputStreamReader

我们在键盘录入时,要用System.in,这是一个字节流对象。但我们键盘输入的是文本信息,我们就需要将其转化为字符流进行读取,这样读取更方便。
下面的例子为从控制台获取键盘输入,并将其变成大写输出。
public static void changeStream(){//声明缓冲字符流变量BufferedReader bufr = null;String line = null;try{//转换流,获取键盘输入bufr = new BufferedReader(new InputStreamReader(System.in));//单行获取,如果为over结束循环while(!(line = bufr.readLine()).equals("over")){//将获取到的字符串变为大写System.out.println(line.toUpperCase());}}catch(IOException e){e.printStackTrace();}finally{try{//关闭流if(bufr != null)bufr.close();}catch(IOException e){e.printStackTrace();}}}

OutputStreamWriter

用到的类是OutputStreamWriter,用法与InputStreamReader类似。
它可以把字节流转换为字符流并查相应的码表。
------- Windows Phone 7手机开发、.Net培训、期待与您交流! -------
0 0
原创粉丝点击