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

郑州平台类网站惠州seo关键词

郑州平台类网站,惠州seo关键词,传动设备 技术支持 东莞网站建设,中建招聘2022社会招聘信息FileReader 使用read()方法读取单个字符,下面是如何修改使程序性能更好的过程。 第一种:处理异常方式为throws Testpublic void test() throws IOException {//读取hello.txt,并显示内容// 创建文件对象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 方式处理异常,确保流一定可以关闭// 快捷键,ctrl+T,添加外围的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 方式处理异常,确保流一定可以关闭// 快捷键,ctrl+T,添加外围的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);}}}

也可以使用文件流实现文本复制。
注意:

  • 字符流只能用来操作文本,不能用来处理非文本文件;
  • 对于字节流,通常用来处理非文本文件,涉及文本文件复制操作也可以使用字节流。

处理流

缓冲流

作用:提升文件读写的效率
也是在四个抽象基类之上

  • BufferedInputStream
  • BufferedOutputStream
  • BufferedReader
  • BufferedWriter

示例:

 @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对象的类型。
数据流:

  • DataInputStream
  • DataOutputStream

对象流:
ObjectInputStream
ObjectOutputStream

对象的序列化机制:
允许把内存中的Java对象转换成平台无关的二进制流,从而可以把这种二进制持久地保存在磁盘上,或通过网络将其传输到另一个网络节点,当其他程序获得了这种二进制流,就可以恢复成原来的java对象。

  • 序列化过程:ObjectOutputStream
  • 反序列化过程:ObejectInputStream
http://www.zhongyajixie.com/news/22272.html

相关文章:

  • 安徽党组织标准化建设网站国内十大4a广告公司
  • wordpress汇聚素材网网站搜索优化官网
  • 深圳公司建站推广网时代教育培训机构怎么样
  • wordpress没有css样式湖南seo优化按天付费
  • 做盗号网站2024年1月新冠高峰期
  • 网站运营服务商企业网络营销策划
  • 网络教育网站如何做营销推广万网域名官网
  • 做目录右内容网站手机百度经验首页登录官网
  • 做网站的费用如何写分录如何让网站快速收录
  • 信誉好的镇江网站优化2345手机浏览器
  • 成都什么是网站建设宁波seo整站优化软件
  • 调兵山网站建设白嫖永久服务器
  • 网站效果图确认表万网官网入口
  • 运城门户网站建设电商推广平台有哪些
  • 网站空间 程序有限制吗东莞疫情最新消息今天中高风险区
  • 网站系统繁忙是什么意思百度一下你就知道搜索
  • 网站建设推广合同范本百度网站禁止访问怎么解除
  • 如何快速做单页面网站百度收录的网站多久更新一次
  • 设计相关的网站搜索引擎优化教程
  • 广州和信建设公司网站广告投放这个工作难不难做
  • tom企业邮箱注册网站seo关键词排名查询
  • 免费看看视频用什么软件好seo推广百度百科
  • 村级网站建设 不断增强湖南网站seo营销
  • 网站空间到期 数据网站搭建外贸
  • 天元建设集团有限公司是上市公司seo搜索引擎优化排名
  • 如何分析一个网站的用户网站建设策划书
  • 做的好的h游戏下载网站怎么建网站卖东西
  • 搭建网站需要的软件下载南京谷歌推广
  • 海口制作网站软件百度宁波运营中心
  • 新手怎样自己做网站百度app怎么找人工客服