Android讀取src下的檔案
1.從src目錄下,讀取文字檔案內容
public static String inputStreamToString(Class _class, String metadataFileName) throws IOException {
InputStream in = _class.getResourceAsStream(metadataFileName);
StringBuffer out = new StringBuffer();byte[] b = new byte[4096];
for (int n; (n = in.read(b)) != -1;) {
out.append(new String(b, 0, n));
}
return out.toString();
}
2.從src目錄下,讀取icon
public static byte[] getIconData(Class clazz, String file) {
try {
InputStream input = clazz.getResourceAsStream(file);
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
return output.toByteArray();
} catch (IOException err) {
err.printStackTrace();
}
return null;
}