2

我正在开发一个 Android 2.3.3 版应用程序,该应用程序依赖于 SMS 进行 SMS 交易。我写了两个类 SendSMS 和 ReceiveSMS。我想创建一个日志文件来记录它发送或接收的每条短信。为了创建日志文件,我编写了代码但不起作用。

以下是我的代码,

public void WriteOnLog()
{
    File exportDir =  Environment.getExternalStorageDirectory();

    if (!exportDir.exists())          {              
    exportDir.mkdirs();          
    }   
    String fileName;    

        fileName = "log" + ".txt";

    File file = new File(exportDir,fileName); 
    String txt=null ;
    try {
        if(!file.exists()){
            file.createNewFile();
            FileWriter Write = new FileWriter(file,true);
            out = new BufferedWriter(Write);
             txt = "Date Time |" + " Send/Receive |" + " Controller No |" +" msg";
        }
        FileWriter Write = new FileWriter(file,true); 
        out = new BufferedWriter(Write);

        String dd=null,mm=null,yy=null,hh=null,min=null,ss=null,dt=null;    
        SimpleDateFormat sdfDateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        String newtime =  sdfDateTime.format(new Date(System.currentTimeMillis()));             
        yy = newtime.substring(2, 4);
        mm = newtime.substring(5, 7);
        dd = newtime.substring(8, 10);
        hh = newtime.substring(11, 13);
        min = newtime.substring(14, 16);
        ss = newtime.substring(17); 
        dt = dd+"-"+mm+"-"+yy +" " + hh + ":" + min +":"+ ss;
        txt = dt + " |" +" Send " + "| " + phoneNumber + " | " + message.toUpperCase();
        out.write(txt + "\n");     


    }          
    catch(IOException sqlEx)   {              
    Log.e("MainActivity", sqlEx.getMessage(), sqlEx);              

}
}
4

1 回答 1

0

我使用了 OutputStreamWriter(而不是 FileWriter)方法。我让它在我的开发环境中工作并在设备(android 2.3.6)上进行了测试。试试这个:

try {
        File directory;
        if(isSDCard){
            directory = new File(Environment.getExternalStorageDirectory().toString()+"/"+folderNameOrPath+"/");
        }
        else{
            directory = new File(folderNameOrPath);
        }

        boolean fileReturnVal = directory.mkdirs();
        if(fileReturnVal){
            Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder Created successfully");
        }
        else{
            Log.d("Storage", "Write in SD Card: " + folderNameOrPath + " folder either already exists or creation failed");
        }

        File file = new File(directory, fileNameWithExtention);
        FileOutputStream fOut = new FileOutputStream(file);

        OutputStreamWriter osw = new OutputStreamWriter(fOut);

        //Write the string to the file
        osw.write(text);
        osw.flush();
        osw.close();

        //file saved
        Toast.makeText(context, "Text saved in SD Card", Toast.LENGTH_SHORT).show();
        return true;

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        Log.e("Storage", "Write in SD Card: File Not Found ERROR: " + e.toString());
        return false;
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Log.e("Storage", "Write in SD Card: IOException ERROR: " + e.toString());
        return false;
    } catch (Exception e) {
        Log.e("Storage", "Write in SD Card: ERROR: " + e.toString());
        return false;
    }

您可以为您的任务设置附加模式。变量是 SDCard、folderName、fileNameWithExtn。等是方法级别的变量。我将这些作为参数传递。

于 2012-05-29T06:29:24.990 回答