android write file to local

    // Generate log print log log TxT dedicated
         private void printLogMsg(JSONObject jsonObj) {
             
                 String filePath = Environment.getExternalStorageDirectory().getPath() + "/AirportCipPos/log/";
                 
                 String fileName = System.currentTimeMillis() + ".txt";
                 
                 writeTxtToFile(Common.testURl  + "\r\n\r\n"
                 +
                 sign +"\r\n\r\n"
                 +
                 Common.getMD5(sign) + "\r\n\r\n"
                 +
                 jsonObj + "\r\n\r\n"
                         , filePath, fileName);
             
             
         }
         // write string to text file
         public void writeTxtToFile(String strcontent, String filePath, String fileName) {
             //After generating the folder, generate the file again, otherwise there will be an error
             makeFilePath(filePath, fileName);
              
             String strFilePath = filePath+fileName;
             // Every time you write, write a newline
             String strContent = strcontent + "\r\n";
             try {
                 File file = new File(strFilePath);
                 if (!file.exists()) {
                     Log.d("TestFile", "Create the file:" + strFilePath);
                     file.getParentFile().mkdirs();
                     file.createNewFile();
                 }
                 RandomAccessFile raf = new RandomAccessFile(file, "rwd");
                 raf.seek(file.length());
                 raf.write(strContent.getBytes());
                 raf.close();
             } catch (Exception e) {
                 Log.e("TestFile", "Error on write File:" + e);
             }
         }
         
         // generate file
         public File makeFilePath(String filePath, String fileName) {
             File file = null;
             makeRootDirectory (filePath);
             try {
                 file = new File(filePath + fileName);
                 if (!file.exists()) {
                     file.createNewFile();
                 }
             } catch (Exception e) {
                 e.printStackTrace ();
             }
             return file;
         }
         
         // generate folder
         public static void makeRootDirectory(String filePath) {
             File file = null;
             try {
                 file = new File(filePath);
                 if (!file.exists()) {
                     file.mkdir();
                 }
             } catch (Exception e) {
                 Log.i("error:", e+"");
             }
         }
    

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325851069&siteId=291194637