我有一个文件,我已经存储在我的资产文件夹。我读取文件并根据用户需求将某些数据附加到文件中。然后我将文件存储到我的模拟器到数据目录中。我正在使用bufferedwriter达到此目的,但它不起作用,并且数据文件夹中生成的结果文件的大小为0kb。该文件正在制作中。 这里是我的代码bufferedwriter不工作在android
InputStream myInput = this.getAssets().open("exportformat.txt");
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(myInput));
String outFileName = "/data/data/packagename/attachment.txt";
OutputStream myOutput = new FileOutputStream(outFileName);
BufferedWriter outStream = new BufferedWriter(
new OutputStreamWriter(myOutput));
String datafromfile;
while ((datafromfile = inputStream.readLine()) != null) {
StringBuffer sb = new StringBuffer(datafromfile);
if (datafromfile.equals("DTSTART:"))
sb.append(details[2]);
if (datafromfile.equals("DTEND:"))
sb.append(details[3]);
if (datafromfile.equals("SUMMARY:"))
sb.append(details[0]);
if (datafromfile.equals("DESCRIPTION:"))
sb.append(details[1]);
datafromfile = sb.toString();
outStream.write(datafromfile);
outStream.newLine();
}
// Close the streams
myOutput.flush();
myOutput.close();
outStream.close();
inputStream.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
我试过调试。数据正在被正确读取,这只是写操作不起作用。
谢谢你提前。
答:
下面的代码工作:
InputStream myInput = this.getAssets().open("exportformat.txt");
BufferedReader inputStream = new BufferedReader(
new InputStreamReader(myInput));
String outFileName = "/data/data/com.helios.NauticDates/attachment.ics";
File checkfile = new File(outFileName);
if(checkfile.exists()){
checkfile.delete();
}
for(int i =0 ;i<4;i++){
if(details[i].equals("null"))
details[i]=" ";
}
FileOutputStream myOutput = new FileOutputStream(outFileName);
String datafromfile;
while ((datafromfile = inputStream.readLine()) != null) {
StringBuffer sb = new StringBuffer(datafromfile);
if (datafromfile.equals("DTSTART:"))
sb.append(details[2]);
if (datafromfile.equals("DTEND:"))
sb.append(details[3]);
if (datafromfile.equals("SUMMARY:"))
sb.append(details[0]);
if (datafromfile.equals("DESCRIPTION:"))
sb.append(details[1]);
if(datafromfile.equals("CATEGORIES:"))
sb.append(details[4]);
datafromfile = sb.toString();
datafromfile+="\n";
byte[] temp = datafromfile.getBytes();
myOutput.write(temp);
}
// Close the streams
myOutput.flush();
myOutput.close();
inputStream.close();
emailintent.putExtra(Intent.EXTRA_STREAM,Uri.parse(outFileName));
emailintent.setType("plain/text");
startActivity(Intent.createChooser(emailintent, "Send mail..."));
} catch (Exception e) {
e.printStackTrace();
}
试过不工作。 – user590849 2011-05-02 13:01:58