bufferedwriter在android中不工作
我在我的资产文件夹中存储了一个文件。我阅读了文件,并根据用户要求将某些数据附加到文件中。然后将文件存储到emulator的数据目录中。我正在为此使用bufferedwriter,但它不起作用,在数据文件夹中生成的结果文件大小为0kb。文件正在制作中。 这是我的密码bufferedwriter在android中不工作,android,Android,我在我的资产文件夹中存储了一个文件。我阅读了文件,并根据用户要求将某些数据附加到文件中。然后将文件存储到emulator的数据目录中。我正在为此使用bufferedwriter,但它不起作用,在数据文件夹中生成的结果文件大小为0kb。文件正在制作中。 这是我的密码 InputStream myInput = this.getAssets().open("exportformat.txt"); BufferedReader inputStream = new BufferedRea
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();
}
InputStream myInput=this.getAssets().open(“exportformat.txt”);
BufferedReader inputStream=新的BufferedReader(
新的InputStreamReader(myInput));
字符串outFileName=“/data/data/com.helios.NauticDates/attachment.ics”;
文件checkfile=新文件(outFileName);
if(checkfile.exists()){
checkfile.delete();
}
对于(int i=0;i尝试使用FileWriter
而不是FileOutputStream
,如下所示:
FileWriter myOutput = new FileWriter(outFileName);
BufferedWriter outStream = new BufferedWriter(myOutput);
我有一个代码,其中我将sqlite数据库从assets文件夹复制到android系统的data文件夹
private String DB_PATH =mycontext.getPackageName() + "/databases/";
private static String DB_NAME = "PartyDetails.sqlite";
private void copydatabase() throws IOException {
//Open your local db as the input stream
InputStream myinput = mycontext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outfilename = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myoutput = new FileOutputStream(outfilename);
// transfer byte to inputfile to outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myinput.read(buffer))>0)
{
myoutput.write(buffer,0,length);
}
//Close the streams
myoutput.flush();
myoutput.close();
myinput.close();
}
希望有帮助:-)