阻塞式(IO)
发布日期:2022-02-14 16:09:30
浏览次数:22
分类:技术文章
本文共 8586 字,大约阅读时间需要 28 分钟。
输入输出(I/O)
概念:
- 入和出是相对与内存而言的
- 输入:把数据读到内存称为输入(即input),进行数据的read操作;
- 输出:把数据从内存往外部设备写数据,称为输出(即output),进行数据的write操作;
File类
概念:
- File类是java.io包中的一个类;
- 可表示文件,还可表示目录;
- File对象可对文件或目录的属性进行操作;
File对象无法操作文件的具体数据;
构造方法File(String pathname):指定文件(或目录)名和路径创建文件对象;
//在当前目录下创建一个与aaa.txt文件名相关联的文件对象File f1 = new File("aaa.txt");//指明详细的路径以及文件名,请注意双斜线或用反斜杠File f2 = new File("D:\\Java\\Hello.java");//指明详细的路径以及目录名,请注意双斜线File f3 = new File("D:\\Java");
常用方法
- boolean exists():判断文件是否存在(存在true,否则false);
- boolean isFile():判断是否为文件(文件true,否则false);
- boolean isDirectory():判断是否为目录(目录true,否则为false);
- String getName():获得文件名称;
- String getAbsolutePath():获得文件的绝对路径;
- long length():获得文件的长度;
- boolean createNewFile():创建新文件成功返回true,否则返回false,可能抛出IOException异常必须catch;
- boolean delete():删除文件,删除成功返回true,否则返回false;
public class TestFile { public static void main(String[] args) { //创建File对象 File file = new File("notes.txt"); File dir = new File("E:\\project"); System.out.println("file是否是文件:" + file.isFile()); System.out.println("dir是否是目录:" + dir.isDirectory()); System.out.println("file是否可读:" + file.canRead()); System.out.println("file是否存在:" + file.exists()); }}
- String[] list():将目录下的子目录及文件的名字得到String数组;
- File[] listFiles():将目录下的子目录及文件的实例返回到File数组中
//打印该目录(或文件)所有子目录(存在)及其文件File files = new File("D:/Sublime");if(files.isDirectory()){ File[] filArr = files.listFiles(); for(File f : filArr){ if(f.isDirectory()){ System.out.println(f); iterratorFile(f); } System.out.println(f); }}
字节 字符来分
字节流:InputStream OutputStream;(可以读写二进制文件,主要处理音频、图片、歌曲、字节流处理单元为1个字节)
- 常用类
- 字节输入流:FileInputStream
- 字节输出流:FileOutputStream
public class FileInputStreamDemo { public static void main(String[] args) { //创建一个输出流,写入文件 //File file = new File("D:/tmp/repoter.txt"); //File file2 = new File("D:/tmp/java.jpg"); //File file3 = new File("D:/tmp/java1.jpg"); int count = 0; File file2 = new File("D:/tmp/a.txt"); File file3 = new File("D:/tmp/aaaa.txt"); FileInputStream fileInputStream = null; //inputread(file, fileInputStream); FileOutputStream fileOutputStream = null; try { fileInputStream = new FileInputStream(file2); fileOutputStream = new FileOutputStream(file3); byte[] buffer = new byte[200]; while(fileInputStream.read(buffer)!=-1){ count++; fileOutputStream.write(buffer); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch(IOException e){ e.printStackTrace(); }finally{ try { if (fileInputStream!=null) { fileInputStream.close(); } if (fileOutputStream!=null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println(count); } private static void inputread(File file, FileInputStream fileInputStream) { try { //创建一个输入流,读取文件 fileInputStream = new FileInputStream(file); int buffer = 0; while((buffer=fileInputStream.read())!=-1){ System.out.print((char)buffer); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e){ e.printStackTrace(); } finally{ try { if (fileInputStream!=null) { fileInputStream.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }}
- 常用类
字符流:Reader,Writer;(主要处理字符或字符串,字符流处理单元为2个字节)
- 常用类
- 字符输入流:FileReader
- 字符输出流:FileWriter
public class UseReaderAndWriteToCopy { public static void main(String[] args) { File file = new File("D:/tmp/a.txt"); File file1 = new File("D:/tmp/aa.txt"); int count = 0; FileReader fileReader = null; FileWriter fileWriter = null; try { fileReader = new FileReader(file); fileWriter = new FileWriter(file1); char[] chArr = new char[100]; while (fileReader.read(chArr)!=-1) { count++; fileWriter.write(chArr); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { if (fileReader!=null) { fileReader.close(); } if (fileWriter!=null) { fileWriter.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println(count); }}
- 常用类
输入流输出流
- 输入流:InputStream Reader(往内存中读叫输入流)
- 输出流:OutputStream Writer(从内存中往外写叫输出流)
节点流和处理流
- 节点流:FileInputStream,FileOutpurStream,FileReader,FileWriter,System.in,System.out;
- 处理流:BufferedInputStream,BufferedOutputStream,BufferedReader,BufferedWriter
- 特点
- 字符缓冲输出流提供了写入一个空行的方法newLine();
- 字符缓冲输出流,把写入的数据先写入到内存,在使用flush()方法将内存数据刷到硬盘上;
- 注意:在使用字符缓冲输出流时,一定先flush(),然后在close(),避免数据的丢失。
- 特点
缓冲区原理
- 对要操作的数据进行临时的缓存,提高读写效率;
public class BufferReaderandBufferWriterCopy { public static void main(String[] args) { BufferedReader buffR = null; BufferedReader buffR1 = null; BufferedWriter buffW = null; try { buffR = new BufferedReader(new FileReader("D:/tmp/st.txt")); buffR1 = new BufferedReader(new FileReader("D:/tmp/st.txt")); buffW = new BufferedWriter(new FileWriter("D:/tmp/stt.txt")); String line = null; while ((line=buffR.readLine())!=null) { buffW.write(line); buffW.newLine(); } while ((line=buffR1.readLine())!=null) { buffW.write(line); buffW.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { if (buffR!=null) { buffR.close(); } if (buffR1!=null) { buffR.close(); } if (buffW!=null) { buffW.close(); } } catch (Exception e2) { // TODO: handle exception } } }}
转换流(字节->字符流)·
InputStreamReader OutputStreamWriter
public class InputStreamReaderandOutputStreamWriter { public static void main(String[] args) { BufferedReader bfr = null; BufferedReader bfr1 = null; BufferedWriter bfw = null; try { bfr = new BufferedReader(new InputStreamReader(new FileInputStream("D:/tmp/OutandIN.java"))); bfr1 = new BufferedReader(new InputStreamReader(System.in)); bfw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:/tmp/OutandIN2.java"))); String line = null; while ((line=bfr.readLine())!=null) { bfw.write(line); bfw.newLine(); } while (!(line=bfr1.readLine()).equals("exit")) { bfw.write(line); bfw.newLine(); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally { try { if (bfr!=null) { bfr.close(); } if (bfr1!=null) { bfr1.close(); } if (bfw!=null) { bfw.flush(); bfw.close(); } } catch (IOException e) { e.printStackTrace(); } } }}
套路
public static void main(String[] args){ IO流赋值null try{ 给IO流对象赋值(可生成实例的子类) 按某种格式read while(按格式得到read值,判断I的read结束){ O的write得到read值 } }catch(FileNotFoundException e){ e.printStackTrace(); }catch(IOExcption e){ e.printStackTrace(); }finally{ try{ if(输入流!=null){ fis.close(); } if(输出流!=null){ fos.close(); } }catch(IOException e){ e.printStackTrace(); } } }
转载地址:https://blog.csdn.net/qq_29593573/article/details/73251354 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月20日 17时33分41秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
PPT - 如何从 PowerPoint 幻灯片中删除幻灯片编号
2019-04-27
正则表达式 - 匹配任何字符(包括换行符)
2019-04-27
程序人生 - 5G来了!路由器和移动硬盘危险了?
2019-04-27
JS - mescroll.js 下拉刷新使用时应该注意的问题(卡顿,滑不动)
2019-04-27
赚钱 36 计 - 第十五计:扣点计
2019-04-27
赚钱 36 计 - 第十六计:生态计
2019-04-27
赚钱 36 计 - 第十九计:传媒计
2019-04-27
赚钱 36 计 - 第二十六计:股份计
2019-04-27
赚钱 36 计 - 第三十一计:微商计
2019-04-27
Postman - 设置全局请求 header 信息
2019-04-27
Postman - 快速分享接口
2019-04-27
MySQL - 条件查询中 AND 与 OR 联合使用
2019-04-27
MD5 - 简介
2019-04-27
Groovy - 探索之 invokeMethod 方法
2021-06-30
网络基础 - URL & URI 区别
2021-06-30
Groovy - 操作符之 “<<”
2021-06-30