IO流基础语法

xiaoxiao2021-02-27  233

字符流和字节流

字节流2个基类 - InputStream - OutputStream

字符流2个基类 - Reader - Writer

字符流

Writer–>FileWriter

FileWriter fw = null; try{ fw = new FileWriter("demo.txt"true); //创建新的文件,true表示不覆盖源文件续写,("demo.txt")则覆盖源文件 fw.writer("asdf"); } catch(IOException e){ System.out.println(e.toString()); } finally{ try{ if(fw!=null){ fw.close();//关闭资源之前会刷新缓冲中的数据 //flush需要时使用,close一定要使用 } } catch(IOException e){ System.out.println(e.toString()); } }

Reader–>FileReader

FileReader fr = new FileReader("demo.text"); char[] buf = new char[1024]; int num = 0; while((num=fr.read(buf)) != -1){ System.out.print(new String(buf,0,num)); } fr.close();

复制文件

FileWriter fw = null; FileReader fr = null; try{ fw = new FileWriter("copy_demo.txt"); fr = new FileReader("demo.txt"); char[] buf = new char[1024]; int len = 0; while((len=fr.read(buf)) != -1) { fw.write(buf,0,len); } } catch(IOException e) { throw new RuntimeException("读写失败"); } finally{ if(fr!=null){ try{ fr.close(); } catch(Exception e){ } } }

字符流的缓冲区

BufferedWriterBufferedReader

BufferedWriter

//FileWriter fw = new FileWriter("demo.text"); //BufferedWriter bw = new BufferedWriter(fw); BufferedWriter bw = new BufferedWriter(new FileWriter("demo.txt")); bw.writer("xxxx"); bw.newLine();//换行,跨平台 bw.flush();//重要用到缓冲区就要刷新 bw.close;//关闭缓冲区就是在关闭缓冲区中的流对象,不用再关闭fw

BufferedReader

BufferedReader br = new BufferedReader(new FileReader("demo.txt")); String line = null; while((line=br.readLine()) != null){ System.out.println(line);//readLine()不返回终止符 } br.close();

复制文件

BufferedReader br = null; BufferedWriter bw = null; try{ br = new BufferedReader(new FileReader("demo.txt")); bw = new BufferedWriter(new FileWriter("copy_demo.txt")); String line = null; while((line=br.readLine()) != null){ bw.write(line); bw.newLine(); bw.flush(); } } catch(Exception e){ throw new RuntimeException("读写失败"); } finally{ try{ if(bw!=null) bw.close(); } catch(Exception e){ throw new RuntimeException("关闭失败"); } try{ if(br!=null) br.close(); } catch(Exception e){ throw new RuntimeException("关闭失败"); } }

LineNunberReader

BufferesReader–>LineNumberReader

LineNUmberReader lnr = new LineNumberReader(new FileReader("demo.txt")); String line = null; lnr.setLineNumber(100); while((line=lnr.readLine()) != null){ System.out.println(lnr.getLineNumber()+":"+line); } lnr.close();

字节流

OutputStream–>FileOutputStream

FileOutputStream fos = new FileOutputStream("img.png"); fos.write("xxxxx".getBytes[]);//已经有数据,不需要flush或close才写入 fos.close();

InputStream–>FileInputStream

FileInputStream fis = new FileInputStream("img.png"); /* byte[] bytes = new byte[fis.available()];//available可以直接获得读取的长度,但是容易发生内存溢出 fis.read(bytes); System.out.println(new String(bytes)); fis.close(); */ byte[] bytes = new byte[1024]; int len = 0; while((len=fis.read(bytes)) != -1){ System.out.println(new String(bytes,0,len)); } fis.close();

复制文件

FileOutputStream fos = null; FileInputStream fis = null; try{ fos = new FileOutputStream("img_copy.png"); fis = new FileInputStream("img.png"); byte[] buf = new byte[1024]; int len =0; while((len=fis.read(buf)) != -1){ fos.write(buf,0,len); } } catch(Exception e){ throw new RuntimeException("读写失败"); } finally{ try{ if(fos!=null) fos.close(); } catch(Exception e){ throw new RuntimeException("关闭失败"); } try{ if(fis!=null) fis.close(); } catch(Exception e){ throw new RuntimeException("关闭失败"); } }

字节流的缓冲区

BufferedOutputStreamBufferedInputStream BufferedOutputStream bos = null; BufferedInputStream bis = null; bis = new BufferedOutputStream(new FileOutputStream("demo.txt")); bos = new BufferesInputStream(new FileInputStream("copy_demo.txt")); int by = 0; while((by=bis.read()) != -1){ bos.write(by); } bos.close(); bis.close();

自定义InputStream的缓冲区–>read和write的特点

read方法读取为byte,而返回值类型为int,是为了避免读取到1111-1111返回-1与判断条件冲突的情况

所以将其提升为int,并且通过& 255(& x0ff)的方式保证提升后前3个字节补0

而write方法写入数据时,只写该数据最低8位

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==0) { 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(); } } /* 11111111 11111111 11111111 11111111 & 00000000 00000000 00000000 11111111 ------------------------------------- 00000000 00000000 00000000 11111111 */

转换流

重要特点:可以指定编码表

读取转换流

//InputStream is = System.in;//键盘录入 FileInputStream fis = new FileInputStream("demo.txt");//文件读取 InputStreamReader isr = new InputStreamReader(fis,"utf-8"); BufferedReader br = new BufferedReader(isr);

写入转换流

//OutputStream out = System.out;//输出到控制台 FileOutputStream fos = new FileOutputStream("demo.txt");//输出到文件 OutputStreamWriter osw = new OutputStreamWriter(fos,"utf-8"); BufferedWriter bw = new BufferedWriter(osw);
转载请注明原文地址: https://www.6miu.com/read-10540.html

最新回复(0)