InputStream bis = new BufferedInputStream(new FileInputStream(new File(hcPath)));是什么意思
时间: 2024-05-29 11:13:12 浏览: 10
?这段代码是用来读取一个文件路径指向的文件,并将其存储到输入流中。InputStream表示输入流,它是Java中所有输入流的父类,BufferedInputStream是缓冲输入流,它可以提供更快的读取速度,FileInputStream是文件输入流,它可以从文件中读取数据。整个代码的作用就是从指定文件中读取数据并存储到输入流中。
相关问题
这段代码会导致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;
这段代码中使用了ZipFile和InputStream来读取压缩文件的内容,但是在finally块中并没有关闭这些资源。如果在读取文件过程中出现异常,这些资源将不会被关闭,可能会导致文件被占用,造成后续的操作失败。
另外,代码中使用了两次ZipFile来打开同一个压缩文件,这是不必要的。
建议对代码进行如下修改:
```java
List<UnzipFileVo> vos = new ArrayList<>();
ZipFile zip = null;
try {
if (Objects.isNull(file) || !file.exists()) {
return Collections.emptyList();
}
zip = new ZipFile(file, Charset.forName("GBK"));
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") || ze.getName().endsWith(".jpg")) {
vo = parseName(ze.getName(), ze, vo);
InputStream in = zip.getInputStream(ze);
MultipartFile multipartFile = getMultipartFile(in, System.currentTimeMillis() + ".png");
vo.setMultipartFile(multipartFile);
vos.add(vo);
}
}
}
} catch (Exception e) {
e.printStackTrace();
throw new ServiceException("文件有误,请重新上传!");
} finally {
if (Objects.nonNull(zip)) {
try {
zip.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//强制清空原来的文件
FileUtil.clean(zipPath);
}
return vos;
```
在修改后的代码中,只使用了一次ZipFile来打开压缩文件,并且在finally块中关闭了ZipFile和InputStream。同时,只有当文件名以".png"或".jpg"结尾时,才会读取该文件的内容并将其转换成MultipartFile对象。
inputstream怎么分段读取文件
可以使用BufferedInputStream类来分段读取文件,代码示例如下:
```java
FileInputStream fis = new FileInputStream("file.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
byte[] buffer = new byte[1024];
int bytesRead = 0;
while ((bytesRead = bis.read(buffer)) != -1) {
// 处理读取的数据,例如写入另一个文件或者打印到控制台
}
bis.close();
fis.close();
```
在这个示例中,我们创建了一个FileInputStream对象来打开文件,然后将其包装在一个BufferedInputStream对象中。接下来,我们定义了一个byte数组作为缓冲区,大小为1024字节。然后在while循环中,我们使用BufferedInputStream的read()方法读取文件中的数据,并将其存储在缓冲区中。如果读取数据成功,则返回读取的字节数。在每次读取数据后,我们可以对缓冲区中的数据进行处理,例如写入另一个文件或者打印到控制台。最后,我们使用BufferedInputStream的close()方法关闭流。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)