Android:一个编写器不写入文件

时间:2014-11-28 21:45:25

标签: java android outputstream writer

这是我的功能:

//This function is for parsing the online xml file and adding those to the ".txt" file
private void downloadData() throws Exception {
    Thread thread = new Thread(){
        public void run(){
            HttpURLConnection http = null;
            InputStream xmlStream = null;
            try {
                URL url = new URL(rateUrl);
                http = (HttpURLConnection) url.openConnection();
                XMLPullParserHandler parser = new XMLPullParserHandler();

                currency_name.add("EUR");
                currency_rate.add(1.0);
                parser.parse(http.getInputStream(), currency_name, currency_rate);

                //Uppdate ui on the ui-thread
                runOnUiThread(new Runnable(){
                    public void run() {
                        adapter.notifyDataSetChanged();

                        //Write to file, THIS IS WHERE THE PROBLEM IS
                        PrintWriter writer = null;
                        try {
                            OutputStream os = MainActivity.this.openFileOutput(currency_file, Context.MODE_PRIVATE);
                            writer = new PrintWriter(os);
                            //Currency data files
                            for(int i = 0; i < currency_name.size(); i++){
                                writer.println(currency_name.get(i).toString() + ":" + currency_rate.get(i).toString());
                            }

                            //Date file (time)
                            String timeStamp = new SimpleDateFormat("yyyyMMddHHmmss").format(Calendar.getInstance().getTime());
                            OutputStream os2 = MainActivity.this.openFileOutput(time_file, Context.MODE_PRIVATE);
                            writer = new PrintWriter(os2);
                            writer.println(timeStamp);
                        }
                        catch(IOException ioe) {
                            showToast("Error while writing to files.");
                        }
                        finally {
                            if(writer != null) {
                                writer.close();
                            }
                        }
                    }
                });
            }
            catch (Exception e) {
                showToast("Some error");
            }
            finally{
                try{
                    if(xmlStream != null){
                        xmlStream.close();
                    }
                }
                catch (Exception e){
                    showToast("some error2");
                }
                if(http != null){
                    http.disconnect();
                }
            }
        }
    };
    thread.start();
}

在输出流(第一个)上,当它应该写入currency_file时,它不会这样做,但是第二个写入剂量在time_file中写入日期。我错过了什么吗?

0 个答案:

没有答案