如何将InputSteam 转化成OutputStream 请问如何将InputSteam 转化成OutputStream 写入文件当中? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 读取InputSteam的内容,再写入OutputStream 应该从InputStream中读取数据,然后写入OutputStream中。 private void saveDataFile(InputSteam sin, String saveToDir) throw Exception { OutputStream os = null ; String strSydate = data_System.currentTimeMillis().to_String File fileo = new File(saveToDir + "data_" + strSydate + ".xml" ); try { os = new FileOutputStream(fileo); while(sin.available()>0){ int data = sin.read(); os.write(data); } } catch (IOException e) { e.printStackTrace();}请问以上这个方法可以吗?有个疑问 int data = sin.read() 这个从输入流里读出的为什么是个int类型?而且直接把 int 类型写到了 输出流 os 里边?这个可以吗? 读出来的是int类型,但实际上只读了1byte保存在了int的低8位上。直接把int写到输出流是可以的,因为此方法只将int的低8位写入。 先把inputStream的内容读成string或byte[]然后再把string或byte[]写入outputStream以下内容是读取的步骤呵呵,写入部分自己做吧------------------不超70码的分割线------------------private String readStringFromInputStream(InputStream inputStream, int contentLength, String charSetName) throws Exception { return new String(readByteArrayFromInputStream(inputStream, contentLength), charSetName); } private byte[] readByteArrayFromInputStream(InputStream inputStream, int contentLength) throws Exception { byte[] array = new byte[contentLength]; int readBytesNumber = 0; while (readBytesNumber < contentLength) { int readBytesNumberThisTime = inputStream.read(array, readBytesNumber, contentLength - readBytesNumber); if (readBytesNumberThisTime == -1) { throw new Exception("读字节错误"); } readBytesNumber += readBytesNumberThisTime; } return array; } 日期格式转换问题 关于继承的几点问题 大侠指点 用eclisps SWT打包成jar 失败 Java 怎样实现查询一个数据库有多少表和表名? web.xml关于servlet的配置问题.在线等!!! 谁有jcreator的注册码? 关于java类的全局变量问题 VisualAge的有问题 有关java垃圾收集的疑问 排序问题 [J2ME问题]请问我运行时出现这样的问题要怎么解决
OutputStream os = null ;
String strSydate = data_System.currentTimeMillis().to_String
File fileo = new File(saveToDir + "data_" + strSydate + ".xml" );
try {
os = new FileOutputStream(fileo);
while(sin.available()>0){
int data = sin.read();
os.write(data);
}
} catch (IOException e) {
e.printStackTrace();
}
请问以上这个方法可以吗?
有个疑问 int data = sin.read() 这个从输入流里读出的为什么是个int类型?
而且直接把 int 类型写到了 输出流 os 里边?这个可以吗?
直接把int写到输出流是可以的,因为此方法只将int的低8位写入。
int contentLength, String charSetName) throws Exception {
return new String(readByteArrayFromInputStream(inputStream,
contentLength), charSetName);
} private byte[] readByteArrayFromInputStream(InputStream inputStream,
int contentLength) throws Exception {
byte[] array = new byte[contentLength]; int readBytesNumber = 0;
while (readBytesNumber < contentLength) {
int readBytesNumberThisTime = inputStream.read(array,
readBytesNumber, contentLength - readBytesNumber);
if (readBytesNumberThisTime == -1) {
throw new Exception("读字节错误");
}
readBytesNumber += readBytesNumberThisTime;
}
return array;
}