根据路径获取文件的两种方式

/**
	 * 从类路径下获取资源
	 * 
	 * @param filePath
	 * @return
	 * @throws IOException
	 */
	public static String getFilesFromClassPath(String filePath)
			throws IOException {
		InputStream inStream = CpmapFileUtil.class
				.getResourceAsStream(filePath);
		BufferedReader reader = new BufferedReader(new InputStreamReader(
				inStream));
		StringBuffer result = new StringBuffer();
		String line = "";

		while ((line = reader.readLine()) != null) {
			result.append(line);
		}

		return reader.toString();
	}
	/**
	 * 从文件路径获取资源
	 * @param FilePath
	 * @return
	 */
	public static String getTextFromLocalFile(String FilePath) {

		File in = new File(FilePath);
		StringBuffer sb = new StringBuffer("");
		if (!in.exists())
			return "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(in));
			String readLine = "";
			while ((readLine = br.readLine()) != null) {
				sb.append(readLine);
			}
			br.close();
			in = null;
			return sb.toString();
		} catch (Exception e) {
			in = null;
			return "";
		}
	}

你可能感兴趣的:(根据路径获取文件的两种方式)