BufferedInputStream bis = new BufferedInputStream(new FileInputStream(String.valueOf(getResourceAsStream("贪吃蛇游戏/null.txt"))));

时间: 2023-06-20 18:05:54 浏览: 39
这段代码创建了一个BufferedInputStream对象,用于读取资源文件"贪吃蛇游戏/null.txt"。 其中getResourceAsStream()方法是用于从类路径中获取资源的,它返回一个InputStream对象,表示该资源的字节流。String.valueOf()方法则将该InputStream对象转换为字符串类型,以便传递给FileInputStream构造函数。 最终创建的BufferedInputStream对象被用于读取该资源文件的内容。由于使用了缓冲流,因此可以提高读取效率。
相关问题

BufferedInputStream bis = new BufferedInputStream(new FileInputStream(String.valueOf(Text.class.getResourceAsStream("贪吃蛇游戏/null.txt")))); byte[] bys = new byte[1024]; int len; while ((len = bis.read(bys)) != -1) { System.out.print(new String(bys, 0, len));//测试用 String s2 = new String(bys, 0, len); score = Integer.valueOf(s2); }

这段代码的作用是从资源文件 "贪吃蛇游戏/null.txt" 读取数据,并将其中的整数值赋值给变量 score。具体来说,这段代码使用了 BufferedInputStream 类来读取文件,以 byte 数组的形式缓存了读取到的数据,然后将 byte 数组转换为字符串并解析其中的整数值,并将其赋值给 score 变量。需要注意的是,这段代码中的文件路径可能需要根据实际情况进行修改。

import java.io.IOException; import java.io.FileOutputStream; import java.io.FileInputStream; import java.io.BufferedInputStream; public class fileDemo { public static void main(String[] args) throws IOException { method1(); method2(); method3(); method4(); } // 一次读取一个字节 public static void method1() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=fis.read())!=-1) { //System.out.println((char)b); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 一次读取一个字节数组 public static void method2() throws IOException { FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; byte[] bs=new byte[1024]; while((b=fis.read(bs))!=-1) { //System.out.println(new String(bs)); fos.write(bs,0,b); } long end1 = System.currentTimeMillis(); System.out.println("使用FileInputStream的readread(byte[] b)方法复制时间:" + (end1 - start1) + "ms"); fis.close(); fos.close(); } // 缓冲流+一次读一个字节 public static void method3() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int b=0; while((b=bis.read())!=-1) { //System.out.println((char)(b)); fos.write((char)b); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } //缓冲流+一次读一个字节数组 public static void method4() throws IOException { BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); FileOutputStream fos = new FileOutputStream("copy.mp4"); long start1 = System.currentTimeMillis(); int len=0; byte[] bs=new byte[1024]; while((len=bis.read(bs))!=-1) { //System.out.println(new String(bs,0,len)); fos.write(bs,0,len); } long end1 = System.currentTimeMillis(); System.out.println("使用BufferedInputStream的read(byet[] b)方法复制时间:" + (end1 - start1) + "ms"); bis.close(); fos.close(); } } 给出每一行代码的注释

``` import java.io.IOException; // 导入IOException类 import java.io.FileOutputStream; // 导入FileOutputStream类 import java.io.FileInputStream; // 导入FileInputStream类 import java.io.BufferedInputStream; // 导入BufferedInputStream类 public class fileDemo { // 定义fileDemo类 public static void main(String[] args) throws IOException { // 主函数,抛出IOException异常 method1(); // 调用method1函数 method2(); // 调用method2函数 method3(); // 调用method3函数 method4(); // 调用method4函数 } // 一次读取一个字节 public static void method1() throws IOException { // 定义method1函数,抛出IOException异常 FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); // 创建FileInputStream对象fis,打开文件D:\bbb.mp4 FileOutputStream fos = new FileOutputStream("copy.mp4"); // 创建FileOutputStream对象fos,打开文件copy.mp4 long start1 = System.currentTimeMillis(); // 获取当前时间 int b=0; // 定义整型变量b并初始化为0 while((b=fis.read())!=-1) { // 当读取到的字节不等于-1时,执行循环 //System.out.println((char)b); // 输出读取到的字节 fos.write((char)b); // 将读取到的字节写入文件 } long end1 = System.currentTimeMillis(); // 获取当前时间 System.out.println("使用FileInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); // 输出复制所用时间 fis.close(); // 关闭文件输入流 fos.close(); // 关闭文件输出流 } // 一次读取一个字节数组 public static void method2() throws IOException { // 定义method2函数,抛出IOException异常 FileInputStream fis=new FileInputStream("D:\\bbb.mp4"); // 创建FileInputStream对象fis,打开文件D:\bbb.mp4 FileOutputStream fos = new FileOutputStream("copy.mp4"); // 创建FileOutputStream对象fos,打开文件copy.mp4 long start1 = System.currentTimeMillis(); // 获取当前时间 int b=0; // 定义整型变量b并初始化为0 byte[] bs=new byte[1024]; // 创建1024字节的字节数组bs while((b=fis.read(bs))!=-1) { // 当读取到的字节不等于-1时,执行循环 //System.out.println(new String(bs)); // 输出读取到的字节 fos.write(bs,0,b); // 将读取到的字节写入文件 } long end1 = System.currentTimeMillis(); // 获取当前时间 System.out.println("使用FileInputStream的readread(byte[] b)方法复制时间:" + (end1 - start1) + "ms"); // 输出复制所用时间 fis.close(); // 关闭文件输入流 fos.close(); // 关闭文件输出流 } // 缓冲流+一次读一个字节 public static void method3() throws IOException { // 定义method3函数,抛出IOException异常 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); // 创建BufferedInputStream对象bis,打开文件D:\bbb.mp4 FileOutputStream fos = new FileOutputStream("copy.mp4"); // 创建FileOutputStream对象fos,打开文件copy.mp4 long start1 = System.currentTimeMillis(); // 获取当前时间 int b=0; // 定义整型变量b并初始化为0 while((b=bis.read())!=-1) { // 当读取到的字节不等于-1时,执行循环 //System.out.println((char)(b)); // 输出读取到的字节 fos.write((char)b); // 将读取到的字节写入文件 } long end1 = System.currentTimeMillis(); // 获取当前时间 System.out.println("使用BufferedInputStream的read()方法复制时间:" + (end1 - start1) + "ms"); // 输出复制所用时间 bis.close(); // 关闭缓冲输入流 fos.close(); // 关闭文件输出流 } // 缓冲流+一次读一个字节数组 public static void method4() throws IOException { // 定义method4函数,抛出IOException异常 BufferedInputStream bis=new BufferedInputStream(new FileInputStream("D:\\bbb.mp4")); // 创建BufferedInputStream对象bis,打开文件D:\bbb.mp4 FileOutputStream fos = new FileOutputStream("copy.mp4"); // 创建FileOutputStream对象fos,打开文件copy.mp4 long start1 = System.currentTimeMillis(); // 获取当前时间 int len=0; // 定义整型变量len并初始化为0 byte[] bs=new byte[1024]; // 创建1024字节的字节数组bs while((len=bis.read(bs))!=-1) { // 当读取到的字节数不等于-1时,执行循环 //System.out.println(new String(bs,0,len)); // 输出读取到的字节 fos.write(bs,0,len); // 将读取到的字节数组写入文件 } long end1 = System.currentTimeMillis(); // 获取当前时间 System.out.println("使用BufferedInputStream的read(byet[] b)方法复制时间:" + (end1 - start1) + "ms"); // 输出复制所用时间 bis.close(); // 关闭缓冲输入流 fos.close(); // 关闭文件输出流 } } ```

相关推荐

这段代码会导致F:\detailsZIP\cccccnew.zip: 另一个程序正在使用此文件,进程无法访问。这个问题吗; List<UnzipFileVo> vos = new ArrayList<>(); ZipFile zip = null; InputStream in = null; try { if (Objects.isNull(file)) { // if (Objects.isNull(file) || !file.exists()) { return Collections.emptyList(); } //ZipFile zip = new ZipFile(file); //F:\detailsZIP\有文本.zip zip = new ZipFile(file, Charset.forName("GBK"));//解决中文乱码 if (Objects.isNull(zip)) { return Collections.emptyList(); } ZipFile zf = new ZipFile(file); BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); ZipInputStream zis = new ZipInputStream(bis); ZipEntry ze; while((ze=zis.getNextEntry()) != null){ if(ze.isDirectory()){ System.out.println("这是目录"); }else{ UnzipFileVo vo = new UnzipFileVo(); if(ze.getName().endsWith(".png")){ vo = parseName(ze.getName(), ze, vo); }else if(ze.getName().endsWith(".jpg")){ vo = parseName(ze.getName(), ze, vo); } in = zip.getInputStream(ze); MultipartFile multipartFile = getMultipartFile(in, System.currentTimeMillis() + ".png"); vo.setMultipartFile(multipartFile); vos.add(vo); } } } catch (Exception e) { e.printStackTrace(); if (Objects.nonNull(e)) { throw new ServiceException("文件有误,请重新上传!"); } else { Integer code = ((CheckedException) e).getCode(); if (Objects.equals(code, 201)) { throw new CheckedException(e.getMessage()); } } } finally { if (Objects.nonNull(zip)) { try { zip.close(); } catch (IOException e) { e.printStackTrace(); } } if (Objects.nonNull(in)) { try { in.close(); } catch (IOException e) { e.printStackTrace(); } } //强制清空原来的文件 FileUtil.clean(zipPath); } return vos;

最新推荐

recommend-type

基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip

【资源说明】 基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip基于51单片机的音乐播放器设计+全部资料+详细文档(高分项目).zip 【备注】 1、该项目是个人高分项目源码,已获导师指导认可通过,答辩评审分达到95分 2、该资源内项目代码都经过测试运行成功,功能ok的情况下才上传的,请放心下载使用! 3、本项目适合计算机相关专业(人工智能、通信工程、自动化、电子信息、物联网等)的在校学生、老师或者企业员工下载使用,也可作为毕业设计、课程设计、作业、项目初期立项演示等,当然也适合小白学习进阶。 4、如果基础还行,可以在此代码基础上进行修改,以实现其他功能,也可直接用于毕设、课设、作业等。 欢迎下载,沟通交流,互相学习,共同进步!
recommend-type

2024xxx市智能静态交通系统运营项目可行性实施方案[104页Word].docx

2024xxx市智能静态交通系统运营项目可行性实施方案[104页Word].docx
recommend-type

zigbee-cluster-library-specification

最新的zigbee-cluster-library-specification说明文档。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

实现实时数据湖架构:Kafka与Hive集成

![实现实时数据湖架构:Kafka与Hive集成](https://img-blog.csdnimg.cn/img_convert/10eb2e6972b3b6086286fc64c0b3ee41.jpeg) # 1. 实时数据湖架构概述** 实时数据湖是一种现代数据管理架构,它允许企业以低延迟的方式收集、存储和处理大量数据。与传统数据仓库不同,实时数据湖不依赖于预先定义的模式,而是采用灵活的架构,可以处理各种数据类型和格式。这种架构为企业提供了以下优势: - **实时洞察:**实时数据湖允许企业访问最新的数据,从而做出更明智的决策。 - **数据民主化:**实时数据湖使各种利益相关者都可
recommend-type

list根据id查询pid 然后依次获取到所有的子节点数据

可以使用递归的方式来实现根据id查询pid并获取所有子节点数据。具体实现可以参考以下代码: ``` def get_children_nodes(nodes, parent_id): children = [] for node in nodes: if node['pid'] == parent_id: node['children'] = get_children_nodes(nodes, node['id']) children.append(node) return children # 测试数
recommend-type

JSBSim Reference Manual

JSBSim参考手册,其中包含JSBSim简介,JSBSim配置文件xml的编写语法,编程手册以及一些应用实例等。其中有部分内容还没有写完,估计有生之年很难看到完整版了,但是内容还是很有参考价值的。
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

实现实时监控告警系统:Kafka与Grafana整合

![实现实时监控告警系统:Kafka与Grafana整合](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9tbWJpei5xcGljLmNuL21tYml6X2pwZy9BVldpY3ladXVDbEZpY1pLWmw2bUVaWXFUcEdLT1VDdkxRSmQxZXB5R1lxaWNlUjA2c0hFek5Qc3FyRktudFF1VDMxQVl3QTRXV2lhSWFRMEFRc0I1cW1ZOGcvNjQw?x-oss-process=image/format,png) # 1.1 Kafka集群架构 Kafka集群由多个称为代理的服务器组成,这
recommend-type

未定义标识符CFileFind

CFileFind 是MFC(Microsoft Foundation Class)中的一个类,用于在Windows文件系统中搜索文件和目录。如果你在使用CFileFind时出现了“未定义标识符”的错误,可能是因为你没有包含MFC头文件或者没有链接MFC库。你可以检查一下你的代码中是否包含了以下头文件: ```cpp #include <afx.h> ``` 另外,如果你在使用Visual Studio开发,还需要在项目属性中将“使用MFC”设置为“使用MFC的共享DLL”。这样才能正确链接MFC库。