本文为B站Java教学视频BV1Kb411W75N的相关笔记,主要用于我的记录与分享,若有错误欢迎留言指出。
本章笔记涵盖视频内容P581~P617java
public void test1(){ //构造器1 File file1 = new File("hello.txt"); //相对路径 File file2 = new File("D:\\workspace_idea\\day08\\he.txt"); //绝对路径 //构造器2 File file3 = new File("D:\\workspace_idea","day08");//父路径下的子路径 //构造器3 File file4 = new File(file3,"hi.txt"); //应用其它File类的路径 }
File类的获取功能windows
public String getAbsolutePath():获取绝对路径数组
public String getPath():获取路径app
public String getName():获取名称dom
public String getParent():获取上层文件目录路径。若无,返回nullide
public long length():获取文件长度(字节数),不能获取目录的长度优化
public long lastModified():获取最后一次的修改时间(时间戳)ui
public String[] list:获取指定目录下的全部文件,或者文件目录的名称数组编码
public String[] listFiles:获取指定目录下的全部文件,或者文件目录的File数组idea
File类的重命名功能
public boolean renameTo(File dest):把文件重命名为指定的文件路径
File类的判断功能
public boolean isDirectory():判断是不是文件目录
public boolean isFile():判断是不是文件
public boolean exists():判断是否存在
public boolean canRead():判断是不是可读
public boolean canWrite():判断是否可写
public boolean isHidden():判断是不是隐藏
File类的建立与删除功能
public boolean createNewFile():建立文件。若文件存在,则不建立,返回false
public boolean mkdir():建立文件目录。若是此文件目录已存在,则不建立。若是此文件目录上层目录不存在,也不建立
public boolean mkdirs():建立文件目录。若是此文件目录已存在,则不建立。若是此文件目录上层目录不存在,则一并建立
public boolean delete():删除文件或者文件夹
按操做数据单位不一样分为:字节流(8 bit),字符流(16 bit)
按数据流的流向不一样分为:输入流,输出流
按流的角色的不一样分为:节点流,处理流
抽象基类 | 字节流 | 字符流 |
---|---|---|
输入流 | InputStream | Reader |
输出流 | OutputStream | Writer |
流的体系结构
抽象基类 | 节点流(文件流) | 缓冲流(处理流的一种) |
---|---|---|
InputStream | FileInputStream | BufferedInputStream |
OutputStream | FileOutputStream | BufferedOutputStream |
Reader | FileReader | BufferedReader |
Writer | FileWriter | BufferedWriter |
public class Test{ public static void main(String[] args) { } public void testFileReader(){ //1.实例化File类的对象,指明要操做的文件 File file = new File("hello.txt"); //相对路径 try { //2.提供具体的流 FileReader fr = new FileReader(file); //3.数据的读入 //read():返回读入的一个字符,若是达到文件末尾,返回-1 int data = 0; while((data = fr.read()) != -1){ System.out.print((char)data); } //4.流的关闭操做 fr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /* 异常的处理:为了保证流资源必定能够执行关闭操做,不推荐使用throw,而是须要用try-catch-finally处理 读入的文件必定要存在,不然就会报FileNotFoundException */
public class Test{ public static void main(String[] args) { } public void testFileReader1(){ //1.File类的实例化 File file = new File("hello.txt"); try { //2.FileReader流的实例化 FileReader fr = new FileReader(file); //3.读入的操做 //read(char[] cbuf):返回每次读入cbuf数组中的字符的个数,若是达到文件末尾,返回-1 char[] cbuf = new char[5]; //每次都read长度为5的字符 int len; while((len = fr.read(cbuf)) != -1){ //方式一: //错误的写法 // for(int i = 0;i < cbuf.length;i++){ // System.out.print(cbuf[i]); // } //正确的写法 for(int i = 0;i < len;i++){ System.out.print(cbuf[i]); } //方式二: //错误的写法 // String str = new String(cbuf); // System.out.print(str); //正确的写法 String str = new String(cbuf,0,len); System.out.print(str); } //4.资源的关闭 fr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /* 重载后的read能够一次性读入多个字符,效率获得提高 须要注意数组的覆盖问题 */
public class Test{ public static void main(String[] args) { } public void testFileWriter(){ //1.提供File类的对象,指明写出到的文件 File file = new File("hello1.txt"); try { //2.提供FileWriter的对象,用于数据的写出 FileWriter fw = new FileWriter(file,false); //3.写出的操做 fw.write("AAA"); fw.write("BBBBBBB"); //4.流资源的关闭 fw.close(); } catch (IOException e) { e.printStackTrace(); } } } /* 写入操做,对应的File能够不存在,不会报异常 File对应的硬盘中的文件若是不存在,在输出的过程当中,会自动建立此文件 File对应的硬盘中的文件若是存在: 若是流使用的构造器是:FileWriter(file,false)/FileWriter(file):对原有文件的覆盖 若是流使用的构造器是:FileWriter(file,true):不会对原有文件覆盖,而是在原有文件基础上追加内容 */
public void testFileInputOutputStream(){ File srcFile = new File("1.jpg"); File destFile = new File("2.jpg"); FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream(srcFile); fos = new FileOutputStream(destFile); byte[] buffer = new byte[5]; int len; while((len = fis.read(buffer)) != -1){ fos.write(buffer,0,len); } fos.close(); fis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /* 对于文本文件(txt/java/c/cpp),使用字符流处理 对于非文本文件(jpg/mp3/mp4/doc),使用字节流处理 */
public class Test{ public static void main(String[] args) { //1.造文件 File scrFile = new File("1.jpg"); File destFile = new File("3.jpg"); try { //2.造流 //2.1 造节点流 FileInputStream fis = new FileInputStream(scrFile); FileOutputStream fos = new FileOutputStream(destFile); //2.2 造缓冲流 BufferedInputStream bis = new BufferedInputStream(fis); BufferedOutputStream bos = new BufferedOutputStream(fos); //3.复制的细节:读取,写入 byte[] buffer = new byte[10]; int len; while((len = bis.read(buffer)) != -1){ bos.write(buffer,0,len); } //4.资源关闭 //关闭外层流的同时,内层流也会自动的进行关闭。 bos.close(); bis.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public class Test{ public static void main(String[] args) { try { //匿名建立处理流 BufferedReader br = new BufferedReader(new FileReader(new File("1.txt"))); BufferedWriter bw = new BufferedWriter(new FileWriter(new File("2.txt"))); //读写操做 //方式一:使用char[]数组 // char[] cbuf = new char[1024]; // int len; // while((len = br.read(cbuf)) != -1){ // bw.write(cbuf,0,len); // } //方式二:使用String String data; while((data = br.readLine()) != null){ //方法一: bw.write(data + "\n"); //data中不包含换行符(须要额外添加换行符) //方法二: bw.write(data); bw.newLine(); //至关于每写入一次就新建立一行("\n") } //关闭资源 bw.close(); br.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } /* flush()是在写入时用于刷新缓冲区的,程序默认缓冲区满了后才会写入内容,flush会强制让缓冲区输出内容 */
定义:转换流是字符流的一种,其提供字节流与字符流之间的转换
解码:字节,字节数组 → 字符数组,字符串
编码:字符数组,字符串 → 字节,字节数组
public class Test{ public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("1.txt"); //使用系统默认的字符集读取输入流 // InputStreamReader isr = new InputStreamReader(fis); //使用指定的字符集读取输入流 InputStreamReader isr = new InputStreamReader(fis,"UTF-8"); char[] cbuf = new char[20]; int len; while((len = isr.read(cbuf)) != -1){ String str = new String(cbuf,0,len); System.out.print(str); } isr.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
System.in:标准的输入流,默认从键盘输入,类型是InputStream
System.out:标准的输入流,默认从控制台输出,类型是PrintStream(OutputStream的子类)
System类的setIn(InputStream is) / setOut(PrintStream ps)方式从新指定输入和输出的流
//经过转化流与标准输出输入实现Scanner public class Test{ public static void main(String[] args) { InputStreamReader isr = new InputStreamReader(System.in); BufferedReader br = new BufferedReader(isr); while(true){ String data = null; try { data = br.readLine(); } catch (IOException e) { e.printStackTrace(); } if("test".equalsIgnoreCase(data)){ System.out.println("结束!"); break; } } } }
public class Test{ public static void main(String[] args) { PrintStream ps = null; FileOutputStream fos = null; try { fos = new FileOutputStream(new File("D:\\IO\\text.txt")); } catch (FileNotFoundException e) { e.printStackTrace(); } //建立打印输出流,设置为自动刷新模式(写入换行符的时候会刷新输出缓冲区) ps = new PrintStream(fos,true); if(ps != null){ System.setOut(ps); } for(int i = 0;i <= 255; i++){ System.out.print((char)i); } } } /* 这样子print的内容都会输出到指定的文件下,至关于重写了print */
public class Test{ public static void main(String[] args) { try { DataOutputStream dos = new DataOutputStream(new FileOutputStream("data.txt")); dos.writeUTF("HARRY"); dos.flush(); dos.writeInt(23); dos.flush(); dos.writeBoolean(true); dos.flush(); dos.close(); //读取不一样类型的数据的顺序要与当初写入文件时,保存的数据的顺序一致 DataInputStream dis = new DataInputStream(new FileInputStream("data.txt")); String name = dis.readUTF(); int age = dis.readInt(); boolean isMale = dis.readBoolean(); System.out.println(name); System.out.println(age); System.out.println(isMale); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
定义:ObjectInputStream和ObjectOutputStream用于存储和读取基本数据类型数据或对象的处理流,它能够把Java的对象写入到数据源中,也能把对象从数据源中还原回来
序列化:用ObjectOutputStream类保存基本数据类型或对象的机制
反序列化:用ObjectInputStream类读取基本数据类型或对象的机制
ObjectInputStream和ObjectOutputStream不能序列化static和transient修饰的成员变量
public class Test{ public static void main(String[] args) { //序列化:将内存中的java对象保存到磁盘中 //使用ObjectOutputStream实现 try { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("1.dat")); oos.writeObject(new String("TestTest")); oos.flush(); oos.close(); //反序列化:将磁盘文件中的对象还原为内存中的一个java对象 //使用ObjectInputStream实现 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.dat")); Object obj = ois.readObject(); String str = (String)obj; System.out.println(str); ois.close(); } catch (IOException | ClassNotFoundException e) { e.printStackTrace(); } } }
/* 自定义类须要知足如下要求,方可序列化 1.须要实现Serializable接口 2.当前类提供一个全局常量:serialVersionUID 3.除了当前类须要实现Serializable接口以外,还必须保证其内部全部属性均可以序列化 4.ObjectInputStream和ObjectOutputStream不能序列化static和transient修饰的成员变量 */ class Person implements Serializable{ public static final long serialVersionUID = 64846545648L; private String name; private int age; } /* serialVersionUID用来代表类的不一样版本,其目的是对序列化对象进行版本控制。 若是类没有显式定义这个静态常量,它的值是自动生成的。若类的实例变量作了修改,serialVersionUID可能发生变化(没法反序列化) */
定义:RandomAccessFile这个类实现了DataInput和DataOutput两个接口,意味着它既能够做为一个输入流也能够做为一个输出流。它支持"随机访问"的方式,程序能够直接跳到文件的任意地方来读,写文件
若是RandomAccess做为输出流时,写出到的文件若是不存在,则在执行过程当中自动建立。若是写出到的文件存在,则会对原有文件内容进行覆盖(默认状况下,从头覆盖)
有些相似C风格的读写方式
public class Test{ public static void main(String[] args) { try { RandomAccessFile raf1 = new RandomAccessFile(new File("1.jpg"),"r"); RandomAccessFile raf2 = new RandomAccessFile(new File("2.jpg"),"rw"); byte[] buffer = new byte[1024]; int len; while((len = raf1.read(buffer)) != -1){ raf2.write(buffer,0,len); } raf1.close(); raf2.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
public class Test{ public static void main(String[] args) { try { RandomAccessFile raf1 = new RandomAccessFile("hello.txt","rw"); raf1.seek(3);//将指针调到角标为3的位置 //保存指针3后面的全部数据到StringBuilder中 StringBuilder builder = new StringBuilder((int)new File("hello.txt").length()); byte[] buffer = new byte[20]; int len; while((len = raf1.read(buffer)) != -1){ builder.append(new String(buffer,0,len)); } //调回指针,写入xyz raf1.seek(3); raf1.write("xyz".getBytes()); //将StringBuilder中的数据写入到文件中 raf1.write(builder.toString().getBytes()); raf1.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }