直接上样例参考(附有部分说明):
File fileName = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test/test.levp");
FileOutputStream fileOutputStream=null;
DataOutputStream dataOutputStream=null;
try {
fileOutputStream = new FileOutputStream(fileName);
dataOutputStream = new DataOutputStream(fileOutputStream);
dataOutputStream.writeUTF("第1句话。");
dataOutputStream.writeUTF("第2句话。");
dataOutputStream.writeUTF("第3句话。");
dataOutputStream.writeUTF("第4句话。");
…………………………………………………………………………
dataOutputStream.flush();
fileOutputStream.close();
dataOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
fileOutputStream.close();
dataOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
StringBuilder content = new StringBuilder();
FileInputStream fileInputStream = null;
DataInputStream dataInputStream = null;
try {
fileInputStream = new FileInputStream(fileName);
dataInputStream = new DataInputStream(fileInputStream);
while (true) {
content.append(dataInputStream.readUTF()+ "\n");//by throwing java.io.EOFException to get the Reading result(目前,ReadReadUTFT还没有现判断文件读完的方法,可通过文件读到底发生异常来处理相关数据或目的)
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();//探测发生文件读完的异常后,程序跳转至此处进行处理(也可以在处设置相关目标代码),最后跳转finally处
} finally {
tv_Sample.setText(content);//实现如在设置的TextView中显示等目的
try {
fileInputStream.close();
dataInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}