首页 > 其他 > 详细

保证IO流不出错

时间:2018-11-15 12:37:14      阅读:161      评论:0      收藏:0      [点我收藏+]
package com.io.demo1;

import java.io.FileInputStream;
import java.io.IOException;

/**
* 测试IO
* io流,输入流,输出流
*/

public class demo_one {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("d:/a.txt"); // 内容是:abc
StringBuilder sb = new StringBuilder();
int temp = 0;
//当temp等于-1时,表示已经到了文件结尾,停止读取
while ((temp = fis.read()) != -1) {
sb.append((char) temp);
}
System.out.println(sb);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
//这种写法,保证了即使遇到异常情况,也会关闭流对象。
if (fis != null) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

保证IO流不出错

原文:https://www.cnblogs.com/leigepython/p/9962653.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!