黑马程序员--------------JAVA基础-------------IO流二

来源:互联网 发布:三菱fx2nplc仿真软件 编辑:程序博客网 时间:2024/05/16 06:54

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

字节流

字符流在操作文本文件时比较方便,但是涉及到图片或者其他媒体文件就不适用了,这时需要用到字节流。

|--InputStream 字节输入流

int available()   返回此输入流下一个方法调用可以不受阻塞地从此输入流读取(或跳过)的估计字节数。

abstract int read() 从输入流中读取数据的下一个字节。

int read(byte[] b) 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b 中。

int read(byte[] b, int off, int len)   将输入流中最多 len 个数据字节读入 byte 数组。

|--OutputStream 字节输出流

void write(byte[] b) 将 b.length 个字节从指定的 byte 数组写入此输出流。

void write(byte[] b, int off, int len) 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流。

abstract void write(int b) 将指定的字节写入此输出流。

import java.io.*;class  FileStream{public static void main(String[] args) throws IOException{readFile_1;readFile_2();readFile_3();}public static void readFile_3()throws IOException{FileInputStream fis = new FileInputStream("fos.txt");//int num = fis.available();byte[] buf = new byte[fis.available()];//定义一个刚刚好的缓冲区。不用在循环了。如果文件过大就会出现内存溢出,慎用!fis.read(buf);System.out.println(new String(buf));fis.close();}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();}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();}public static void writeFile()throws IOException{FileOutputStream fos = new FileOutputStream("fos.txt");fos.write("abcde".getBytes());  //writer方法写入的是字节数组,所以要把字符串转换为字节数组。fos.close();}}
如果需要复制一个图片这时就需要使用字节流:

<strong>/</strong>*思路:1,用字节读取流对象和图片关联。2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。3,通过循环读写,完成数据的存储。4,关闭资源。*/import java.io.*;class  CopyPic{public static void main(String[] args) {FileOutputStream fos = null;FileInputStream fis = null;try{fos = new FileOutputStream("c:\\2.bmp");  //目的fis = new FileInputStream("c:\\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{try{if(fis!=null)fis.close();}catch (IOException e){throw new RuntimeException("读取关闭失败");}try{if(fos!=null)fos.close();}catch (IOException e){throw new RuntimeException("写入关闭失败");}}}}
但是如果每次复制,每次读写都需要自己定义缓冲数组就会显得麻烦,java的装饰设计模式,可以把原有的功能增强。

字符流

Reader------->BufferedReader   构造器 BufferedReader(Reader in) 创建一个使用默认大小输入缓冲区的缓冲字符输入流。

Writer--------->BufferedWriter  构造器  BufferedWriter(Writer out) 创建一个使用默认大小输出缓冲区的缓冲字符输出流。

字节流

InputStream-----> BufferedInputStream  构造器 BufferedInputStream(InputStream in) 创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。

OutputStream----->BufferedOutputStream 构造器 BufferedOutputStream(OutputStream out)创建一个新的缓冲输出流,以将数据写入指定的底层输出流。


/*字符读取流缓冲区:该缓冲区提供了一个一次读一行的方法 readLine,方便于对文本数据的获取。当返回null时,表示读到文件末尾。readLine方法返回的时候只返回回车符之前的数据内容。并不返回回车符。*/import java.io.*;class  BufferedReaderDemo{public static void main(String[] args) throws IOException{//创建一个读取流对象和文件相关联。FileReader fr = new FileReader("buf.txt");//为了提高效率。加入缓冲技术。将字符读取流对象作为参数传递给缓冲对象的构造函数。BufferedReader bufr = new BufferedReader(fr);String line = null;while((line=bufr.readLine())!=null){System.out.print(line);}bufr.close();}}
/*缓冲区的出现是为了提高流的操作效率而出现的。所以在创建缓冲区之前,必须要先有流对象。该缓冲区中提供了一个跨平台的换行符。newLine();*/import java.io.*;class  BufferedWriterDemo{public static void main(String[] args) throws IOException{//创建一个字符写入流对象。FileWriter fw = new FileWriter("buf.txt");//为了提高字符写入流效率。加入了缓冲技术。//只要将需要被提高效率的流对象作为参数传递给缓冲区的构造函数即可。BufferedWriter bufw = new BufferedWriter(fw);for(int x=1; x<5; x++){bufw.write("abcd"+x);bufw.newLine();bufw.flush();}//记住,只要用到缓冲区,就要记得刷新。//bufw.flush();//其实关闭缓冲区,就是在关闭缓冲区中的流对象。bufw.close();}}

/*通过缓冲区复制一个.java文件。*/import java.io.*;class  CopyTextByBuf{public static void main(String[] args) {BufferedReader bufr = null;BufferedWriter bufw = null;try{bufr = new BufferedReader(new FileReader("BufferedWriterDemo.java"));bufw = new BufferedWriter(new FileWriter("bufWriter_Copy.txt"));String line = null;while((line=bufr.readLine())!=null){bufw.write(line);bufw.newLine();bufw.flush();}}catch (IOException e){throw new RuntimeException("读写失败");}finally{try{if(bufr!=null)bufr.close();}catch (IOException e){throw new RuntimeException("读取关闭失败");}try{if(bufw!=null)bufw.close();}catch (IOException e){throw new RuntimeException("写入关闭失败");}}}}

标准输入:System.in( InputStream流)

标准输出: System.out (PrintStream流)

如何读取键盘录入呢?

/*读取键盘录入。System.out:对应的是标准输出设备,控制台。System.in:对应的标准输入设备:键盘。需求:通过键盘录入数据。当录入一行数据后,就将该行数据进行打印。如果录入的数据是over,那么停止录入。*/import java.io.*;class  ReadIn{public static void main(String[] args) 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.toUpperCase());sb.delete(0,sb.length());}elsesb.append((char)ch);}}}
/*通过键盘录入一行数据并打印其大写,发现其实就是读一行数据的原理。也就是readLine方法。能不能直接使用readLine方法来完成键盘录入的一行数据的读取呢?readLine方法是字符流BufferedReader类中的方法。而键盘录入的read方法是字节流InputStream的方法。那么能不能将字节流转成字符流在使用字符流缓冲去的readLine方法呢?*/import java.io.*;class  TransStreamDemo{public static void main(String[] args) throws IOException{//获取键盘录入对象。//InputStream in = System.in;//将字节流对象转成字符流对象,使用转换流。InputStreamReader//InputStreamReader isr = new InputStreamReader(in);//为了提高效率,将字符串进行缓冲区技术高效操作。使用BufferedReader//BufferedReader bufr = new BufferedReader(isr);//键盘的最常见写法。BufferedReader bufr = new BufferedReader(new InputStreamReader(System.in));//OutputStream out = System.out;//OutputStreamWriter osw = new OutputStreamWriter(out);//BufferedWriter bufw = new BufferedWriter(osw);BufferedWriter bufw = new BufferedWriter(new OutputStreamWriter(System.out));String line = null;while((line=bufr.readLine())!=null){if("over".equals(line))break;bufw.write(line.toUpperCase());bufw.newLine();bufw.flush();}bufr.close();}}

0 0
原创粉丝点击