首页 > 技术文章 > Java编码字符串,解码字符串,取得文件大小,读文件内容

chenchaochao 2016-05-25 15:51 原文

1.Java编码字符串

public static String encode(String s, String encodeType) {
		if (s == null || s.equals("")) {
			return "";
		}
		if (encodeType == null || encodeType.equals("")) {
			return s;
		}
		try {
			return URLEncoder.encode(s, encodeType);
		} catch (Exception e) {
		}
		return s;
	}

2.Java解码字符串

public static String decode(String s, String encodeType) {
        if (s == null || s.equals("")) {
            return "";
        }
        if (encodeType == null || encodeType.equals("")) {
            return s;
        }
        try {
            s = URLDecoder.decode(s, encodeType);
        } catch (Exception e) {
        }
        return s;
    }

3.Java取得文件大小

public static long getFileSize(File f) throws Exception {
        long s = 0;
        if (f.isDirectory()) {
            for (File file : f.listFiles()) {
                s += getFileSize(file);
            }
        } else {
            FileInputStream fis = null;
            try {
                fis = new FileInputStream(f);
                s = fis.available();
            } catch (IOException e) {
            } finally {
                if (fis != null) {
                    fis.close();
                }
            }
        }
        return s;
    }

 

4.Java读文件内容

public static String readFile(String path, String encoding) {
File file = new File(path);
return readFile(file, encoding);
}

 

推荐阅读