根據路徑獲取文件的兩種方式

/**
	 * 從類路徑下獲取資源
	 * 
	 * @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 "";
		}
	}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章