Java 安卓:一位作家唐';归档

Java 安卓:一位作家唐';归档,java,android,outputstream,writer,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;

以下是我的功能:

//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();
}
//此函数用于解析联机xml文件并将其添加到“.txt”文件中
私有void downloadData()引发异常{
线程线程=新线程(){
公开募捐{
HttpURLConnection http=null;
InputStream xmlStream=null;
试一试{
URL URL=新URL(rateUrl);
http=(HttpURLConnection)url.openConnection();
XMLPullParserHandler parser=新的XMLPullParserHandler();
货币名称加上(“欧元”);
货币汇率加上(1.0);
parser.parse(http.getInputStream(),货币名称,货币汇率);
//ui线程上的Uppdate ui
runOnUiThread(新的Runnable(){
公开募捐{
adapter.notifyDataSetChanged();
//写入文件,这就是问题所在
PrintWriter=null;
试一试{
OutputStream os=MainActivity.this.openFileOutput(货币\u文件,上下文.MODE\u私有);
writer=新的PrintWriter(操作系统);
//货币数据文件
对于(int i=0;i

在outputstream(第一个)上,当它应该写入货币_文件时,它不会这样做,但是第二个写入程序会在时间_文件中写入日期。我错过什么了吗

您没有关闭第一个
PrintWriter
实例。请尝试使用调试器,在发布之前先查看哪里出错