Open top menu






In this tutorial explain how to Create / Delete Multiple text file Inside Root Folder  and every text file Size is 5Mb .

private File createEVENTFileWriter() {

    File filelog = null;

    try {

        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/" + log_foldereventname);
        myDir.mkdirs();
        String h = DateFormat.format("MM-dd-yyyyy ss",System.currentTimeMillis()).toString();
        filelog = new File(myDir, log_filenameevent);
        filelog.createNewFile();
        long fileSizeInBytes = filelog.length();
        long fileSizeInKB = fileSizeInBytes / 1024;

        if (fileSizeInKB > 5000) {

            File from = new File(myDir, log_filenameevent);
            File to = new File(myDir, h + log_filenameevent);
            if (from.exists()) {
                from.renameTo(to);

            }
        }

    } catch (Exception err) {
        fileWriteCreationFailed = true;
        err.printStackTrace();
    }
    return filelog;

}
 

public void DeletOlderFolder() {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir =
new File(root + "/" + log_foldereventname);
    myDir.mkdirs();
    File list[] = myDir.listFiles();
   
for (int i = 0; i < list.length; i++) {
       
long diff = new Date().getTime() - list[i].getAbsoluteFile().lastModified();
       
if (diff >  3 * 24 * 60 * 60 * 1000) {//3 * 24 * 60 * 60 * 1000(3days)
           
list[i].getAbsoluteFile().delete();
        }
    }
Read more




Explain- The Log file delete alternate in  3 days.


import android.content.Context;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;


public class LogFile {

    Context context;

     public LogFile ( ) {

        DeletOlderFolder();
    }

    public void DeletOlderFolder(){

        File file=new File("sdcard/LoungeLogFile.txt");

        if(file != null) {

            long diff = new Date().getTime() - file.lastModified();

            if (diff > 3 * 24 * 60 * 60 * 1000) {

                file.delete();

            }

        }

    }

}
 
 

public class MainActivity extends Activity  {
 
@Override

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
 
 
new LogFile (); // Delete Log file in 3 days
 
 
}
}


Read more


Explain-  write Log file programmatically in android.


 
import android.content.Context;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;


public class LogClass {
    Context context;

    public LogClass(Context context, String text) {
       this.context=context;
        LogFile(text);
        DeletOlderFolder();
    }

    public  void LogFile( String text){

            File logFile = new File("sdcard/LoungeLogFile.txt");
            if (!logFile.exists()) {
                try {
                    logFile.createNewFile();
                }catch (IOException e){
                    e.printStackTrace();
                }
            }
            try {
                BufferedWriter buf = new BufferedWriter(

                                    new FileWriter(logFile, true));
                buf.append(text);
                buf.newLine();
                buf.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

    }
 }
Read more



How to solve SSL.SSLHandshakeException in Android?

Basically ,SSL usage Scenario, a server configurd  with a certificate containing a public key as well as a Matching private key.As part of handshake between an SLL client and server.

TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
     
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
         
return null;
      }
     
public void checkClientTrusted(X509Certificate[] certs, String authType) {
      }
     
public void checkServerTrusted(X509Certificate[] certs, String authType) {
      }
  } };
 
final SSLContext sc = SSLContext.getInstance("SSL");
  sc.init(
null, trustAllCerts, new java.security.SecureRandom());
  HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
 
// Create all-trusting host name verifier
 
HostnameVerifier allHostsValid = new HostnameVerifier() {
     
public boolean verify(String hostname, SSLSession session) {
         
return true;
      }
  };
 
// Install the all-trusting host verifier
 
HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
  URL url =
new URL(aurl[0]);
//  HttpsURLConnection connection = (HttpsURLConnection) (url.openConnection());
 
URLConnection connection = url.openConnection();
  connection.setRequestProperty(
"connection", "close");
  connection.connect();
  


Try this, 
Read more