7

1.I am Saving the Android GPS enable and disable Time using a Text Log file inside SD card.

I want to Print multiple lines text, without erasing Previous Line data.

Below code is Printing Multiple lines Perfectly But When App is Kept in Sleep and Restarting . Printing a row along with Previous data Count, when GPS ON or OFF.

Help me how to fix it.

Required Output:(after Restart the app)

<< GPs Enabled At :2016/06/08 17.15.00>>
<< GPs Disabled At :2016/06/08 17.45.00>>
<< GPs Enabled At :2016/06/08 17.49.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>

Getting Output:(after Restart the app)

<< GPs Enabled At :2016/06/08 17.15.00>>
<< GPs Disabled At :2016/06/08 17.45.00>>
<< GPs Enabled At :2016/06/08 17.49.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>
<< GPs Disabled At :2016/06/08 17.52.00>>

method For Log File:

 public void Logmethod(String NoramalStr,String Dateval)
    {

    /* Log FOlder Text files Starts here*/
            try {

                File newFolder = new File(Environment.getExternalStorageDirectory(), "GpsFolder");
                if (!newFolder.exists()) {
                    newFolder.mkdir();
                }
                try {
                    File file = new File(newFolder, "GPSStatus" + ".txt");
                    file.createNewFile();

                 /*Writing the Log in specific files*/
                    BufferedWriter out;
                    try {


                        String separator1 = System.getProperty("line.separator");
                        FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPSStatus.txt"), true);
                        OutputStreamWriter osw = new OutputStreamWriter(fOut);
                        osw.append("<< " + NoramalStr +" : "+Dateval + " >>");
                        osw.append(separator1); // this will add new line ;
                        osw.flush();
                        osw.close();
                        fOut.close();

                        Log.i(NoramalStr," : "+Dateval);

                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }


                } catch (Exception ex) {
                    System.out.println("ex: " + ex);
                }
            } catch (Exception e) {
                System.out.println("e: " + e);
            }


        }
Kumar
  • 969
  • 2
  • 22
  • 56
  • 2
    I'm a little unclear about the question. You mention the word "erase" several times, but it appears that the difference between your required output and your actual output is that you are printing duplicated lines. Is the problem with duplications, or with erasures ? – OYRM Feb 08 '16 at 21:25
  • It looks like the Logmethod being called more than one time. Can you add the code that calls that method? – TDG Feb 11 '16 at 20:07
  • @Kumar: you are trying to print logs when app is not running or restart device? – Dhaval Parmar Feb 12 '16 at 03:56

2 Answers2

0

If the question about erasures of log file: you always do createNewFile try to check for file exists

public void Logmethod(String NoramalStr, String Dateval) {

    try {

        File newFolder = new File(Environment.getExternalStorageDirectory(), "GpsFolder");
        boolean directoryExists = newFolder.exists();

        Log.d(TAG, " directoryExists = " + directoryExists);

        if (!directoryExists) {
            boolean dirCreate = newFolder.mkdir();
            Log.d(TAG, " dirCreate = " + dirCreate);
        }
        File file = new File(newFolder, "GPSStatus" + ".txt");

        boolean fileExists = file.exists();
        Log.d(TAG, " fileExists = " + fileExists);

        if (!fileExists) {
            boolean fileCreateResult = file.createNewFile();
            Log.d(TAG, " fileCreateResult = " + fileCreateResult);
        }

        String separator1 = System.getProperty("line.separator");
        FileOutputStream fOut = new FileOutputStream(new File(newFolder + "/GPSStatus.txt"), true);
        OutputStreamWriter osw = new OutputStreamWriter(fOut);
        osw.append("<< ");
        osw.append(NoramalStr);
        osw.append(" : ");
        osw.append(Dateval);
        osw.append(" >>");
        osw.append(separator1); // this will add new line ;
        osw.flush();
        osw.close();
        fOut.close();

        Log.i(TAG, " : " + Dateval);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

If it is about permanent listening of location providers enable/disable look at How do I receive the system broadcast when GPS status has changed? and use receiver in the manifest file

    <receiver android:name=".GPStStatusReceiver">
        <intent-filter>
            <action android:name="android.location.PROVIDERS_CHANGED" />
        </intent-filter>
    </receiver>
Community
  • 1
  • 1
AndreyICE
  • 3,574
  • 29
  • 27
0
   try {
          File file = new File(newFolder, "GPSStatus" + ".txt");
               if (!file.exists()) {
            file.createNewFile();
        }

and then

      FileWriter writer = new FileWriter(file,true);
        writer.append(response);// you want to expand
        writer.flush();
        writer.close();
        Log.e("Suceess", "'file Write");

This help you

Vinod Ranga
  • 511
  • 8
  • 15