济南做网站比较好的公司,网站设计说明,广州百度网站推广,网站域名费IO与网络编程 3 输入输出流IO3.1 基础定义3.2 IO框架3.3 读取字节输入流-InputStream3.3.1 InputStream.read3.3.2 FileInputStream类说明 3.4读取字符输入流Reader3.4.1 Reader.read3.4.2 FileReader类说明 3.5 字节输出流OutputStream3.5.1 OutputStream.write3.5.2 FileOutp… IO与网络编程 3 输入输出流IO3.1 基础定义3.2 IO框架3.3 读取字节输入流-InputStream3.3.1 InputStream.read3.3.2 FileInputStream类说明 3.4读取字符输入流Reader3.4.1 Reader.read3.4.2 FileReader类说明 3.5 字节输出流OutputStream3.5.1 OutputStream.write3.5.2 FileOutputStream类说明 3.6字符输出流Writer3.6.1 Writer.write3.6.2 FileWriter类说明 3.7 BufferedInputStream类说明 3 输入输出流IO
3.1 基础定义
java的输入输出流是比较难懂的地方,什么是java.io I/O 是指Input/Output即输入和输出。 Input指从外部读入数据到内存例如把文件从磁盘读取到内存从网络读取数据到内存等等。 Output指把数据从内存输出到外部例如把数据从内存写入到文件把数据从内存输出到网络等等。 Java程序在执行的时候是在内存进行的外部的数据需要读写到内存才能处理而在内存中的数据是随着程序结束就消失的有时候我们也需要把数据输出到外部文件。Java中是通过流 处理IO的这种处理模式称为 IO流IO流是一种顺序读写数据的模式。 你可以想象它是一根水管数据就像水一样 起点—终点 可互相流动。 源头 从文件、网络、管道我们称之为目标源 输入/输出 从目标源读取到内容我们叫输入用InputStream或者Reader相关子类来处理。 从内存输出到目标源我们称之为输出用OutputStream或者Writer相关子类来处理 传递介质 我们想要把目标源转化成Byte或者Char才能传输Byte用InputStream/OutputStream来操作Char用Reader/Writer来操作。 一般情况视频、音频、图片等用byte来传递文字类的用Char来传递方便一些
3.2 IO框架 1.4大框架类 类型字节流字符流输入流InputStreamReader输出流OutputStreamWriter 3.3 读取字节输入流-InputStream
InputStream 这个抽象类是表示输入字节流的所有类的超类父类。
3.3.1 InputStream.read
InputStream 中的三个基本的读方法
int read() 读取一个字节数据并返回读到的数据如果返回 -1表示读到了输入流的末尾。int read(byte[] b) 将数据读入一个字节数组同时返回实际读取的字节数。如果返回-1表示读到了输入流的末尾。int read(byte[] b, int off, int len) 将数据读入一个字节数组同时返回实际读取的字节数。如果返回 -1表示读到了输入流的末尾。off 指定在数组 b 中存放数据的起始偏移位置len 指定读取的最大字节数。
InputStream子类 ByteArrayInputStream FileInputStream FilterInputStream PushbackInputStream DataInputStream BufferedInputStream LineNumberInputStream ObjectInputStream PipedInputStream SequenceInputStream StringBufferInputStream 其中最重要的是FileInputStream、BufferedInputStream
3.3.2 FileInputStream类说明
FileInputStream是文件字节输入流就是对文件数据以字节的方式来处理如音乐、视频、图片等。 //1.一个一个和字节读入Testpublic void t2() throws IOException {FileInputStream fisnew FileInputStream(d:\\x.txt);int dataByte0;//1.一个一个和字节读入如果返回-1表示读取到末尾while((dataBytefis.read())!-1){System.out.print((char)dataByte);}fis.close();}//以字节数组读入Testpublic void t3() throws IOException {File filenew File(d:\\x.txt);InputStream fisnew FileInputStream(file);int byte_len0;byte[]bytesnew byte[8];//一个字节数组的读出数据,放置到bytes数组李高效while((byte_lenfis.read(bytes))!-1){System.out.print(new String(bytes));
// for (int i 0; i byte_len; i) {
// System.out.print((char) bytes[i]);
// }}}//一次性读入Testpublic void t4() throws IOException {File filenew File(d:\\a.txt);InputStream fisnew FileInputStream(file);//把流里面左右内容转化为字节jdk1.9以上提供byte[]bytes fis.readAllBytes();System.out.println(new String(bytes));//或者用以下方法/**int iAvail fis.available(); //总的字节数int byte_len0;byte[]bytes2new byte[iAvail];//一个字节数组的读出数据,放置到bytes数组李高效while((byte_lenfis.read(bytes2))!-1){System.out.println(new String(bytes2));}**/}注意 英文一般占用1个字节中文占用3个字节直接用FileInputStream操作字符形式的文件容易出现乱码所以视频、音频、图片这些非字符型的流处理才直接使用FileInputStream 3.4读取字符输入流Reader
我们程序很多时候是操作字符型的输入流如果用字节inputStream来读取其是以字节byte形式容易出现乱码用Reader操作的是字符流是字符不会出现乱码
3.4.1 Reader.read
两则都是通过read方法来读取一个是直接一个是字符。
类读取方法说明InputStreamint read();//读取一个字节返回的是字节本身是一个整数 int read(byte[] bs) //把流内容读取到字节数组bs里Readerint read();//读取的是一个字符char返回这个字符对应的整数int read(char[] chs) //把流的内容读取到一字符数组里
3.4.2 FileReader类说明
我们这个例子是以Reader-InputStreamReader-FileReader为例子 Testpublic void t5() throws IOException {File file new File(d:\\x.txt);Reader readernew FileReader(file);//一个一个字符读取注意read返回的是一个整数用char可以转化为对应的char//在java中char可以用整数表示int ch;while((chreader.read())!-1){System.out.print((char)ch);}reader.close();}Testpublic void t6() throws IOException {File file new File(d:\\x.txt);Reader readernew FileReader(file);//读取一个字符数组高效int len-1;char[] chnew char[10];//这里字符数组的大写可以任意定义也可以定义全部大小//chnew char[(int)file.length()]; //这个写法是long-int,如果文字内容太大可能造成丢失while((lenreader.read(ch))!-1){System.out.print(new String(ch));}//或者这样写 while((lenreader.read(ch,0,10))!-1){System.out.print(new String(ch));}reader.close();}3.5 字节输出流OutputStream
3.5.1 OutputStream.write
OutputStream最重要的方法就是write OutputStream源码 /**b就是字节值如果读完流后返回-1*/
public abstract void write(int b) throws IOException;/**字节数组如果读完流后返回-1*/public void write(byte b[]) throws IOException {write(b, 0, b.length);}/**字节数组如果读完流后返回-1*/public void write(byte b[], int off, int len) throws IOException {Objects.checkFromIndexSize(off, len, b.length);// len 0 condition implicitly handled by loop boundsfor (int i 0 ; i len ; i) {write(b[off i]);}}
3.5.2 FileOutputStream类说明
OutputStream-FileOutputStreamFileOutputStream是处理文件流的类 Testpublic void t7() throws IOException {File file new File(d:\\jj.txt);//把字符串写入OutputStream osnew FileOutputStream(file);String str我热你温\n你个蛤蟆皮;os.write(str.getBytes());os.close();}//文件复制Testpublic void t8() throws IOException {File file new File(d:\\a.txt);File target new File(d:\\jb.txt);InputStream isnew FileInputStream(file);OutputStream osnew FileOutputStream(target);int len-1;byte[] bsnew byte[10];while((lenis.read(bs))!-1){os.write(bs);}System.out.println(copy ok);is.close();os.close();}Testpublic void t9() throws IOException {File file new File(d:\\a.txt);File target new File(d:\\jb.txt);InputStream isnew FileInputStream(file);OutputStream osnew FileOutputStream(target);int c-1;while((cis.read())!-1){os.write(c);}System.out.println(copy ok);is.close();os.close();}
3.6字符输出流Writer
OutputStream一般用于视频、音频、图片等非字符类的资源流如果是文字类型用Writer最方便
3.6.1 Writer.write
和OutputStream一致其最重要的方法就是write方法 Writer源码
/**输入一个char字符对应的整数*/
public void write(int c) throws IOException {synchronized (lock) {if (writeBuffer null){writeBuffer new char[WRITE_BUFFER_SIZE];}writeBuffer[0] (char) c;write(writeBuffer, 0, 1);}}/**输入char数组*/public void write(char cbuf[]) throws IOException {write(cbuf, 0, cbuf.length);}/**字符串*/public void write(String str) throws IOException {write(str, 0, str.length());}/**字符串*/public void write(String str, int off, int len) throws IOException {synchronized (lock) {.....}}3.6.2 FileWriter类说明
代码说明
Testpublic void t10() throws IOException {File file new File(d:\\x.txt);String str宇宙第一胎神四川蛤蟆皮;Writer writernew FileWriter(file);writer.write(str);writer.close();}Testpublic void t11() throws IOException {File file new File(d:\\a.txt);File target new File(d:\\jb2.txt);Reader rdnew FileReader(file);Writer wtnew FileWriter(target);int c-1;//读取字符while((crd.read())!-1){wt.write(c);}System.out.println(copy ok);rd.close();wt.close();}Testpublic void t12() throws IOException {File file new File(d:\\a.txt);File target new File(d:\\jb3.txt);Reader rdnew FileReader(file);Writer wtnew FileWriter(target);int len-1;char[] csnew char[20];//读取字符while((lenrd.read(cs))!-1){wt.write(cs);}System.out.println(copy ok);rd.close();wt.close();}
3.7 BufferedInputStream类说明
使用方式基本和FileInputStream一致。
BufferedInputStream有一个内部缓冲区数组一次性读取较多的字节缓存起来默认读取defaultBufferSize 8192作用于读文件时可以提高性能。