IO基础加强 输入输出流

------字节输出流   fileOutPutStream

public static void main(String[] args) throws IOException {

//1、创建目标对象,表示要把数据存入的地方
File target = new File("c:/abc/1.txt");
//2、创建字节流输出对象
// OutputStream fos = new FileOutputStream(target);
//创建一个可以追加字符的流,该流写入时不会删除之前的
OutputStream fos2 = new FileOutputStream(target,true);
//3、输出流操作
// 输出流的write方法
//void write(int b) 把一个字节写入文件(此处可以理解为byte因为byte小于int)
// fos.write(65);
//void  write(byte[] b) 将字节数组写进文件
// fos.write("abc".getBytes());
//void write(byte[] b, int off, int len) 把数组b中从off开始的len个字节
//从byte中第二个字符开始取2个字符
fos2.write("abc".getBytes(),1,2);
//4、关闭资源对象
fos2.close();
 

}

 

 

------字节输入流   fileInPutStream

 

//1、创建源对象,表示要读取的文件
File target = new File("c:/abc/1.txt");
//2、创建字节流输入对象
InputStream is = new FileInputStream(target);
//3、读取操作
//int read() 从文件中读取一个字节当没有数据时候会返回-1
int read = is.read();
//int read(byte[] b) 读取多个字节并存入数组中(如果此时数组的length大于文件字节总数,则数组值以0补充)

//比如使用new byte[5]   去读一个1.txt的文件   文件内容为ABC 则读出来为[65,66,67,0,0] 最后两位为空  补0

read(byte[])方法会返回int类型,这个数字代表读取到了多少个字节   我们获取数据时,要根据数组中的实际字节数来

 

//int read(byte[] b, int off, int len)读取多个字节并存入数组中
//指定从索引位置(off)开始存储(len)个字节
byte[] b = new byte[6];
int read = is.read(b,3,2);

System.out.println(Arrays.toString(b));

 

//循环读取所有的数据 根据 read(byte[]) 方法返回的读取字节数判断文件是否已经读取完毕

byte[] b = new byte[5];
int i = -1;
while((i=is.read(b))!=-1){
System.out.println(new String(b,0,i));
}

//4、关闭资源
is.close();

}

 

------利用流来做一次文件拷贝  

public static void main(String[] args) throws IOException {
//1、创建源对象目标对象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
//2、创建字节流输入对象
InputStream is = new FileInputStream(sourse);
OutputStream os = new FileOutputStream(target);
//3、读取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示读取出文件的长度
while((lenth=is.read(b))!=-1){

                    //这里一定要制定长度,否则会有 
os.write(b, 0, lenth);
}
//4、关闭资源
is.close();
os.close();
}               

 

 

 

------流的异常处理方法

public static void main(String[] args){
InputStream is = null;
OutputStream os = null;
try {
//1、创建源对象目标对象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
//2、创建字节流输入对象
is = new FileInputStream(sourse);
os = new FileOutputStream(target);
//3、读取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示读取出文件的长度
while((lenth=is.read(b))!=-1){
os.write(b);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//4、关闭资源(此时又需要处理异常)
try {
//如果流对象为null 又会报空指针
if(is != null){
   is.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}
//为防止close方法报错而使得os没有关闭,所以理论上所有的流都应该单独写关闭方法
try {
//如果流对象为null 又会报空指针
if(os != null){
   os.close();
}
} catch (Exception e2) {
e2.printStackTrace();
}

}

}

 

 

自从java7开始提供了自动关闭资源的方法,比较方便实用上面一坨可以改为如下写法,实际上就是把try上面加了一对( )   ,在括号里面存放需要关闭的资源  ,jvm会自动帮你关闭,这样看起来代码是不是简洁多了呢?

 

public static void main(String[] args){
//1、创建源对象目标对象
File sourse = new File("c:/abc/w.wmv");
File target = new File("c:/abc/xxx.wmv");
try (
//2、创建字节流输入对象
InputStream is = new FileInputStream(sourse);
OutputStream os = new FileOutputStream(target);
){
//3、读取操作
byte[] b = new byte[10];
int lenth = -1 ; //表示读取出文件的长度
while((lenth=is.read(b))!=-1){
os.write(b);
}
} catch (Exception e) {
e.printStackTrace();
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章