当前位置: 首页 > news >正文

网站服务器++免费铁总建设函网站

网站服务器++免费,铁总建设函网站,怎样创建网站的代码,淘宝店群软件定制开发FileReader 使用read()方法读取单个字符#xff0c;下面是如何修改使程序性能更好的过程。 第一种#xff1a;处理异常方式为throws Testpublic void test() throws IOException {//读取hello.txt#xff0c;并显示内容// 创建文件对象File file new File(hello.txt…FileReader 使用read()方法读取单个字符下面是如何修改使程序性能更好的过程。 第一种处理异常方式为throws Testpublic void test() throws IOException {//读取hello.txt并显示内容// 创建文件对象File file new File(hello.txt);// 读取数据// 输入型的字符流用于读取数据FileReader fileReader new FileReader(file);// 显示数据,read()读取单个字符// 方式1 // int data fileReader.read(); // while(data ! -1){ // System.out.print((char)data); // data fileReader.read(); // // }// 方式2int data;while((data fileReader.read()) ! -1){System.out.print((char)data);}// 关闭流否则造成内存泄漏fileReader.close(); // 由于使用到异常处理可以看到当抛出异常之后close可能就不能调用到这一步所以应该用try,catch方法}但是遇到异常就抛出了无法进行到close()因此使用try-catch-finally方法。 Testpublic void test1() {// 使用try catch finally 方式处理异常确保流一定可以关闭// 快捷键ctrlT,添加外围的if等FileReader fileReader null;try {File file new File(hello.txt);fileReader new FileReader(file);int data;while((data fileReader.read()) ! -1){System.out.print((char)data);}} catch (IOException e) {e.printStackTrace();} finally {try {// 更好的增加健壮性再判断一下是否为空if (fileReader ! null) fileReader.close();} catch (IOException e) {throw new RuntimeException(e);}}}在读取显示这里还可以改进通过每次读取多个字符存到字符数组中较少与磁盘交互的次数。 如下所示 Testpublic void test1() {// 使用try catch finally 方式处理异常确保流一定可以关闭// 快捷键ctrlT,添加外围的if等FileReader fileReader null;try {File file new File(hello.txt);fileReader new FileReader(file);char [] cbuffer new char[5];int len;while((len fileReader.read(cbuffer)) ! -1){for (int i 0; i len; i){ // 不是 cbuffer.lengthSystem.out.print(cbuffer[i]);}}} catch (IOException e) {e.printStackTrace();} finally {try {// 更好的增加健壮性再判断一下是否为空if (fileReader ! null) fileReader.close();} catch (IOException e) {throw new RuntimeException(e);}}}FileWriter 示例 Testpublic void test2() {// 读入操作// 创建文件FileWriter fw null;try {File file new File(write.txt);//fw new FileWriter(file); // 覆盖文件使用的构造器fw new FileWriter(file, true); // 现有基础上增加内容的构造器fw.write(I did do this.\n);fw.write(You are so glad.\n);} catch (IOException e) {throw new RuntimeException(e);} finally {try {if (fw ! null)fw.close();} catch (IOException e) {throw new RuntimeException(e);} finally {}}}执行步骤 创建读取或写出的File类的对象创建输入流或输出流具体的读入或写出的过程关闭流资源避免内存泄漏 图片音频视频是字节为单位进行存储的。不能用上面处理字符的进行操作。 FileInputStream、FileOutputStream 使用方法如下所示 Testpublic void test() {FileInputStream fileInputStream null;FileOutputStream fileOutputStream null;try {// 不同于单纯的文字实现图片的读取存放和复制// 创建相关的file类对象File srcFile new File(test.jpg);File destFile new File(test1.jpg);// 读入写出fileInputStream new FileInputStream(srcFile);fileOutputStream new FileOutputStream(destFile);// 数据的读入和写出byte[] buffer new byte[1024];int len;while((len fileInputStream.read(buffer)) ! -1){fileOutputStream.write(buffer, 0, len);}System.out.println(复制图片成功);} catch (IOException e) {e.printStackTrace();} finally {try {if(fileOutputStream ! null)fileOutputStream.close();} catch (IOException e) {throw new RuntimeException(e);}try {if (fileInputStream ! null)fileInputStream.close();} catch (IOException e) {throw new RuntimeException(e);}}}也可以使用文件流实现文本复制。 注意 字符流只能用来操作文本不能用来处理非文本文件对于字节流通常用来处理非文本文件涉及文本文件复制操作也可以使用字节流。 处理流 缓冲流 作用提升文件读写的效率 也是在四个抽象基类之上 BufferedInputStreamBufferedOutputStreamBufferedReaderBufferedWriter 示例 Testpublic void test() {FileInputStream fis null;FileOutputStream fos null;BufferedInputStream bis null;BufferedOutputStream bos null; // 外层try {// 字节流测试File srcFile new File(hello.txt);File destFile new File(hello1.txt);// 创建输入输出fis new FileInputStream(srcFile);fos new FileOutputStream(destFile);bis new BufferedInputStream(fis);bos new BufferedOutputStream(fos);byte [] buffer new byte[1024];int len;while((len bis.read(buffer)) ! -1){bos.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {// 关闭先关外层再关内层// 外层流的关闭也会对内层流自动关闭try {bos.close();} catch (IOException e) {throw new RuntimeException(e);}try {bis.close();} catch (IOException e) {throw new RuntimeException(e);} // fos.close(); // fis.close();}}转化流 对象流 了解数据流只支持java基本数据类型和字符串的读写不支持其他java对象的类型。 数据流 DataInputStreamDataOutputStream 对象流 ObjectInputStream ObjectOutputStream 对象的序列化机制 允许把内存中的Java对象转换成平台无关的二进制流从而可以把这种二进制持久地保存在磁盘上或通过网络将其传输到另一个网络节点当其他程序获得了这种二进制流就可以恢复成原来的java对象。 序列化过程ObjectOutputStream反序列化过程ObejectInputStream
http://www.w-s-a.com/news/225313/

