如何把文件流变成的字符串变回文件
怎么把文件流变成的字符串变回文件?
也就是以下过程的逆过程:
public String selfReadFile(String strFileName){
StringBuffer buf=null;
BufferedReader breader = null;//reader for the template files
try
{
breader = new BufferedReader(new InputStreamReader(new FileInputStream((strFileName)),Charset.forName("utf-8")));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
e.printStackTrace();
}//catch
return buf.toString();
}
------解决方案--------------------
output stream
------解决方案--------------------
InputStream is=null;
BufferedReader br=null;
try {
is=asset.open("airport.txt");
br=new BufferedReader(new InputStreamReader(is,"GBK"));
fos=new FileOutputStream("/data/data/com.tarena.android/airport_1.txt");
String str=null;
while((str=br.readLine())!=null){
fos.write(str.getBytes());
fos.flush();
}catch (IOException e) {
// TODO: handle exception
}finally{
if(br!=null)try{br.close();}catch(IOException e){}
if(is!=null)try{is.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}
也就是以下过程的逆过程:
public String selfReadFile(String strFileName){
StringBuffer buf=null;
BufferedReader breader = null;//reader for the template files
try
{
breader = new BufferedReader(new InputStreamReader(new FileInputStream((strFileName)),Charset.forName("utf-8")));
buf = new StringBuffer();
while(breader.ready())
buf.append((char)breader.read());
breader.close();
}//try
catch(Exception e)
{
e.printStackTrace();
}//catch
return buf.toString();
}
------解决方案--------------------
output stream
------解决方案--------------------
InputStream is=null;
BufferedReader br=null;
try {
is=asset.open("airport.txt");
br=new BufferedReader(new InputStreamReader(is,"GBK"));
fos=new FileOutputStream("/data/data/com.tarena.android/airport_1.txt");
String str=null;
while((str=br.readLine())!=null){
fos.write(str.getBytes());
fos.flush();
}catch (IOException e) {
// TODO: handle exception
}finally{
if(br!=null)try{br.close();}catch(IOException e){}
if(is!=null)try{is.close();}catch(IOException e){}
if(fos!=null)try{fos.close();}catch(IOException e){}
}