读写文件io流
下文笔者讲述java中IO读写的代码测试,如下所示
@Test public void FileOutTest() { String filePath="D:/test123.jpeg"; String toPath="D:/test456.png"; FileInputStream fis=null; FileOutputStream fos=null; try { fis=new FileInputStream(filePath); fos=new FileOutputStream(toPath); byte[] bytes=new byte[1024]; while (true){ int len = fis.read(bytes); if(len!=-1){ fos.write(bytes,0,len); }else{ break; } } }catch (Exception e){ e.printStackTrace(); }finally { try { if(fis!=null){ fis.close(); } if(fos!=null){ fos.close(); } }catch (Exception e){ e.printStackTrace(); } } }
新版IO处理
@Test public void FileCache() { try { FileChannel fis = new FileInputStream(filePath).getChannel(); FileChannel fos = new FileOutputStream(toPath).getChannel(); ByteBuffer bb = ByteBuffer.allocate(1024 * 1024); while (true) { int read = fis.read(bb); if (read == -1) { break; } bb.flip(); fos.write(bb); bb.clear(); } fis.close(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。