OutputStreamWriter不写 [英] OutputStreamWriter not writing

查看:196
本文介绍了OutputStreamWriter不写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有asynctask的文件.之后安排执行程序每秒一次将信息写入所述文件.一旦我按下按钮,执行器就会关闭,文件也将关闭,但通常不会在文件中写入任何内容.

I have created a file with an asynctask. Afterwards scheduled an executor to write information to said file once every second. Once I touch a button the executor is shut down and the file closed but more often than not nothing is written in the file.

代码:

startButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {

                preferences.edit().putBoolean(GlobalConstants.stopPreference,false).commit();
                loc_thr = new LocationThread(preferences, getApplicationContext());
                loc_thr.run();

                startButton.setVisibility(View.INVISIBLE);
                startButton.setClickable(false);
                stopButton.setVisibility(View.VISIBLE);
                stopButton.setClickable(true);

                currentDateAndTime = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                fileName = getString(R.string.loc_log_path) + currentDateAndTime + ".txt";
                new CreateFileTask(fileName).execute();
                loc_file = new File(fileName);
                try {
                    FOS = new FileOutputStream(loc_file.getAbsolutePath());
                    OSW = new OutputStreamWriter(FOS);
                    OSW.write(GlobalConstants.fileHeader + '\n');
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                scheduledTaskExecutor = Executors.newScheduledThreadPool(5);
                scheduledTaskExecutor.scheduleAtFixedRate(new Runnable() {
                    public void run() {
                        if(loc_thr.getLocationStatus() & loc_thr.newLocation() & !preferences.getBoolean(GlobalConstants.stopPreference,false)){
                            SensorBundle SB = new SensorBundle(loc_thr.getCurrentLocation(),loc_thr.getCurrentGPSStatus());
                            try {
                                OSW.write(new SimpleDateFormat("yyyy/MM/dd;hh:mm:ss").format(new Date()) + ";");
                                OSW.write(SB.getLatitude() + ";");
                                OSW.write(SB.getLongitude() + ";");
                                OSW.write(SB.getAltitude() + ";");
                                OSW.write(SB.getAccuracy() + ";");
                                OSW.write(SB.getProvider() + ";");
                                OSW.write('\n');
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        } else{
                            if(preferences.getBoolean(GlobalConstants.stopPreference,false)){
                                try {
                                    OSW.close();
                                    FOS.close();

                                    scheduledTaskExecutor.shutdown();
                                } catch (IOException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }
                        }
                    }
                }, 0, 1, TimeUnit.SECONDS);
                }
        });

每当按下停止按钮时,先前查询的SharedPreference就会设置为true.

Whenever the stop button is pressed the SharedPreference queried earlier is set to true.

推荐答案

我认为您的问题可能在这里

I think your problem may be here

   new CreateFileTask(fileName).execute();
   loc_file = new File(fileName);

我假设此任务创建了文件,并且您希望可以在new File(fileName)下创建文件.这是否成立是不确定的.如果AsyncTask CreateFileTask计划运行并在执行下一条语句之前完成,则文件将在该文件中,否则将不会存在.您是否从IOException或FileNoteFoundExceptions中看到logcat中的堆栈跟踪?

I assume this task creates the file and you're expecting that when you can new File(fileName) the file is already created. Whether this is true or not is indeterminate. If the AsyncTask CreateFileTask is scheduled to run and completes before the next statement is executed then the file will be there, otherwise it won't be. Are you seeing stack traces in logcat from the IOException or FileNoteFoundExceptions?

这篇关于OutputStreamWriter不写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