IO基础加强 day03

------字节输出流   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) 读取多个字节并存入数组中(注意此时返回值有总字节数,超过该总数量(索引)之后的byte数组的值为0)
byte[] b=new byte[10];
System.out.println(Arrays.toString(b));
int read2 = is.read(b);
for (int i=0;i<read2;i++) {
System.out.println(b[i]);
}
//当该数组已经读完了文件的数据之后再调用此时数组中全部都是0
byte[] c=new byte[10];
is.read(c);
System.out.println(Arrays.toString(c));
//将byte数组构建成String
String str = new String(b,0,read2);

System.out.println("------------");
System.out.println("------------");

System.out.println("------------");


                //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));


                //循环读取所有的数据
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();
}
}

猜你喜欢

转载自blog.csdn.net/qq_39205291/article/details/80558894
今日推荐