Java基础--IO流(1)

mac2024-01-28  31

IO流

IO四步走1. 找文件路径,创建文件路径对象 2. 流的实例化,创建需要的流 3. 读入写出操作,具体流的操作具体分析 4. 关闭资源

File类

File类的一个对象,代表一个文件或者一个文件目录

File类声明在java.io下

如何创建File类实例相对路径和绝对路径 相对:相较于某个路径下,指明的路径绝对:包含盘符在内的文件或者文件目录路径 路径分隔符 windows:斜杠或者/unix:/

常用方法:

public String getAbsolutePath():获取绝对路径public String getPath():获取路径public String getName():获取名称public String getParent():获取上层文件目录路径。没有返回nullpublic Long length():获取文件长度(字节数),不能获取目录长度public Long lastModified():获取最后一次的修改时间,毫秒值public String[] list():获取指定目录下的所有文件或文件目录的名称数组public File[] listFiles():获取指定目录下的所有文件或者文件目录的File数组

判断方法:

boolean isDirectory():是否是目录boolean isFile():是否是文件boolean exists():是否存在boolean canRead():是否可读boolean canWrite():是否可写boolean isHidden():是否隐藏

创建方法:

boolean createNewFile();//创建新的文件boolean mkdir();//创建文件夹,存在falseboolean mkdirs();//穿件文件夹,如果父类目录不存在,一并创建父文件夹

注意点:

File类中设计文件或文件目录的查找,创建,删除,重命名,修改改时间,文件大小等方法,并未涉及写入或读取文件内容的操作,如果需要读写操作,需要IO流来完成后续File类的对象常会用作参数传递到流的构造器中,指明读取或写入的目标 public static void main(String[] args) throws Exception { /** * 找到文件目录和路径 主要是查出路径 */ File file1 = new File("Hello.txt"); File file2 = new File("D:\\IdeaProjects\\JavaDemo\\he.txt"); File file3 = new File("D:\\IdeaProjects","JavaIO"); File file4 = new File(file3,"Hello.txt"); System.out.println("******************************************"); /** 查看一些文件或者文件夹的属性 */ System.out.println(file1.getAbsolutePath()); System.out.println(file1.getParent()); System.out.println(file1.getName()); System.out.println(file1.getPath()); System.out.println(file1.length()); System.out.println(file1.lastModified()); System.out.println(file1.exists()); /** * 创建文件 添加文件 */ File file5 = new File("D:\\IdeaProjects\\JavaDemo\\he.txt"); if(file5.exists()){ System.out.println("文件已存在,创建失败"); }else{ file5.createNewFile(); System.out.println("文件不存在,创建成功"); } /** * 创建文件夹 */ File file6 = new File("D:\\IdeaProjects\\JavaDemo\\sou"); boolean mkdir = file6.mkdir(); if(mkdir){ System.out.println("文件夹创建成功"); }else{ System.out.println("文件夹创建失败"); } //file.mkdirs():如果上级目录不存在,会创建父级文件夹 }

IO流(读写流,把硬盘中的文件读写进内存–程序–到硬盘)

流分类

操作数据单位:字节流、字符流数据的流向:输入流、输出流流的角色:节点流(直接处理文件的)、处理流(对流的处理)

抽象基类:InputStream、OutputStream、Reader、Writer节点流:FileInputStream、FileOutputStream、FileReader、FileWriter缓冲流(处理流的一种):BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter

字符流demo

字符流读入数据案例FileReader

read():返回读入的一个字符,默认返回int类型,如果达到文件末尾,返回-1异常处理:为了保证资源一定执行关闭操作,需要用try-catch-finally处理读入的文件一定要存在,否则会报文件不存在异常

字符流不能实现对图片的读入和写出

但是对于文本文件,应该用字符流读写,因为不容易出现乱码

对于图片等格式的文件,适合用字节流读取,因为它是以字节为形式读取的

public void test1() { FileReader fileReader = null; try { //1. 获取文件路径 File file = new File("D:\\IdeaProjects\\JavaDemo\\he.txt"); //2. 接通流,字符流 fileReader = new FileReader(file); //3. 循环读取,当读取到-1时结束 // read() 读取一个字符,每次返回读取到的一个字符 int data = fileReader.read(); while (data != -1) { System.out.println((char) data); data = fileReader.read();//第一次读到H,返回H,.... } } catch (Exception e) { e.printStackTrace(); } finally { //4. 关闭流 try { if(fileReader != null){ fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } read的重载方法:read(char[] c) 存字符数组中读取读到的字符 public void test2(){ FileReader fr = null; try { //1. 创建路径对象,找到文件 File file = new File("D:\\IdeaProjects\\JavaDemo\\he.txt"); //2. 创建流对象 fr = new FileReader(file); //3. 读写操作 //一次读取5个字符,用字符数组装载 char[] cbuffer = new char[5]; int len;//每次读取的长度 //read(char[] c) 读取字符数组中的字符 while((len = fr.read(cbuffer)) != -1){ //写法1 for (int i = 0; i < len; i++) { System.out.print(cbuffer[i]); } //写法2 /*String str = new String(cbuffer,0,len); System.out.println(str);*/ } } catch (Exception e) { e.printStackTrace(); } finally { try { if(fr != null){ fr.close(); } } catch (Exception e) { e.printStackTrace(); } } } 从内存(程序)中写出数据到硬盘的文件里 输出操作:对应的file可以不存在,如果不存在,在输出过程中,会自动创建该文件如果不存在,根据构造器来具体使用FileWriter(file, true/false);//如果是true,会在原有文件内容中追加内容, 如果是false,会覆盖原有内容FileWriter(file);//默认构造器 public void test3(){ FileWriter fw = null; try { //1. 找到文件,写出操作时,如果文件不存在,会自动创建文件 File file = new File("Hello.txt"); //2. 创建流对象 fw = new FileWriter(file); //3. 写入操作 fw.write("I am bob\n"); fw.write("U and Me"); } catch (IOException e) { e.printStackTrace(); } finally { //4. 关闭资源 try { if(fw != null){ fw.close(); } } catch (IOException e) { e.printStackTrace(); } } } 读取和写入 public void test4(){ FileReader fr = null; FileWriter fw = null; try { //1. 创建两个文件的路径 File file1 = new File("Hello.txt"); File file2 = new File("he.txt"); //2. 创建读入和写出流 fr = new FileReader(file1); fw = new FileWriter(file2); //3. 读操作 和 写操作 char[] charBuffer = new char[10]; int len;//每次读入到数组中的个数 while((len = fr.read(charBuffer)) != -1){ //写法1 for (int i = 0; i < len; i++) { fw.write(charBuffer[i]); } //写法2 // fw.write(charBuffer,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fw != null){ //4. 关闭流 fw.close(); } } catch (IOException e) { e.printStackTrace(); }finally { if(fr != null){ try { // 读入流必须关掉 fr.close(); } catch (IOException e) { e.printStackTrace(); } } } } }

字节流

适合读写一些图片,音频之类的,按字节读取 public void test1() throws IOException { //1. 找到文件 File file = new File("he.txt"); //2. 创建字节读取流 FileInputStream fis = new FileInputStream(file); //3. 读取文件内容 因为流是字节流,用字节数组存 byte[] bytes = new byte[5]; int len; while ((len = fis.read(bytes)) != -1){ /*for (int i = 0; i < len; i++) { System.out.println(bytes[i]); }*/ String str = new String(bytes,0,len); System.out.print(str); } //4. 关闭流 fis.close(); } 文件复制: public void copyFile(String srcPath, String endPath){ FileInputStream fis = null; FileOutputStream fos = null; try { //1. 创建文件路径对象,找到文件 File file1 = new File(srcPath); File file2 = new File(endPath); //2. 创建流对象 fis = new FileInputStream(file1); fos = new FileOutputStream(file2); //3. 读取字节 byte[] bytes = new byte[1024]; int len; while((len = fis.read(bytes)) != -1){ fos.write(bytes,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fos != null){ fos.close(); } } catch (IOException e) { e.printStackTrace(); } finally { try { if(fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } } @Test public void testCopyFile(){ long start = System.currentTimeMillis();//开始时间 String srcPath = "D:\\IdeaProjects\\JavaDemo\\sou\\图片01.jpg"; String endPath = "D:\\IdeaProjects\\JavaDemo\\sou\\图片03.jpg"; copyFile(srcPath,endPath); long end = System.currentTimeMillis();//结束时间 System.out.println("花费时间:" + (end - start)); }

缓冲流

为了解决文件读写效率不高问题,使用缓冲流缓冲流就是给节点流一个强化提高速度的原理 提供了一个缓冲区 1024的8次方,8190 提供了一个方法,flush() 刷新缓冲区 public void test(){ /** * 使用Buffered流主要是为了提高读写速度 */ BufferedInputStream bis = null; BufferedOutputStream bos = null; try { //1. 找到文件 File file1 = new File("D:\\IdeaProjects\\JavaDemo\\sou\\图片01.jpg"); File file2 = new File("D:\\IdeaProjects\\JavaDemo\\sou\\图片04.jpg"); //2. 创建输入输出流 FileInputStream fis = new FileInputStream(file1); FileOutputStream fos = new FileOutputStream(file2); //3. 给输入输出流加Buffer流 bis = new BufferedInputStream(fis); bos = new BufferedOutputStream(fos); //4. 读入和写出文件内容 byte[] bytes = new byte[1024]; int len; while((len = bis.read(bytes)) != -1){ bos.write(bytes,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //5. 先关闭外层流,再关闭内层流,内层流可以不用管 if(bis != null){ try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if(bos != null){ try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } }

字节流和字符流的转换—转换流

转换流属于字符流 InputStreamReader:将一个字节的输入流转换成字符的输入流OutputStreamWriter:一个字节的输出流转换成字符的输出流 作用就是:字节流和字符流的转换编码和解码: 编码:字节,字节数组转换成字符串,字符数组解码:字符数组,字符串转换成字节,字节数组 public void test(){ InputStreamReader isr = null; try { //创建字节流,读取文件 FileInputStream fis = new FileInputStream("D:\\IdeaProjects\\JavaDemo\\Hello.txt"); //创建字节字符转换流,以默认字符集形式 isr = new InputStreamReader(fis); //可指定编码 //InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); char[] chars = new char[10]; int len;//每个字符长度 while ((len = isr.read(chars)) != -1) { // String str = new String(chars,0,len); // System.out.println(str); for (int i = 0; i < len; i++) { System.out.print(chars[i]); } } } catch (IOException e) { e.printStackTrace(); } finally { if (isr != null){ try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } } } 转换流的编码和解码 public void test2(){ InputStreamReader isr = null;//将字节转换成字符--解码 OutputStreamWriter osw = null;//将字符转换成字节--编码 try { //1. 找到文件对象 File file1 = new File("D:\\IdeaProjects\\JavaDemo\\Hello.txt"); File file2 = new File("D:\\IdeaProjects\\JavaDemo\\he.txt"); //2. 创建流 FileInputStream fis = new FileInputStream(file1); OutputStream fos = new FileOutputStream(file2); // 加入转换流--把字节文件转换成字符文件的流 isr = new InputStreamReader(fis,"UTF-8"); osw = new OutputStreamWriter(fos,"UTF-8"); //3. 读写数据 char[] c = new char[10]; int len;//每次读取的字节数 while((len = isr.read(c)) != -1){ osw.write(c,0,len); } } catch (IOException e) { e.printStackTrace(); } finally { //4. 关闭流 if(osw != null){ try { osw.close(); } catch (IOException e) { e.printStackTrace(); } } if(isr != null){ try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } } }
最新回复(0)