Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8001589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:07:55+00:00 2026-06-04T16:07:55+00:00

I am working on an Android version 2.3.3 application which depends on SMS for

  • 0

I am working on an Android version 2.3.3 application which depends on SMS for SMS dealing. I have written two classes SendSMS and ReceiveSMS. I want to create a log file which logs each SMS that it is sent or received. For creating log file I have written code but does not work.

Following is my code,

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);              

}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:07:57+00:00Added an answer on June 4, 2026 at 4:07 pm

    I have used the OutputStreamWriter (instead of FileWriter) Approach. I have it working in my dev environment and tested on device (android 2.3.6). try this out:

    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;
        }
    

    you can set append mode for you task. The variables isSDCard, folderName, fileNameWithExtn. etc. are method level variables. I am passing those as parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an application which uses SQLite Database on Android.I have a custom
Hi Ive written a simple android application with two xml pages, one has an
I am working on designing an Android application, and I have several questions I
I have some code which is working in iphone and i want to make
I'm working on an Android application. I have an activity that has this method:
I'm working on the Android version of an application I first created on iPhone
I am working on application which should go into android 2.2(Froyo) and android 2.3(GingerBread)
Our team is working on Android Application back ended with App Engine. We have
I have an application building against Android 2.1 and I want to override the
I am working in android. I designed a video player in my application. Most

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.