javaIO流(2):文件字节输入流
2015-09-13 20:32
417 查看
一:输入流步骤
1.设定输入流的源
2.创建指向源的输入流
3.让输入流读取源中的数据
4.关闭输入流
二:具体用法
1.构造方法
FileInputStream(File file);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
参数name和file指定的文件称为输入流的源
FileInputStream输入流打开一个到达文件的通道(源是指该文件,输入流指向这个文件),输入流出现的异常是IOException异常
2.案例:读取名为hello.txt的文件
方法一:
try{
FileInputStream in=new FileInputStream(“hello.txt”);//创建指向文件的流
}
catch(IOException e){
System.out.println(“File read error:”+e);
}
方法二:
File f=new File(“hello.txt”);//创建输入流的源
try{
FileInputStream in=new FileInputStream(f);//创建指向源的输入流
}
catch(IOException e){
System.out.println(“File read error:”+e);
}
3.使用输入流读取字节
文件字节流可以调用从父类继承的read方法顺序的读取文件,只要不关闭流,每次调用read方法就顺序的读取文件中的其余内容,直到文件的末尾或者文件的字节流被关闭。
int read()输入流调用该方法从源中读取单个字节的数据,该方法返回字节值(0~225之间的一个整数),如果未读出字节就返回—1
int read(byte b[]) 输入流调用该方法从源中试图读取b.length个字节到数组b中,返回实际读取的字节数,如果达到文件的末尾返回-1.
int read(byte b[],int off,int len)输入流调用该方法,从源中试图读取len个字节到数组b中,并返回实际读取的字节数目,如果达到文件末尾,则返回-1,参数off指定从字节数组的某个位置开始存放读取的数据。
FileInputStream流顺序的读取文件,只要不关闭流,每次调用read方法就顺序取源中的其余内容。
4.关闭流
输入流提供关闭方法close();
三:案例
[java] view
plaincopyprint?
<span style="font-size:14px;">public static void main(String[] args) {
// TODO Auto-generated method stub
int n=-1;
byte a[]=new byte[100];
try {
File f=new File("Demo2.txt");
InputStream in=new FileInputStream(f);
while((n=in.read(a, 0, 100))!=-1){
String s=new String(a,0,n);
System.out.println(s);
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</span>
四:案例二
[java] view
plaincopyprint?
<span style="font-size:14px;">public class Demo3 {
public static void main(String[] args) {
// 1.获取文件对象
File file=new File("D:\\IO.txt");
InputStream is=null;
try {
//2.建立输入流
is=new FileInputStream(file);
byte [] buf=new byte[1024];
//3.读取文件内容,放到read(byte b[])放到数组中
is.read(buf);
System.out.println(new String(buf));
} catch (Exception e) {
e.printStackTrace();
}finally{
//4.关闭流对象
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}</span>
上面代码存在缺点,受到byte数组开辟空间大小的限制,开辟动态数组空间,根据文件大小决定,使用read(),一个字节一个字节读取。
[java] view
plaincopyprint?
<span style="font-size:14px;"><span style="white-space:pre"> </span>//1.获取文件对象
File file=new File("D:\\IO.txt");
InputStream is=null;
try {
//2.建立输入流
is=new FileInputStream(file);
//固定字节数组长度
//byte [] buf=new byte[1024];
//3.读取文件内容
//is.read(buf);
//动态数组
//byte [] buf=new byte[(int)file.length()];
//is.read(buf);
byte [] buf=new byte[1024];
int len=0;
StringBuffer buffer=new StringBuffer();
while((len=is.read(buf))!=-1){
String str=new String(buf);
buffer.append(str);
}
System.out.println(new String(buf));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
//4.关闭流对象
if(is!=null)
{
try
{
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}</span>
1.设定输入流的源
2.创建指向源的输入流
3.让输入流读取源中的数据
4.关闭输入流
二:具体用法
1.构造方法
FileInputStream(File file);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name);通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
参数name和file指定的文件称为输入流的源
FileInputStream输入流打开一个到达文件的通道(源是指该文件,输入流指向这个文件),输入流出现的异常是IOException异常
2.案例:读取名为hello.txt的文件
方法一:
try{
FileInputStream in=new FileInputStream(“hello.txt”);//创建指向文件的流
}
catch(IOException e){
System.out.println(“File read error:”+e);
}
方法二:
File f=new File(“hello.txt”);//创建输入流的源
try{
FileInputStream in=new FileInputStream(f);//创建指向源的输入流
}
catch(IOException e){
System.out.println(“File read error:”+e);
}
3.使用输入流读取字节
文件字节流可以调用从父类继承的read方法顺序的读取文件,只要不关闭流,每次调用read方法就顺序的读取文件中的其余内容,直到文件的末尾或者文件的字节流被关闭。
int read()输入流调用该方法从源中读取单个字节的数据,该方法返回字节值(0~225之间的一个整数),如果未读出字节就返回—1
int read(byte b[]) 输入流调用该方法从源中试图读取b.length个字节到数组b中,返回实际读取的字节数,如果达到文件的末尾返回-1.
int read(byte b[],int off,int len)输入流调用该方法,从源中试图读取len个字节到数组b中,并返回实际读取的字节数目,如果达到文件末尾,则返回-1,参数off指定从字节数组的某个位置开始存放读取的数据。
FileInputStream流顺序的读取文件,只要不关闭流,每次调用read方法就顺序取源中的其余内容。
4.关闭流
输入流提供关闭方法close();
三:案例
[java] view
plaincopyprint?
<span style="font-size:14px;">public static void main(String[] args) {
// TODO Auto-generated method stub
int n=-1;
byte a[]=new byte[100];
try {
File f=new File("Demo2.txt");
InputStream in=new FileInputStream(f);
while((n=in.read(a, 0, 100))!=-1){
String s=new String(a,0,n);
System.out.println(s);
}
in.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}</span>
四:案例二
[java] view
plaincopyprint?
<span style="font-size:14px;">public class Demo3 {
public static void main(String[] args) {
// 1.获取文件对象
File file=new File("D:\\IO.txt");
InputStream is=null;
try {
//2.建立输入流
is=new FileInputStream(file);
byte [] buf=new byte[1024];
//3.读取文件内容,放到read(byte b[])放到数组中
is.read(buf);
System.out.println(new String(buf));
} catch (Exception e) {
e.printStackTrace();
}finally{
//4.关闭流对象
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}</span>
上面代码存在缺点,受到byte数组开辟空间大小的限制,开辟动态数组空间,根据文件大小决定,使用read(),一个字节一个字节读取。
[java] view
plaincopyprint?
<span style="font-size:14px;"><span style="white-space:pre"> </span>//1.获取文件对象
File file=new File("D:\\IO.txt");
InputStream is=null;
try {
//2.建立输入流
is=new FileInputStream(file);
//固定字节数组长度
//byte [] buf=new byte[1024];
//3.读取文件内容
//is.read(buf);
//动态数组
//byte [] buf=new byte[(int)file.length()];
//is.read(buf);
byte [] buf=new byte[1024];
int len=0;
StringBuffer buffer=new StringBuffer();
while((len=is.read(buf))!=-1){
String str=new String(buf);
buffer.append(str);
}
System.out.println(new String(buf));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
//4.关闭流对象
if(is!=null)
{
try
{
is.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
}
}</span>
相关文章推荐
- java中使用字节流的方式复制文件
- 拦截器的应用
- java集合TreeSet的两种排序方式
- Struts2自定义类型转换器分为局部类型转换器和全局类型转换器
- javaSwing组件连接SQLite数据库
- 用javaEE做连接数据库的操作时,提示找不到数据库驱动相应的类
- Java基础(二)
- 如何设置Java虚拟机内存以适应大程序的装载
- Java初体验
- Java运行环境绿色部署配置
- java中的Random类
- 《Google Java编程风格指南》代码注释与编码规范~总结
- eclipse里面调试maven插件
- Java 加载配置文件的方式
- eclipse配置maven
- java中使用sort()对ArrayList进行排序
- java.rmi.ConnectException: Connection refused to host: 127.0.0.1
- JavaNIO:非阻塞NIO通信及相关对象讲解
- 【MyEclipse】MyEclipse使用总结——MyEclipse10安装SVN插件
- Strurts2下导入Excel文件数据