06

mac2025-02-11  18

6 InputStream和OutputStream基本特点

二者都是【字节】输入输出流的抽象父类。 以字节为单位处理数据,每次读取/写入一个字节。 适合处理二进制文件,如音频、视频等。 实现类有FileInputStream 和 FileOutputStream

FileInputStream in = null; try{ in = new FileInputStream("e:\\测试类文件\\file.txt"); byte[] b =new byte[1024]; int w=in.read(b); //int w获取字节个数查API System.out.println(w); //,0,获取元素个数查API String s = new String(b,0,w); System.out.println(s+s.length()); }catch (Exception e) { e.printStackTrace(); }finally{ try{ in.close(); }catch (IOException e) { e.printStackTrace(); } } FileOutputStream out = null; try{ out = new FileOutputStream("e:\\测试类文件\\file.txt"); String Str = new String ("疯狂的学习"); byte[] b = Str.getBytes(); out.write(b); System.out.println("写入ok"); }catch(Exception e){ e.printStackTrace(); }finally{ try { out.close(); } catch (IOException e) { e.printStackTrace(); } }
最新回复(0)