码迷,mamicode.com
首页 > 其他好文 > 详细

保证IO流不出错

时间:2018-11-15 13:46:48      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:new   ati   文件   str   abc   builder   har   temp   imp   

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流不出错

标签:new   ati   文件   str   abc   builder   har   temp   imp   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!