欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

android 网络文件上传下载工具类总结

来源: 开发者 投稿于  被查看 21523 次 评论:218

android 网络文件上传下载工具类总结


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("c1c2");

		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加权限。。

用户评论