【问题标题】:OutputStreamWriter not writingOutputStreamWriter 不写
【发布时间】:2012-08-21 13:23:15
【问题描述】:

我创建了一个带有异步任务的文件。之后安排一个执行程序每秒一次将信息写入所述文件。一旦我触摸一个按钮,执行程序就会关闭,文件也会关闭,但文件中通常没有写入任何内容。

代码:

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。

【问题讨论】:

    标签: android file android-asynctask fileoutputstream executor


    【解决方案1】:

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

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

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

    【讨论】:

    • 对主要活动进行 I/O 操作有多安全?因为我只是为了避免这种情况而创建了该异步任务。我得到 EBADF,文件号错误。
    • 你绝对不应该在主线程上做 I/O。创建 AsyncTask 是正确的做法,但关键是它是异步的。调用 AsyncTask.execute() 后的所有代码都应该在 AsyncTask.onPostExecute() 方法中运行。
    • 你会推荐使用 ASyncTask 而不是 ScheduledTaskExecutor 吗?我设法通过实现回调来同步所有内容。
    • ScheduledTaskExecutor 的优点是可以让你定期做一些事情,AsyncTasks 是一次性的,但是你可以有一个计划下一个,虽然这看起来更脆弱,我认为 Executor 适合你目的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2012-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    相关资源
    最近更新 更多