java学习(152):字节缓冲输出流.docx

上传人:A*** 文档编号:142724299 上传时间:2020-08-22 格式:DOCX 页数:6 大小:41.22KB
返回 下载 相关 举报
java学习(152):字节缓冲输出流.docx_第1页
第1页 / 共6页
java学习(152):字节缓冲输出流.docx_第2页
第2页 / 共6页
java学习(152):字节缓冲输出流.docx_第3页
第3页 / 共6页
java学习(152):字节缓冲输出流.docx_第4页
第4页 / 共6页
java学习(152):字节缓冲输出流.docx_第5页
第5页 / 共6页
点击查看更多>>
资源描述

《java学习(152):字节缓冲输出流.docx》由会员分享,可在线阅读,更多相关《java学习(152):字节缓冲输出流.docx(6页珍藏版)》请在金锄头文库上搜索。

1、java学习(152):字节缓冲输出流import javax.imageio.IIOException;import java.io.*;import java.util.Date;/字符输入流public class FileManagerChar public static void readCharFile(File file) FileReader fileReader=null;/文本输入流 if(file.exists() try fileReader = new FileReader( file );/基于目标存在的文本文档输出流 char chs=new char50;/字符

2、零时缓冲区 int count=0;/存储实际读取的字符数量 while(count=fileReader.read(chs,0,chs.length)!=-1) String s=new String( chs,0,count ); System.out.println( s ); catch (IOException e) e.printStackTrace(); finally try fileReader.close(); catch(IOException e) e.printStackTrace(); /使用文本缓冲流读取文件 public static void useBuffe

3、rReader(File file) FileReader read=null;/基于文件的普通输入流 BufferedReader br=null;/基于某个reader建立的字符缓冲流 if(file.exists() try read=new FileReader( file );/基于文件建立普通文本输入流 br=new BufferedReader( read );/基于某个read建立文本缓冲流 char chs=new char25; int count=0; while (count=br.read(chs,0,chs.length)!=-1) String s=new Str

4、ing( chs,0,count ); System.out.println( s ); catch (IOException e) e.printStackTrace(); finally try br.close(); read.close(); System.out.println( 关闭成功 ); catch (IOException e) e.printStackTrace(); /字节输出流 public static void binaryOutStream(String filePath) String str=start=E:BaiduNetdiskDownloadbaidu

5、61.mp4; byte bys=str.getBytes();/将字符串转换为字节数组 OutputStream out=null; try out = new FileOutputStream( filePath); out.write(bys); catch (IOException e) e.printStackTrace(); finally try out.close(); System.out.println( 资源关闭 ); catch (IOException e) e.printStackTrace(); /使用字节缓冲输出流 public static void useB

6、ufferedOutput(File file) OutputStream out=null; BufferedOutputStream bs=null; String str=日照香炉生紫烟,n遥看瀑布挂前川。n飞流直下三千尺,n以适应河洛就停; byte bys=str.getBytes(); if(file.exists() try System.out.println( file.getAbsolutePath() ); out = new FileOutputStream( file.getAbsoluteFile()+/李白诗.doc ); bs=new BufferedOutpu

7、tStream( out );/基于某个outputstream建立缓冲输出流 bs.write( bys ,0,bys.length);/写入目标文件 catch (IOException e) e.printStackTrace(); finally try bs.close(); out.close(); catch (IOException e) e.printStackTrace(); /字符输出流bufferwrite /file文件存储的目录 /filename 文件名称 /content 文件内容 public static void useBufferedWriter(Fil

8、e fir,String fileName,String content) File file=null; Writer writer=null; BufferedWriter bw=null; if(fir.exists() file=new File(fir,fileName ); char chs=content.toCharArray(); try writer=new FileWriter( file ); bw=new BufferedWriter( writer );/基于Writer实例创建字符缓冲流 bw.write(chs);/将char型数组所有内容写入到目标文件中 ca

9、tch (IOException e) e.printStackTrace(); finally try bw.close(); writer.close(); catch (Exception e) e.printStackTrace(); else /创建目录后写入内容 System.out.println( 目标文件目录没找到 ); public static void copyFile(File target,File dir) InputStream in=null; OutputStream out=null; File copyFile=null;/目标写的文件对象 if(target.exists()/判断目标文件是否存在 if(!dir.exists() dir.mkdirs();/如果目标文件不存在,创建目录 try in = new FileInputStream( target );/基于文件建立输入流 String fileName=target.getName();/获取文件源名称 /避免文件重名 copyFile=new File(dir+/+new Date().getTime()+fileName);/基于目标写入文件对象 out=new FileOutputStream( copyFile );/基于目标文件建立输出

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > IT计算机/网络 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号