---
title: "这个读文件为什么不可以呢？"
author: "winzenghua"
date: "2009-01-06T16:20:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-01-06T16:20:00Z"
id: 992379
url: "https://java4gis.iteye.com/blog/992379"
---

项目中有个读Stream的问题，我用了最原始的办法，发现当InputStream中是xml可以读出，当是其他的时候就不可以了。虽然后来有了其他方案解决了，但是还是不知道原因，这里贴出来，想知道大家的看法，谢谢.

try {  
Assert.notNull(file);  
InputStream in = file.getFileContent().getInputStream();  
List&lt;Byte&gt; cache = new ArrayList&lt;Byte&gt;();  
byte data = (byte) in.read();  
while (data != -1) {  
cache.add(data);  
data = (byte) in.read();  
}  
if (in != null) {  
in.close();  
}  
byte\[] cache2 = new byte\[cache.size()];  
for (int i = 0; i &lt; cache.size(); i++) {  
cache2\[i] = cache.get(i);  
}  
System.out.println("cache2: " + cache2.length);  
output.write(cache2);  
} finally {  
if (output != null) {  
output.close();  
}  
}

===&gt;&gt;

try {  
Assert.notNull(file);  
InputStream in = file.getFileContent().getInputStream();  
byte\[] cache2 = new byte\[in.available()];  
in.read(cache2);  
output.write(cache2);  
if (in != null) {  
in.close();  
}  
} finally {  
if (output != null) {  
output.close();  
}  
}