android 網路檔案上傳下載工具類總結

來源:互聯網
上載者:User

標籤:

1、擷取檔案的最後修改時間

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

2、比較兩個時間的大小

@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、兩個string數組比較,找出第二個數組與第一個數組不同的資料

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、儲存txt快取檔案

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、讀取txt讀取快取檔案

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();// 檔案大於1M, 認為是無效資料, 直接刪除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;}return strDataLine;}

快取檔案儲存在sd卡,記得manifest加許可權。。

在上一篇中講了android 檔案android 使用AsyncHttpClient架構上傳檔案以及使用HttpURLConnection下載檔案

android 網路檔案上傳下載工具類總結

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.