Android Network File upload download Tool class summary

Source: Internet
Author: User

1. Get the last modified time of the file

@SuppressLint ("SimpleDateFormat") public String getfiledatatime (file file) {Date date = new Date (file.lastmodified ()); SimpleDateFormat Sdformat = new SimpleDateFormat ("Yyyy-mm-dd HH:mm:ss");//24-hour string lgtime = Sdformat.format (date); return lgtime;}

2, compare the size of two time

@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 "); ElseSystem.out.println (" c1>c2 "); return result;}

3, two string array comparison, find the second array and the first array of different data

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 txt cache file

Private Boolean Savecrash (string crash) {string fileName = "Cache.txt"; try {File file = new file (Mstrfilepath, fileName); F Ileoutputstream fos = new FileOutputStream (file), Fos.write (Crash.tostring (). GetBytes ()); Fos.close (); catch (Exception e) {return false;} return true;}

5. Read the TXT read cache file

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 ();//files greater than 1M, considered to be none (fileSize >= 1 * 1024x768) {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;} return strdataline;}

cache files stored in SD card, remember manifest plus permissions.

In the previous article said Android file Android using asynchttpclient framework to upload files and use httpurlconnection to download files

Android Network File upload download Tool class summary

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.