14InputStream

package test.io;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
 * [说明/描述]
 * 
 * @author CaiHaiming
 * @date 2016-7-24 下午5:08:13
 * @company chm
 * @version 1.0
 * @copyright copyright (c) 2016
 */
public class FileInputStreamDemo1 {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("file1.txt");
// int n = 0;
// while ((n = fis.read()) != -1) {
// System.out.print((char) n);
// }
// byte[] b = new byte[10];// 1024*1字节 = 1K大小
// 哈哈如果这个定义大小内,刚好包含一个字符那么会将中文输出,但是刚好分开那就比较蛋疼。
// 好吧不建议字节流处理文本
// int n = 0;
// while ((n = fis.read(b)) != -1) {
// System.out.print(new String(b, 0, n));
// }
int num = fis.available();// winodws中回车 回读去\r\n2个字节
// 这种方式慎用,因为很容易造成创建数组时内存溢出
// 对于小的数据量可以,但慎用
byte[] bbuf = new byte[num];
fis.read(bbuf);
System.out.println(new String(bbuf));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值