文件在程序中是以流的形式操作的。
流:数据在数据源(文件)和程序(内存)之间经历的路径
输入流:数据从数据库(文件)到程序(内存)的路径
输出流:数据从程序(内存)到数据源(文件)的路径
文件流分类
Ⅰ.字节流:可以用于读写二进制文件及任何类型文件
Ⅱ.字符流:可以用于读写文本文件
字节流 | 字符流 | |
输入 | InputStream | Reader |
输出 | OutputStream | Writer |
File类的基本使用
获取文件基本信息
这里示范两个,其他查看JDK帮助文档,做JAVA开发记得不要忘记有JDK帮助文档
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:获取文件基本信息 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 //创建一个文件对象 15 File f = new File("d:\\a.txt"); 16 17 //得到文件的路径 18 System.out.println("文件路径:" + f.getAbsolutePath()); 19 //得到文件的大小,字节数 20 System.out.println("文件大小:" + f.length() + "字节"); 21 } 22 23 }
创建文件
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:创建文件 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 File f = new File("d:\\beekc.txt"); 15 16 if(!f.exists()) 17 { 18 //可以创建 19 try{ 20 f.createNewFile(); 21 System.out.println("创建文件成功"); 22 }catch(Exception e) { 23 e.printStackTrace(); 24 } 25 }else { 26 System.out.println("有文件,不能创建"); 27 } 28 } 29 30 }
创建文件夹
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:创建文件夹 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 File f = new File("d://beekc"); 15 16 if(!f.isDirectory()) 17 { 18 f.mkdir(); 19 System.out.println("创建文件夹成功"); 20 }else { 21 System.out.println("有文件夹存在"); 22 } 23 24 } 25 26 }
获取文件夹下的全部文件
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:获取某文件夹下的全部文件 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 File f = new File("d://beekc"); 15 16 if(f.isDirectory()) 17 { 18 File[] files = f.listFiles(); 19 for(int i = 0; i < files.length; i++) 20 { 21 //System.out.println(files[i].getName()); 22 System.out.println(files[i]); 23 } 24 } 25 } 26 27 }
字节流实例
FileInputStream类读取文件
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:FileInputStream类读取文件 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 //得到一个文件对象 15 File f = new File("d:\\beekc.txt"); 16 FileInputStream fis = null; 17 18 try{ 19 //因为File类没有读写能力,所以需要InputStream类 20 fis = new FileInputStream(f); 21 22 //定义一个字节数组,相当于缓存 23 byte[] bytes = new byte[1024]; 24 25 //读取实际字节数 26 int n = 0; 27 28 //循环读取 29 while((n = fis.read(bytes)) != -1) 30 { 31 //把字节转成String 32 String s = new String(bytes,0,n); 33 System.out.println(s); 34 } 35 }catch(Exception e){ 36 e.printStackTrace(); 37 }finally{ 38 try{ 39 fis.close(); 40 }catch (Exception e){ 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 }
FileOutputStream类把数据写入文件
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:数据写入文件 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 //得到文件对象 15 File f = new File("d:\\beekc.txt"); 16 FileOutputStream fos = null; 17 18 try{ 19 fos = new FileOutputStream(f); 20 21 String s = "明天你好\r\n"; 22 String s1 = "中国很好"; 23 24 //定义字节数组 25 //byte[] bytes = new byte[1024]; 26 fos.write(s.getBytes()); 27 fos.write(s1.getBytes()); 28 29 }catch (Exception e){ 30 e.printStackTrace(); 31 }finally { 32 try{ 33 fos.close(); 34 }catch (Exception e) { 35 e.printStackTrace(); 36 }finally{ 37 38 } 39 } 40 41 } 42 43 }
图片拷贝
1 /* 2 * 作者:白客C 3 * 时间:2020年03月14日 4 * 内容:图片拷贝 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 FileInputStream fis = null; 15 FileOutputStream fos = null; 16 17 try{ 18 fis = new FileInputStream("c:\\0.png"); 19 fos = new FileOutputStream("d:\\0.png"); 20 21 //相当于缓存 22 byte[] buf = new byte[512]; 23 24 int n = 0; 25 //循环读取字符 26 while((n = fis.read(buf)) != -1) 27 { 28 //输出到指定文件 29 fos.write(buf); 30 } 31 32 }catch (Exception e){ 33 e.printStackTrace(); 34 }finally{ 35 //关闭文件 36 try{ 37 fis.close(); 38 fos.close(); 39 }catch (Exception e){ 40 e.printStackTrace(); 41 } 42 } 43 } 44 45 }
字符流实例
读取一个文件写到另一个文件中
1 /* 2 * 作者:白客C 3 * 时间:2020年03月15日 4 * 内容:读取一个文件写到另一个文件中 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 //文件取出字符流对象(输入流) 15 FileReader fr = null; 16 //写入到文件(输出流) 17 FileWriter fw = null; 18 19 try{ 20 //创建fr对象 21 fr = new FileReader("d:\\beekc.txt"); 22 23 //创建输出对象 24 fw = new FileWriter("c:\\text.txt"); 25 26 int n = 0; 27 //读入到内存 28 char c[] = new char[1024]; 29 30 while ((n = fr.read(c)) != -1) 31 { 32 //String s = new String(c,0,n); 33 //System.out.println(s); 34 fw.write(c); 35 } 36 37 }catch(Exception e) 38 { 39 e.printStackTrace(); 40 }finally{ 41 //关闭文件 42 try{ 43 fr.close(); 44 fw.close(); 45 }catch (Exception e){ 46 e.printStackTrace(); 47 } 48 } 49 50 } 51 52 }
缓冲字符流
1 /* 2 * 作者:白客C 3 * 时间:2020年03月15日 4 * 内容:缓冲字符流 5 */ 6 7 package com.beekc.www; 8 import java.io.*; 9 10 public class Beekc{ 11 12 public static void main(String[] args) 13 { 14 BufferedReader br = null; 15 BufferedWriter bw = null; 16 try{ 17 //先创建FileReader对象 18 FileReader fr = new FileReader("d:\\beekc.txt"); 19 br = new BufferedReader(fr); 20 21 FileWriter fw = new FileWriter("d:\\a.txt"); 22 bw = new BufferedWriter(fw); 23 24 //循环读取文件 25 String s = ""; 26 while ((s = br.readLine()) != null) 27 { 28 //System.out.print(s); 29 //输出到磁盘 30 bw.write(s); 31 } 32 33 }catch (Exception e){ 34 e.printStackTrace(); 35 }finally{ 36 try{ 37 br.close(); 38 bw.close(); 39 }catch (Exception e) 40 { 41 e.printStackTrace(); 42 } 43 } 44 } 45 46 }