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():如果上级目录不存在,会创建父级文件夹 }流分类
操作数据单位:字节流、字符流数据的流向:输入流、输出流流的角色:节点流(直接处理文件的)、处理流(对流的处理)流
抽象基类:InputStream、OutputStream、Reader、Writer节点流:FileInputStream、FileOutputStream、FileReader、FileWriter缓冲流(处理流的一种):BufferedInputStream、BufferedOutputStream、BufferedReader、BufferedWriter字符流读入数据案例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(); } } } } }