2011-05-02 66 views
0

我有一个文件,我已经存储在我的资产文件夹。我读取文件并根据用户需求将某些数据附加到文件中。然后我将文件存储到我的模拟器到数据目录中。我正在使用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(); 
    } 

回答

0

尝试使用的FileWriter代替FileOutputStream像这样:

FileWriter myOutput = new FileWriter(outFileName); 
BufferedWriter outStream = new BufferedWriter(myOutput); 
+0

试过不工作。 – user590849 2011-05-02 13:01:58

0

我有在我复制代码sqlite数据库从资产文件夹到android系统的数据文件夹。

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(); 

} 

希望它可以帮助:-)