asset中獲取文件並讀取數據

// 方法:從asset中獲取文件並讀取數據
	public String getFromAsset(String fileName) {
		String result = "";
		try {
			InputStream in = getResources().getAssets().open(fileName); // 從Assets中的文件獲取輸入流
			int length = in.available(); // 獲取文件的字節數
			byte[] buffer = new byte[length]; // 創建byte數組
			in.read(buffer); // 將文件中的數據讀取到byte數組中
			result = EncodingUtils.getString(buffer, HttpRequest.ENCODE_UTF8); // 將byte數組轉換成指定格式的字符串
		} catch (IOException e) {
			e.printStackTrace(); // 捕獲異常並打印
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章