相关文章:

  • 邯郸营销型网站国际招聘人才网
  • hexo wordpress 主题织梦网站优化教程
  • 网站建设方案及上海市建设协会网站
  • 轴承外贸网站怎么做南宁网站排名优化公司哪家好
  • 沈阳企业网站建站郴州优化公司
  • cctv5+手机在线直播观看seo关键词排名优化方法
  • 网站建设公司怎么谈单怎么开通微信小程序商店
  • 深圳做网站案例一个服务器可以备案几个网站
  • 网络营销策划名词解释泉州百度推广排名优化
  • 一键生成网站的软件互联网营销师是干什么
  • 网站后台管理水印怎么做手机优化设置
  • 哪个网站做图文素材多wordpress++优化
  • 建设网站就选用什么样的公司网站类型分类有哪些
  • 找平面设计师网站网站建设须知
  • 建设联结是不是正规网站wordpress 微博同步
  • 瑞安微网站建设广州推广
  • 做旅游宣传网站的流程图中国企业集成网电子商务
  • 开发商城网站开发成交功能网站
  • 网站建设公司专业公司排名搭建网站的企业
  • 网站建设难吗海南智能网站建设报价
  • 企业网站建设选题的依据及意义校园网站建设的论文
  • 网站版面设计方案水电维修在哪个网站上做推广好些
  • 邹平建设局官方网站企业宣传片广告公司
  • 南京建设集团网站建站极速通
  • 网站建设与推广员岗位职责网站开发应如何入账
  • 企业网站的作用和目的手机回收站
  • 大连零基础网站建设培训电话郎溪做网站
  • 成都科技网站建设注册公司最少需要多少注册资金
  • 找公司做网站注意事项麻城建设局网站停办
  • 沧州企业做网站wordpress 消息通知