内存与存储设备之间传输数据的通道
下面两个为字节流的父类(抽象类)
//InputStream 字节输入流 public int read(){} public int read(byte[] b){} public int read(byte[] b, int off, int len){} // OutputStream 字节输出流 public void write(int n){} public void write(byte[] b){} public void write(byte[] b, int off, int len){}文件输入流
psvm(String[] args) throws Exception{ // 1 创建FileInputStream 并指定文件路径 FileInputStream fis = new FileInputStream("d:\\abc.txt"); // 2 读取文件 // fis.read(); // 2.1单字节读取 int data = 0; while((data = fis.read()) != -1){ sout((char)data); } // 2.2 一次读取多个字节 byte[] buf = new byte[3]; // 大小为3的缓存区 int count = fis.read(buf); // 一次读3个 sout(new String(buf)); sout(count); int count2 = fis.read(buf); // 再读3个 sout(new String(buf)); sout(count2); // 上述优化后 int count = 0; while((count = fis.read(buf)) != -1){ sout(new String(buf, 0, count)); } // 3 关闭 fis.close(); }文件输出流
psvm(String[] args) throws Exception{ // 1 创建文件字节输出流 FileOutputStream fos = new FileOutputStream("路径", true);// true表示不覆盖 接着写 // 2 写入文件 fos.write(97); fos.write('a'); // String string = "hello world"; fos.write(string.getByte()); // 3 关闭 fos.close(); }缓冲流:BufferedInputStream / BufferedOutputStream
提高IO效率,减少访问磁盘次数数据存储在缓冲区中,flush是将缓冲区的内容写入文件中,也可以直接close // 使用字节缓冲流 读取 文件 psvm(String[] args) throws Exception{ // 1 创建BufferedInputStream FileInputStream fis = new FileInputStream("路径"); BufferedInputStream bis = new BufferedInputStream(fis); //缓冲区默认大小:8192=8kb // 2 读取 int data = 0; while((data = bis.read()) != -1){ sout((char)data); } // 用自己创建的缓冲区,大小为1024=1kb byte[] buf = new byte[1024]; int count = 0; while((count = bis.read(buf)) != -1){ sout(new String(buf, 0, count)); } // 3 关闭 bis.close(); } // 使用字节缓冲流 写入 文件 psvm(String[] args) throws Exception{ // 1 创建BufferedInputStream FileOutputStream fos = new FileOutputStream("路径"); BufferedOutputStream bos = new BufferedOutputStream(fos); // 2 写入文件 for(int i = 0; i < 10; i ++){ bos.write("hello".getBytes());// 写入8k缓冲区 bos.flush(); // 刷新到硬盘 } // 3 关闭 bos.close(); }使用流传输对象的过程称为序列化、反序列化
UTF-8… 不赘述
reader 字符输入流
public int read(){}public int read(char[] c){}public int read(char[] b, int off, int len){}Writer 字符输出流
public void write(int n){}public void write(String str){}public void write(char[] c){} // 1. 创建FileReader 文件字符输入流 FileReader fr = new FileReader(".."); // 2. 读取 // 2.1 单个字符读取 int data = 0; while((data = fr.read()) != -1){ sout((char)data);// 读取一个字符 } char[] buf = new char[2];// 字符缓冲区读取 int count = 0; while((count = fr.read(buf) != -1)){ sout(new String(buf, 0, count)); } // 3. 关闭 fr.close(); // 1. 创建FileWriter对象 FileWriter fw = new FileWriter(".."); // 2. 写入 for(int i = 0; i < 10; i ++){ fw.write("写入的内容"); } // 3. 关闭 fw.flush(); fw.close(); sout("执行完毕");不能复制图片或二进制文件,使用字节流可以复制任意文件
psvm(String[] args) throws Exception{ // 1. 创建 FileReader fr = new FileReader("..."); FileWriter fw = new FileWriter("..."); // 2. 读写 int data = 0; while((data = fr.read()) != -1){ fw.write(data); } // 3. 关闭 fw.flush(); fw.close(); fr.close(); }高效读写、支持输入换行符、可一次写一行读一行
psvm(String[] args) throws Exception{ // 创建缓冲流 FileReader fr = new FileReader(".."); BufferedReader br = new BufferedReader(fr); // 读取 // 1. 第一种方式 char[] buf = new char[1024]; int count = 0; while((count = br.read(buf)) != -1){ sout(new String(buf, 0, count)); } // 2. 第二种方式 一行一行读取 String line = null; while((line = br.readLine()) != null){ sout(line); } // 关闭 br.close(); } psvm(String[] args){ // 1. 创建BufferedWriter对象 FileWriter fw = new FileWriter(".."); BufferedWriter bw = new BufferedWriter(fw); // 2. 写入 for(int i = 0; i < 10; i ++){ bw.write("写入的内容"); vw.newLine(); // 写入一个换行符 bw.flush(); } // 3. 关闭 bw.close(); // 此时会自动关闭fw }封装了print() / println() 方法 支持写入后换行
支持数据原样打印
psvm(String[] args){ // 1 创建打印流 PrintWriter pw = new PrintWriter(".."); // 2 打印 pw.println(97); //原样打印,打印到文件中就是97而不是a pw.println(true); pw.println(3.14); pw.println('a'); // 3 关闭 pw.close(); }桥转换流 InputStreamReader / OutputStreamWriter
InputStreamReader 可将字节流转换为字符流
OutputStreamWriter可将字符流转换为字节流
可设置字符的编码方式
psvm(String[] args) throws Exception{ // 1 创建InputStreamReader对象 FileInputStream fis = new FisInputStream(".."); InputStreamReader isr = new InputStreamReader(fis, "utf-8"); // 2 读取文件 int data = 0; while((data = isr.read()) != -1){ sout((char)data); } // 3 关闭 isr.close(); } psvm(String[] args) throws Exception{ // 1 创建OutputStreamReader对象 FileOutputStream fos = new FileOutputStream(".."); OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8"); // 2 写入 for(int i = 0; i < 10; i ++){ osw.write("写入内容"); osw.flush(); } // 3 关闭 osw.close(); }字符缓冲流接收转换流对象的方式:
//磁盘文件字节流加载为内存中的字符流 BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(".."),"UTF-8")); //内存中的字符流存储到磁盘文件的字节流 BufferedWriter in = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(".."),"UTF-8"));概念:代表物理盘符中的一个文件或者文件夹
/* File类的使用 1. 分隔符 2. 文件操作 3. 文件夹操作 */ public class Demo{ psvm(String[] args){ separator(); } // 1. 分隔符 public static void separator(){ sout("路径分隔符" + File.pathSeparator); sout("名称分隔符" + File.separator); } // 2. 文件操作 public static void fileOpen(){ // 1. 创建文件 if(!file.exists()){ // 是否存在 File file = new File("..."); boolean b = file.creatNewFile(); } // 2. 删除文件 // 2.1 直接删除 file.delete(); // 成功true // 2.2 使用jvm退出时删除 file.deleteOnExit(); // 3. 获取文件信息 sout("获取绝对路径" + file.getAbsolutePaht()); sout("获取路径" + file.getPath()); sout("获取文件名称" + file.getName()); sout("获取夫目录" + file.getParent()); sout("获取文件长度" + file.length()); sout("文件创建时间" + new Date(file.lashModified()).toLocalString()); // 4. 判断 sout("是否可写" + file.canWrite()); sout("是否是文件" + file.isFile()); sout("是否隐藏" + file.isHidden()); } // 文件夹操作 public static void directoryOpe() throws Exception{ // 1. 创建文件夹 File dir = new File("..."); sout(dir.toString()); if(!dir.exists()){ //dir.mkdir(); // 只能创建单级目录 dir.mkdirs(); // 创建多级目录 } // 2. 删除文件夹 // 2.1 直接删除 dir.delete(); // 只能删除最底层空目录 // 2.2 使用jvm删除 dir.deleteOnExit(); // 3. 获取文件夹信息 sout("获取绝对路径" + dir.getAbsolutePaht()); sout("获取路径" + dir.getPath()); sout("获取文件名称" + dir.getName()); sout("获取夫目录" + dir.getParent()); sout("获取文件长度" + dir.length()); sout("文件夹创建时间" + new Date(dir.lashModified()).toLocalString()); // 4. 判断 sout("是否是文件夹" + dir.isFile()); sout("是否隐藏" + dir.isHidden()); // 5. 遍历文件夹 File dir2 = new File("..."); String[] files = dir2.list(); for(String string : files){ sout(string); } // FileFilter接口的使用,其作用为返回满足设定的要求的文件 File[] files2 = dir2.listFiles(new FileFilter(){ @Override public boolean accept(File pathname){ if(pathname.getName().endsWith(".jpg")){ //只获取格式为.jpg的图片 return true; } return false; } }); for(File file : files2){ sout(file.getName()); } } }