34 File
34.1 File类概述和构造方法
package myFile; import java.io.File; //三个构造方法的使用 public class Demo01 { public static void main(String[] args) { //1 File f1 = new File("C:\\Users\\14051\\Desktop\\java_learning\\HelloWorld.java"); //抽象路径表示形式,并不是说这个路径存在 System.out.println(f1); //2 File f2 = new File("E:\\itcast", "java.txt"); System.out.println(f2); //3 File f3 = new File("E:\\itcast"); File f4 = new File(f3, "java.txt"); System.out.println(f4); } }
34.2 File类创建功能
package myFile; import java.io.File; import java.io.IOException; public class Demo02 { public static void main(String[] args) throws IOException { //需求1:D盘itcast文件夹下创建文件java.txt File f1 = new File("D:\\itcast\\java.txt"); System.out.println(f1.createNewFile()); //不存在就创建并返回true;存在就不创建,返回false //需求2:在itcast文件夹下创建目录 File f2 = new File("D:\\itcast\\JavaSE"); System.out.println(f2.mkdir()); //需求3:创建嵌套子目录 File f3 = new File("D:\\itcast\\JavaEE\\JavaWeb"); System.out.println(f3.mkdirs()); //总结:createNewFile()创建文件,mkdir,mkdirs创建目录 } }
34.3 File类判断和获取功能
package myFile; import java.io.File; //8个常见方法的使用 public class Demo4 { public static void main(String[] args) { File f = new File("C:\\programming_software\\JetBrains\\IdeaProjects\\JavaSE_Code\\D34\\src\\myFile\\java.txt"); //1.是否为文件 System.out.println(f.isFile()); //2.是否为目录 System.out.println(f.isDirectory()); //3.是否存在 System.out.println(f.exists()); //4.获取绝对路径 System.out.println(f.getAbsolutePath()); //5.返回路径名字符串 System.out.println(f.getPath()); //6.获取文件或目录名称 System.out.println(f.getName()); System.out.println("---------------------------"); //7.获取文件夹下所有文件的字符串 File f2 = new File("D:\\itcast"); String[] strArray = f2.list(); for (String str : strArray) { System.out.println(str); } System.out.println("-----------------------------"); File[] fileArray = f2.listFiles(); //获取绝对路径 for (File file : fileArray) { System.out.println(file); } System.out.println("------------------------------"); for (File file : fileArray) { // System.out.println(file.getName()); if(file.isFile()){ System.out.println(file.getName()); } } System.out.println("-------------------------------"); } }
34.4 File类删除功能
package myFile; import java.io.File; import java.io.IOException; //删除功能 public class Demo05 { public static void main(String[] args) throws IOException { File f1 = new File("java1.txt"); //创建在项目文件下 System.out.println(f1.createNewFile()); //删除 System.out.println(f1.delete()); //创建和删除目录 File f2 = new File("itcast"); System.out.println(f2.mkdir()); System.out.println(f2.delete()); System.out.println("----------------------"); //创建cast目录,接着在目录下创建java.txt文件 File f3 = new File("cast"); System.out.println(f3.mkdir()); File f4 = new File("cast\\java.txt"); System.out.println(f4.createNewFile()); System.out.println("----------------------------"); //再次删除:注意得先删除路径下的内容,才能删除文件夹 System.out.println(f3.delete()); System.out.println(f4.delete()); System.out.println(f3.delete()); } }
34.5 递归
package myDIGui; //递归 public class Demo01 { public static void main(String[] args) { //不死神兔递归 int[] arr = new int[20]; arr[0] = 1; arr[1] = 1; for (int i = 2; i < arr.length; i++) { arr[i] = arr[i - 1] + arr[i - 2]; } System.out.println(arr[19]); //索引从0开始 //递归 System.out.println(f(20)); } //递归实现 public static int f(int n) { if (n == 1 || n == 2) { return 1; } else { return f(n - 1) + f(n - 2); } } }
案例一:递归求阶乘
package myDIGui; //案例:递归求阶乘 public class Demo02 { public static void main(String[] args) { System.out.println(jiecheng(5)); } public static int jiecheng(int n) { if (n == 0 || n == 1) { return 1; } else { return n * jiecheng(n - 1); } } }
案例二:遍历目录
package myDIGui; import java.io.File; //案例:递归目录,只获取文件的绝对路径 public class Demo03 { public static void main(String[] args) { File f1 = new File("D:\\itcast"); getAllFilePath(f1); } public static void getAllFilePath(File srcFile) { //获取所有问价或目录的file数组 File[] fileArray = srcFile.listFiles(); if (fileArray != null) { //判断该目录下是否有内容 for (File file : fileArray) { //判断该文件是否为目录 if (file.isDirectory()) { getAllFilePath(file); } else { System.out.println(file.getAbsolutePath()); } } } } }
35 字节流
35.1 IO流概述和分类
35.2 字节流写数据
代码:
package myByteStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //将数据写入文件 public class Demo01 { public static void main(String[] args) throws IOException { //创建字节输出流对象:FileOutputStream FileOutputStream fos = new FileOutputStream("fos.txt"); //这一步就能创建文件 fos.write(97); //记事本显示a fos.write(57); //字符9 fos.write(55); //字符7 //最后都要释放资源 fos.close(); //关闭此输出流,并释放与此流相关联的任何系统资源 } }
35.3 字节流写数据的3中方式
package myByteStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; //write三个构造方法的使用 public class Demo02 { public static void main(String[] args) throws IOException { // FileOutputStream fos = new FileOutputStream("fos.txt"); FileOutputStream fos = new FileOutputStream("fos.txt", true); //true实现追加写入 fos.write(97); byte[] bys = {98, 99, 100, 101}; //用字节数组,一次性写入多个字节 fos.write(bys); byte[] bys2 = "fghij".getBytes(); fos.write(bys2); byte[] bys3 = "张曼玉".getBytes(); fos.write(bys3); //写入部分字节 byte[] bys4 = "abcde".getBytes(); fos.write(bys4, 1, 3); //索引为1开始写三个,即bcd //这一步不要忘掉,释放资源 fos.close(); } }
35.4 字节流写数据的两个小问题
一是怎么换行?
二是怎么追加数据?
package myByteStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.nio.charset.StandardCharsets; //字节流写数据的两个小问题 public class Demo3 { public static void main(String[] args) throws IOException { //创建字节输出流对象 FileOutputStream fos = new FileOutputStream("fos.txt"); // FileOutputStream fos = new FileOutputStream("fos.txt", true); //true实现追加写入 //写数据 for (int i = 0; i < 10; i++) { fos.write("hello".getBytes()); fos.write("\r\n".getBytes()); //实现换行 } //释放资源 fos.close(); } }
35.5 字节流写数据加异常处理
这个其实就是用try–catch实现,而不用throws抛出异常了。步骤更繁琐,其实没必要
package myByteStream; import java.io.FileOutputStream; import java.io.IOException; //字节流写数据加入异常处理 public class Demo04 { public static void main(String[] args) { FileOutputStream fos = null; try { fos = new FileOutputStream("fos.txt"); fos.write("hello".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
35.6 字节流读数据(一次读一个字节数据)
package myByteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; //读数据 FileInputStream public class Demo05 { public static void main(String[] args) throws IOException { FileInputStream fi = new FileInputStream("fos.txt"); //第一次读取数据 // int a = fi.read(); // System.out.println(a); //h对应104 // System.out.println((char) a); //h // // //第二次 // System.out.println((char) fi.read()); //没有数据的话会返回-1 // int by = fi.read(); // while(by != -1){ // System.out.print((char)by); // by = fi.read(); // } //优化上面代码 int by; while ((by = fi.read()) != -1) { System.out.print((char) by); } fi.close(); } }
案例:复制文本文件
package myByteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //案例:复制文本文件,把文件a的内容读取复制,生成文件b中存储 public class Demo06 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("fos.txt"); FileOutputStream fos = new FileOutputStream("new_fos.txt"); //读取,写入 int by; while((by=fis.read())!=-1){ fos.write(by); } //释放资源 fis.close(); fos.close(); } }
35.7 字节流读数据(一次读一个字节数组数据)
package myByteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; public class Demo07 { public static void main(String[] args) throws IOException { //创建字节输入流对象 FileInputStream fis = new FileInputStream("fos.txt"); byte[] bys = new byte[1024]; int len; while ((len = fis.read(bys)) != -1) { System.out.println(new String(bys, 0, len)); } //释放资源 fis.close(); } }
案例:复制图片
package myByteStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; //复制图片 public class Demo8 { public static void main(String[] args) throws IOException { FileInputStream fis = new FileInputStream("D:\\itcast\\mn.jpg"); FileOutputStream fos = new FileOutputStream("new_mn.jpg"); byte[] bys = new byte[1024]; int len; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } fis.close(); fos.close(); } }
35.8 字节缓冲流
package myByteStream.Demo2; import java.io.*; public class Demo01 { public static void main(String[] args) throws IOException { // FileOutputStream fos = new FileOutputStream("fos.txt"); // BufferedOutputStream bos = new BufferedOutputStream(fos); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("bos.txt")); bos.write("hello\r\n".getBytes()); bos.write("world\r\n".getBytes()); //释放资源 bos.close(); //读数据 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("bos.txt")); // int by; // while ((by = bis.read()) != -1) { // System.out.print((char) by); // } //读数据第二种方式:一次读取一个数组的数据 byte[] bys = new byte[1024]; int len; while ((len = bis.read(bys)) != -1) { System.out.println(new String(bys, 0, len)); } bis.close(); } }
案例:复制视频
package myByteStream.Demo2; import java.io.*; //复制视频 public class Demo02 { public static void main(String[] args) throws IOException { //记录开始时间 long startTime = System.currentTimeMillis(); //复制视频 // method1(); // method2(); // method3(); method4(); //记录结束时间 long endTime = System.currentTimeMillis(); System.out.println("共耗时:" + (endTime - startTime) + "毫秒"); } //1.基本字节流一次读写一个字节 public static void method1() throws IOException { //共耗时:15031毫秒 FileInputStream fis = new FileInputStream("D:\\itcast\\视频.mp4"); FileOutputStream fos = new FileOutputStream("D:\\itcast\\new_视频.mp4"); int by; while ((by = fis.read()) != -1) { fos.write(by); } fos.close(); fis.close(); } //2.基本字节流一次读写一个字节数组 public static void method2() throws IOException { //共耗时:25毫秒 FileInputStream fis = new FileInputStream("D:\\itcast\\视频.mp4"); FileOutputStream fos = new FileOutputStream("D:\\itcast\\new_视频.mp4"); byte[] bys = new byte[1024]; int len; while ((len = fis.read(bys)) != -1) { fos.write(bys, 0, len); } fos.close(); fis.close(); } //3.字节缓冲流一次读写一个字节 public static void method3() throws IOException { //共耗时:77毫秒 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\itcast\\视频.mp4")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\itcast\\new_视频.mp4")); int by; while ((by = bis.read()) != -1) { bos.write(by); } bis.close(); bos.close(); } //4.字节缓冲流一次读写一个字节数组 public static void method4() throws IOException { //共耗时:7毫秒 BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\itcast\\视频.mp4")); BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\itcast\\new_视频.mp4")); byte[] bys = new byte[1024]; int len; while ((len = bis.read(bys)) != -1) { bos.write(bys, 0, len); } bos.close(); bis.close(); } }
36 字符流
36.1 为什么会出现字符流
package myCharStream.Demo1; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.nio.charset.StandardCharsets; import java.util.Arrays; /* 一个汉字存储: 如果是GBK编码,占用2个字节 如果是UTF-8编码,占用3个字节 */ public class Demo01 { public static void main(String[] args) throws IOException { // FileInputStream fis = new FileInputStream("fos.txt"); // // int by; // while ((by = fis.read()) != -1) { // System.out.print((char) by); // } // // fis.close(); String s = "中国"; byte[] bys = s.getBytes("UTF-8"); System.out.println(Arrays.toString(bys)); byte[] bys2 = s.getBytes("GBK"); System.out.println(Arrays.toString(bys2)); } }
36.2 编码表
36.3 字符串中的编码解码问题
package myCharStream.Demo1; import java.io.UnsupportedEncodingException; import java.util.Arrays; public class Demo02 { public static void main(String[] args) throws UnsupportedEncodingException { //定义一个字符串 String s = "中国"; byte[] bys = s.getBytes("GBK"); //编码 System.out.println(Arrays.toString(bys)); String ss = new String(bys, "GBK"); //解码 System.out.println(ss); } }
36.4 字符流中的编码解码问题
package myCharStream.Demo2; import java.io.*; //字节流读取数据 public class Demo01 { public static void main(String[] args) throws IOException { // FileOutputStream fos = new FileOutputStream("osw.txt"); // OutputStreamWriter osw = new OutputStreamWriter(fos); // OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt")); // OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"), "UTF-8"); OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("osw.txt"), "GBK"); osw.write("中国"); osw.close(); //读数据 InputStreamReader isr = new InputStreamReader(new FileInputStream("osw.txt"),"GBK"); //一次读取一个字符数据 int ch; while((ch=isr.read())!=-1){ System.out.print((char) ch); } isr.close(); } }