public File writeText2SDFromInput(String path , String fileName , InputStream input)
{
File file = null ;
StringBuffer sb = new StringBuffer() ;
BufferedReader br = null ;
OutputStreamWriter osw = null ;
OutputStream os = null ;
br = new BufferedReader(new InputStreamReader(input));
String temp = null;
try {
createSDDir(path);
file = createSDFile(path, fileName);
os = new FileOutputStream(file);
while((temp = br.readLine()) != null){
sb.append(temp);
sb.append("/n");
}
//以GB2312的编码格式往指定文件中写入东西,这样可以避免中文乱码的问题
osw = new OutputStreamWriter(os,"GB2312");
osw.write(sb.toString(),1,sb.toString().length() - 1);
osw.flush();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
try {
osw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return file;
}
用OutputStreamWriter 往SDCARD中写入时为空,因为我少写了上述代码中着重的那一行代码。
flush这个方法的作用是把缓冲区的数据强行输出,如果你不flush就可能会没有真正输出。而我运气就很差,每次都没有输出。。。