####1、实现功能
用字节的 方式读取文件 并且将
其转换成String显示:
####2、功能实现
public void getContentFromFile() {
final String FILE_NAME = "/data/test.txt";
File file = new File(FILE_NAME);
byte[] bufferFile = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream((int) file.length());
try {
Log.v(TAG, "try open /data/test.txt");
FileInputStream fis = new FileInputStream(file);
if (fis != null) {
Log.v(TAG, "read /data/test.txt");
byte[] bytes = new byte[(int) file.length()];
int num = 0;
while ((num = fis.read(bytes)) != -1) {
bos.write(bytes, 0, (int) file.length());
//将字节获得到字节数组
bufferFile = bos.toByteArray();
Log.v(TAG, "lum_1");
System.out.println(Arrays.toString(bufferFile));
//将字节数组转换成字符串显示
String str = new String(bufferFile,"ascii");
System.out.println(str);
}
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}