日期:2014-05-17  浏览次数:20651 次

io流储存文件问题
已获得ByteArrayInputString对像,如何把这个储存成本地文件?
io

------解决方案--------------------
用FileOutPutStream 写到本地去。
------解决方案--------------------

首先返回InputStream 流
public static InputStream fromStringToIputStream(String s) {
if (s != null && !s.equals("")) {
try {

ByteArrayInputStream stringInputStream = new ByteArrayInputStream(
s.getBytes());
return stringInputStream;
} catch (Exception e) {

e.printStackTrace();
}
}
return null;
}

然后把inputstream流输出到文件。

/**
 * 输入InputStream流和文件地址,返回成功与否。
 * 
 * @param is
 * @return
 */
public static boolean fromIputStreamToFile(InputStream is,
String outfilepath) {
BufferedInputStream inBuff = null;
BufferedOutputStream outBuff = null;

try {
// 新建文件输入流并对它进行缓冲
inBuff = new BufferedInputStream(is);

// 新建文件输出流并对它进行缓冲
outBuff = new BufferedOutputStream(
new FileOutputStream(outfilepath));

// 缓冲数组
byte[] b = new byte[1024 * 5];
int len;
while ((len = inBuff.read(b)) != -1) {
outBuff.write(b, 0, len);
}
// 刷新此缓冲的输出流
outBuff.flush();
} catch (Exception e) {
return false;
} finally {
try {
// 关闭流
if (inBuff != null)

inBuff.close();
if (outBuff != null)
outBuff.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

return true;
}

------解决方案--------------------
引用:
按照你的这个来写的,不过报异常了java.lang.ArrayIndexOutOfBoundsException
还是这个问题

看下输入流是不是空的