600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > IO流详解之OutputStream 输出流 InputStream输入流

IO流详解之OutputStream 输出流 InputStream输入流

时间:2022-12-27 19:22:54

相关推荐

IO流详解之OutputStream 输出流 InputStream输入流

1.简单案例

public class AA{//OutputStream 将我们定义好的数据放置到我们的目标文件中private static void testMethod1(String path) throws IOException{//1.打开文件输出流,流的目的地是指定的文件FileOutputStream fos=new FileOutputStream(path, true);//2.通过流文件写数据byte[] byt="java".getBytes();fos.write(byt);//3.关闭流文件fos.close();}public static void main(String[] args) throws IOException {try {testMethod1("C:\\caicai2.txt");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

2.将文件进行拷贝

import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;public class CC {//拷贝文件的一般写法,需要添加异常捕获并关闭IO流的操作//方法作用:将源文件的数据拷贝目标文件夹的位置public static void copyMethod(String srcPath,String destPath) throws IOException{//打开输入流,输出流 源文件srcPath ---> 内存 ----》目标文件destPathFileInputStream fis = new FileInputStream(srcPath);FileOutputStream fos = new FileOutputStream(destPath);//读取和写入信息int len=0;//使用字节数组,当做缓冲区byte[] byt=new byte[1024];while( (len=fis.read(byt)) != -1 ) {fos.write(byt,0,len);}//关闭流fis.close();fos.close();}public static void main(String[] args) {try {//目标文件caicai2.txt若是存在且里面有内容,执行后会被caicai.txt文件完全覆盖//目标文件caicai2.txt若是不存在,则会新建caicai2.txt文件并赋值caicai.txt内容copyMethod("C:\\caicai.txt","C:\\caicai2.txt");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}

3.缓冲流

1. BufferedInputStream 和 BufferedOutputStream类 可以通过减少读写次数来提高输入和输出的速度。

2. 缓冲区输入流和缓冲区输出流要配合使用。缓冲区输入流会将读取到的数据的读入缓冲区,当缓冲区满时,或者调用flush方法,缓冲输出流会将数据写出。

3. 需要传输的数据越大,越能够看到缓冲流带来的效率提升。

案例一:借助缓冲流拷贝传输视频/音频

public static void main(String[] args) {try {//copyMethod("C:\\有旁白语音.mp4","C:\\有旁白语音2.mp4");Method("C:\\有旁白语音.mp4","C:\\有旁白语音2.mp4");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public static void Method(String srcPath,String destPath) throws IOException{//打开输入流,输出流 注意:参数是对应的路径FileInputStream fis=new FileInputStream(srcPath);FileOutputStream fos=new FileOutputStream(destPath);//打开缓冲流,注意:参数是对应的streamBufferedInputStream bis = new BufferedInputStream(fis);BufferedOutputStream bos = new BufferedOutputStream(fos);//读取和写入信息int len=0;//直接在缓冲流之间进行交互while((len=bis.read())!=-1) {bos.write(len);}//关闭流bis.close();bos.close();fis.close();fos.close();}

运行结果

4.无需关闭IO流的操作

格式:

try ( 创建IO流 ) {

执行语句

} catch ( 异常类 异常别名 ){

如果发生异常,执行这段代码

}

前提是这些可关闭的资源必须实现java.lang.AutoCloseable接口。此时就不用在finally中进行资源的释放了。

//拷贝文件的简易写法,无需关闭IO流的操作public static void testMethod2(String srcPath,String descPath){try( FileInputStream fis=new FileInputStream(srcPath);FileOutputStream fos=new FileOutputStream(descPath)){byte[] byt=new byte[1024*1024];int len=0;while ((len=fis.read())!=-1) {fos.write(byt);}}catch (IOException e) {throw new RuntimeException(e);}}

try (SqlSession session = sqlSessionFactory.openSession()) {/*业务代码*/}catch(Exception e){e.printStackTrace();}

5.字符流

1.字符流是建立在字节流之上的,它能够提字符层次的编码和解码。

2.字符流更适用于操作文字数据。(txt等我们能够看懂的数据,音乐,视频就不属于数据)

3.字符流可以进行处理的操作,字节流几乎都可以处理。

4.和字节流的方法相似,字符流也主要是通过read()和write()去操作数据

5.Reader可以通过自身的read()方法将目标处的数据读取出来。

6.和Reader相反,Writer是通过write()方法将指定的数据写入目标处。

public static void testMethod3(String path1,String path2)throws IOException{//打开流Reader reader = new FileReader(path1);Writer write = new FileWriter(path2);// 用数组的话,应该用for()读取打印字符,下面注释的是错误代码/*char[] arr=new char[1024]; while ( (ch=reader.read(arr))!=-1) {write.write(ch);}*///进行拷贝int ch=0;while((ch=reader.read())!=-1) {write.write((char)ch);}reader.close();write.close();}public static void main(String[] args) {try {testMethod3("C:\\aa.txt","C:\\cc.txt");} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}

6.字符输入流和输出流

public static void testMethod5(String path1,String path2) throws IOException{FileReader fr=new FileReader(path1);FileWriter fw=new FileWriter(path2);//字符输入流的缓冲流BufferedReader br = new BufferedReader(fr);BufferedWriter bw = new BufferedWriter(fw);String line=null;while((line = br.readLine())!=null) {//一次写一行bw.write(line);//刷新缓冲//flush()操作可以将缓冲区中的数据推出来,以免造成数据丢失的情况。bw.flush();//进行 换行bw.newLine();}//关闭流br.close();bw.close();fr.close();fw.close();}public static void main(String[] args) {try {testMethod5("C:\\aa.txt","C:\\cc.txt");} catch (IOException e) {e.printStackTrace();}}

7.使用IO流需要注意的点:

1.使用完成后,及时关闭对应的IO流

2.IO流常常伴随着try-catch或Throws等关键字一起使用。

3.IO流是否会出现阻塞的情况?会

4.有哪些方法可以提升IO流性能?数组(new byte[1024]),缓冲流(BufferedReader,BufferedWriter,BufferedInputStream,BufferedOutputStream)

5.Reader/Writer,InputStream/OutputStream没有直接可以实例化的对象。因为都是abstract修饰的抽象类。

6.flush()操作可以将缓冲区中的数据推出来,以免造成数据丢失的情况。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。