Java中的I/O
发布时间:2020-12-15 07:30:28 所属栏目:Java 来源:网络整理
导读:1、Java中的I/O分类 I/O分类: 输入流,输出流,或者 字节流,字符流 I/O中的四个抽象基类: InputStream,OutputStream:两者属于字节流,前者输入,后者输出。一般后缀名为这两个的都属于字节流家族。 Reader,Writer:两者属于字符流,前者输入,后者输出
1、Java中的I/O分类I/O分类:
I/O中的四个抽象基类:
2、File常用方法常用方法:
1 package com.spring.test.service; 2 3 import java.io.File; 4 import java.io.IOException; 5 6 /** 7 * @Author: philosopherZB 8 * @Date: 2019/9/29 9 */ 10 public class Test { 11 public static void main(String[] args){ 12 //File.separator返回本系统的名称分隔符,方便代码跨系统移植,比如windows下文件分隔符是,而其他系统可能就不是。 13 //创建一个文件夹 14 String path1 = "D:" + File.separator + "Test"; 15 File file1 = new File(path1); 16 file1.mkdir(); 17 18 //创建一个文件夹以及他的上级文件夹 19 String path2 = "D:" + File.separator + "Test" + File.separator + "Temp" + File.separator + "demo"; 20 File file2 = new File(path2); 21 file2.mkdirs(); 22 23 //创建一个文件 24 String path3 = "D:" + File.separator + "Test" + File.separator + "Test.txt"; 25 File file3 = new File(path3); 26 try { 27 file3.createNewFile(); 28 } catch (IOException e) { 29 e.printStackTrace(); 30 } 31 32 //读取文件夹以及文件 33 String path4 = "D:" + File.separator + "Test"; //指定读取目录 34 File file4 = new File(path4); 35 //判断目录是否存在 36 if(file4.isDirectory()){ 37 // String[] str = file4.list(); 38 // for(int i=0;i<str.length;i++){ 39 // File f = new File("D:" + File.separator + "Test" + File.separator + str[i]); 40 // if(f.isDirectory()) { 41 // System.out.println(str[i] + "是一个目录"); 42 // }else{ 43 // System.out.println(str[i] + "是一个文件"); 44 // f.delete();//删除该文件 45 // } 46 // } 47 //如果需要获取全路径可以使用listFiles,这个方法的返回时File。 48 File[] files = file4.listFiles(); 49 if(files.length!=0) { 50 for(File file : files){ 51 if(file.isDirectory()) { 52 System.out.println(file + "是一个目录"); 53 }else{ 54 System.out.println(file + "是一个文件"); 55 file.delete();//删除该文件 56 } 57 } 58 } 59 }else{ 60 System.out.println("目录不存在"); 61 } 62 } 63 } 3、对文件进行写入,读出分别使用字符流,字节流写入读出:
1 package com.spring.test.service; 2 3 import java.io.*; 4 5 /** 6 * @Author: philosopherZB 7 * @Date: 2019/9/29 8 */ 9 public class Test { 10 public static void main(String[] args){ 11 String path = "D:" + File.separator + "Test" + File.separator + "Test.txt"; 12 String str = "文件测试内容"; 13 //字节流 14 writeFile(path,str); 15 System.out.println("字节流:" + readFile(path)); 16 //字符流 17 writeFileByStr(path,str); 18 System.out.println("字符流:" + readFileByStr(path)); 19 } 20 21 //写入字符串到指定文件中----字节流,字节流线程不安全,可以通过synchronized来使其成为一个静态同步方法 22 public static synchronized void writeFile(String fileName,String content){ 23 //此处使用的是try-with-resource,实现了Closeable接口的类,可以免去自己写finally块进行资源关闭的操作,是一个java的语法糖 24 try(BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(fileName)))){ 25 byte[] b = content.getBytes(); 26 bos.write(b); //写入 27 bos.flush(); //刷新缓存,及时将缓存中的数据刷到文件中 28 }catch (IOException e){ 29 throw new RuntimeException(e.getMessage(),e); 30 } 31 } 32 33 //从指定文件中读取字符串-----字节流,字节流线程不安全,可以通过synchronized来使其成为一个静态同步方法 34 public static synchronized String readFile(String fileName){ 35 try(BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(fileName)))){ 36 byte[] b = new byte[256]; 37 int count = 0; 38 int temp = 0; 39 //不确定文件内容有多大,循环读取 40 //读到文件末尾的时候才会返回-1 41 while((temp = bis.read()) != -1){ 42 b[count++] = (byte) temp; 43 } 44 return new String(b); 45 }catch (IOException e){ 46 throw new RuntimeException(e.getMessage(),e); 47 } 48 } 49 50 //写入字符串到指定文件中----字符流,字符流的write方法是线程安全的 51 public static void writeFileByStr(String fileName,String content){ 52 try(BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName)))){ 53 bw.write(content); 54 bw.flush(); 55 }catch (IOException e){ 56 throw new RuntimeException(e.getMessage(),e); 57 } 58 } 59 60 //从指定文件中读取字符串-----字符流 61 public static String readFileByStr(String fileName){ 62 try(BufferedReader br = new BufferedReader(new FileReader(new File(fileName)))){ 63 char[] c = new char[256]; 64 int count = 0; 65 int temp = 0; 66 //不确定文件内容有多大,循环读取 67 //读到文件末尾的时候才会返回-1 68 while((temp = br.read()) != -1){ 69 c[count++] = (char) temp; 70 } 71 return new String(c); 72 }catch (IOException e){ 73 throw new RuntimeException(e.getMessage(),e); 74 } 75 } 76 77 } 4、BIO,NIO,AIOBIO(Block IO):
NIO(NewIO):
AIO(Asynchronous IO):
IO阶段:
同步,异步:
阻塞,非阻塞:
BIO,NIO,AIO示例代码参考:
(以上所有内容皆为个人笔记,如有错误之处还望指正。) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |