文件编写者Android

时间:2018-03-14 10:48:39

标签: java android

我正在使用以下代码从我的 FileWriter.class 编写onPostExecute进程文件,它工作正常,但我正在更新我的应用程序,我必须使用{{ 1}}而不是这个,我试着跟随。我知道做错事。任何帮助或指示都会有所帮助。

感谢。

Environment.getExternalStorageDirectory().getPath();

修改 我忘了添加我试图做一些后台操作并在public class FileWriter { public void appendData( String text) { try { File myFile = new File("/sdcard/latencyOP.csv"); // myFile.createNewFile(); FileOutputStream fOut = new FileOutputStream(myFile); OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut); if (myFile.createNewFile()) { myOutWriter.append("TimeStamp,ExecutionTime,OperationType;"); } myOutWriter.append(text); myOutWriter.close(); fOut.close(); /** * Toast.makeText(getBaseContext(), * "Done writing SD 'mysdfile.txt'", Toast.LENGTH_SHORT).show(); */ } catch (Exception e) { /** * Toast.makeText(getBaseContext(), e.getMessage(), * Toast.LENGTH_SHORT).show(); */ } } } 线程上发布结果而不必操纵线程。

更新代码 @Leonardo Acevedo

**FILE.csv**

ASYNCH任务流程

    try {
        File myFile = new File(
                Environment.getExternalStorageDirectory(),
                "latencyOP.csv"
        );

        boolean needToCreateFile = !myFile.exists();
        if (needToCreateFile) {
            myFile.createNewFile();
        }

        FileOutputStream fOut = new FileOutputStream(myFile);
        OutputStreamWriter myOutWriter =
                new OutputStreamWriter(fOut);

        // At this point your file is guaranteed to exist
        if (needToCreateFile) {
            myOutWriter.append("TimeStamp,ExecutionTime,OperationType;");
        }

        myOutWriter.append(text);
        myOutWriter.close();
        fOut.close();

        Toast.makeText(getBaseContext(),
                "Done writing SD 'mysdfile.txt'",
                Toast.LENGTH_SHORT
        ).show();


    } catch (Exception e) {
        e.printStackTrace();
        displayExceptionMessage(e.getMessage());
       //Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

}

public void displayExceptionMessage(String msg) {
    //Toast.makeText(this, "Some Error Occured", Toast.LENGTH_SHORT).show();
}

这是错的吗?

protected void onPreExecute(){ startTime = Calendar.getInstance().getTime().getTime();
    }

    protected void onPostExecute(Long result) {
        endTime = Calendar.getInstance().getTime().getTime();

        long timeDiff = endTime - startTime;
        FileWriter fileWriter = new FileWriter();
        String data = endTime + "," + timeDiff + "," + "Authentication" + ";";
        fileWriter.appendData(data);
}

0 个答案:

没有答案