Android Network File upload and download tool class Summary

Source: Internet
Author: User
Tags date1

Android Network File upload and download tool class Summary

1. Get the last modification time of the file

@ SuppressLint ("SimpleDateFormat") public String getFileDataTime (File file) {Date date Date = new Date (file. lastModified (); SimpleDateFormat sdformat = new SimpleDateFormat ("yyyy-MM-dd HH: mm: ss"); // String LgTime = sdformat in 24-hour format. format (date); return LgTime ;}

2. Compare the two time ranges
@SuppressLint("SimpleDateFormat")public int compareDataTime(String date1, String date2) {java.text.DateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");java.util.Calendar c1 = java.util.Calendar.getInstance();java.util.Calendar c2 = java.util.Calendar.getInstance();try {c1.setTime(df.parse(date1));c2.setTime(df.parse(date2));} catch (java.text.ParseException e) {System.err.println("error");}int result = c1.compareTo(c2);if (result == 0)System.out.println("c1==c2");else if (result < 0)System.out.println("c1
 
  c2");return result;}
 

3. Compare two string arrays to find the data of the second array and the first array.
public String[] compareStringPre(String[] Source, String[] Object) {String[] result;if (Source.length >= Object.length) {result = new String[Source.length];} else {result = new String[Object.length];}int n = 0;for (int i = 0; i < Object.length; i++) {boolean flag = false;for (int j = 0; j < Source.length; j++) {if (Object[i].equals(Source[j])) {flag = true;}}if (!flag) {result[n] = Object[i];n++;}}return result;}

4. Save the txt cache file
private boolean saveCrash(String crash) {String fileName = "cache.txt";try {File file = new File(mstrFilePath, fileName);FileOutputStream fos = new FileOutputStream(file);fos.write(crash.toString().getBytes());fos.close();} catch (Exception e) {return false;}return true;}

5. Read txt files to read cached files
Public String readCrashData () {String strDataLine = ""; String filePath = mstrFilePath + "/cache.txt"; File file = new File (filePath); if (file. exists () = false) return strDataLine; long fileSize = file. length (); // when the file is larger than 1 M, it is considered invalid data. Delete if (fileSize> = 1*1024*1024) {file. delete (); return strDataLine;} if (file. canRead () = false) return strDataLine; try {FileInputStream in = new FileInputStream (file); BufferedReader reader = new BufferedReader (new InputStreamReader (in); strDataLine = reader. readLine (); reader. close ();} catch (FileNotFoundException e) {e. printStackTrace (); return strDataLine;} catch (IOException e) {e. printStackTrace (); return strDataLine ;}

Cached files are stored on the SD card. Remember to add permissions to manifest ..

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.