黑马程序员 java学习笔记——IO流2

来源:互联网 发布:丰田车导航软件 编辑:程序博客网 时间:2024/05/22 14:46

---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ----------------------

字节流

字节流既可以操作文本文件,又可以操作其他格式的文件。

FileInputStream操作文件的三种方式:

第一种方式:一个个字节读入
import java.io.*;public class Demo{public static void main(String[] args) throws IOException{readFile_1();}public static void readFile_1() throws IOException{FileInputStream fis=new FileInputStream("fos.txt");int ch=0;while((ch=fis.read())!=-1){System.out.println((char)ch);}fis.close();}}

第二种方式,读入一个数组。
import java.io.*;public class Demo{public static void main(String[] args) throws IOException{readFile_2();}public static void readFile_2() throws IOException{FileInputStream fis=new FileInputStream("fos.txt");byte[] buf=new byte[1024];int len=0;while((len=fis.read(buf))!=-1){System.out.println(new String(buf,0,len));}fis.close();}}

第三种方式:读入一个大小刚刚好的数组
import java.io.*;public class Demo{public static void main(String[] args) throws IOException{readFile_3();}public static void readFile_3() throws IOException{FileInputStream fis=new FileInputStream("fos.txt");//定义一个大小刚刚好的缓冲区。byte[] buf=new byte[fis.available()];fis.read(buf);System.out.println(new String(buf));fis.close();}}

拷贝图片

思路:
1、用字节读取流对象和图片关联
2、用字节写入流对象创建一个图片文件。用于存储获取到的图片数据
3、通过循环读写,完成数据的存储
4、关闭资源
示例代码如下:
import java.io.*;public class Demo{public static void main(String[] args) throws IOException{FileOutputStream fos=null;FileInputStream fis=null;try{fos=new FileOutputStream("2.bmp");fis=new FileInputStream("1.bmp");byte[] buf=new byte[1024];int len=0;while((len=fis.read(buf))!=-1){fos.write(buf,0,len);}}catch(IOException e){throw new RuntimeException("复制文件失败");}finally{if(fis!=null){try{fis.close();}catch(IOException e){throw new RuntimeException("读取关闭失败");}}if(fos!=null){try{fos.close();}catch(IOException e){throw new RuntimeException("读取关闭失败");}}}}}

自定义字节流缓冲区

示例代码如下:
import java.io.*;class MyBufferedInputStream{private InputStream in;private byte[] buf=new byte[1024*4];private int pos=0,count=0;MyBufferedInputStream(InputStream in){this.in=in;}//一次读一个字节,从缓冲区获取。public int myread() throws IOException{//通过in对象读取硬盘上数据,并存储到buf中。if(count==){count=in.read(buf);if(count<0)return-1;pos=0;byte b=buf(pos);count--;pos++;return b&255;}else if(count>0){byte b=buf(pos);count--;pos++;return b&0xff;}return -1;}public void myClose() throws IOException{in.close();}}

键盘录入

System.out:对应的是标准输出设备:控制台
System.in:对应的是标准输入设备 :键盘

示例代码如下:

import java.io.*;public class Demo{public static void main(String...agrs)throws IOException{InputStream in =System.in;StringBuilder sb = new StringBuilder();while(true){int ch =in.read();if(ch=='\r')continue;if(ch=='\n'){String s=sb.toString();if("over".equals(s))break;System.out.println(s);}else{sb.append((char)ch);}}}}


转换流

读取转换流InputStreamReader

示例代码如下:
import java.io.*;public class Demo{public static void main(String [] args)throws IOException{   InputStream in = System.in;  InputStreamReader isr =  new InputStreamReader(in);   BufferedReader bufr = new BufferedReader(isr);String line = null;while ((line=bufr.readLine())!=null){if("over".equals(line))break;System.out.println(line.toUpperCase());}bufr.close();}}

写入转换流OutputStreamWriter

示例代码如下:
import java.io.*;public class Demo{public static void main(String[] args)throws IOException{BufferedReader bufr =new BufferedReader(new InputStreamReader(System.in));BufferedWriter bufw =new BufferedWriter(new OutputStreamWriter(System.out));for(String line = null;(line=bufr.readLine())!=null;){if("over".equals(line))break;bufw.write(line);bufw.newLine();bufw.flush();}bufr.close();bufw.close();}}

流操作规律

流对象有很多,通过三个明确来确定。

(1).明确源和目的

源:输入流 InputStream Reader
目的:输出流 OutputStream Writer

(2)操作的数据是否是纯文本

是:字符流
不是:字节流

(3)当体系明确后,在明确要使用哪个具体的对象

通过设备来进行区分
源设备:内存,硬盘,键盘
目的设备:内存,硬盘,控制台

小示例分析:
(1) 需求:将键盘录入的数据显示在控制台上。
源: 键盘录入
目的:控制台
(2) 需求:想把键盘录入的数据存储到一个文件中
源 :键盘录入
目的 :文件
(3) 需求:想要将一个文件的数据打印在控制台上
源:文件
目的: 控制台

---------------------- ASP.Net+Android+IO开发S.Net培训、期待与您交流! ----------------------
0 0
原创粉丝点击