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();
}
}
}
}
}
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();
}
}
}
}
}