学习目标
- File类
- File类的作用
- File类的常用功能
- 字节流
- IO流的作用及分类
- 字节流读写数据
- 字节缓冲流读写数据
- Properties集合
- Properties集合的介绍
- Properties集合和IO相关功能
- Properties集合集合流操作
递归
绝对路径:以盘符开始
-
1.1 构造方法
//File重写了toString,File将文件和目录封装成对象// 1 File(String pathname) 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例File file = new File("D:\\abc");// 2 File(String parent, String child) 从父路径名字符串和子路径名字符串创建新的 File实例File file1 = new File("D:\\abc","a.txt");//3 File(File parent, String child) 从父抽象路径名和子路径名字符串创建新的 File实例File file2 = new File(new File("D:\\abc"),"a.txt");
1.2 File类的创建和删除功能
//2 public boolean mkdir():创建一个单级文件夹File file = new File("day10_demo\\cc");//System.out.println(file.mkdir());//1 public boolean createNewFile(): 创建一个新的空的文件File file1 = new File("day10_demo\\b.txt");System.out.println(file1.createNewFile());//3 public boolean mkdirs() :创建一个多级文件夹File file2 = new File("day10_demo\\aa\\bb\\cc");//System.out.println(file2.mkdirs());
public boolean delete() 删除由此抽象路径名表示的文件或目录注意 :1 delete方法直接删除不走回收站。2 如果删除的是一个文件,直接删除。3 如果删除的是一个文件夹,空文件夹直接删掉 , 有内容文件夹需要先删除子文件
1.3 File类的判断和获取功能
File类判断和获取功能:public boolean isDirectory() 测试此抽象路径名表示的File是否为目录public boolean isFile() 测试此抽象路径名表示的File是否为文件public boolean exists() 测试此抽象路径名表示的File是否存在public String getAbsolutePath() 返回此抽象路径名的绝对路径名字符串public String getPath() 获取的是创建File对象给定的路径public String getName() 返回由此抽象路径名表示的文件或目录的名称
1.4 File类的高级获取功能
File file = new File("D:\\");//public File[] listFiles() 返回此抽象路径名表示的目录中的文件和目录的File对象数组File[] files = file.listFiles();for (File f : files) {System.out.println(f.getName());}listFiles方法注意事项:1 当调用者不存在时,返回null2 当调用者是一个文件时,返回null3 当调用者是一个空文件夹时,返回一个长度为0的数组4 当调用者是一个有内容的文件夹时,将里面所有文件和文件夹的路径放在File数组中返回 , 拿到儿子辈的文件 , 包含隐藏内容
2. IO流
2.1 IO流能解决什么问题?
在以往的学习中,我们通过变量,数组或者集合存储的数据,都是存储在内存中的,不能永久化存储,只要代码运行结束数据就会丢失,所以我们需要使用IO流,将数据写到文件中,实现数据永久化存储,还可以把文件中的数据读取到内存中。在网络中数据的传输。
2.2 IO流分类(以内存为中心)
按照流向区分
- 输入流 : 用来读数据
- 输出流 : 用来写数据
- 按照类型区分
- 字节流(可以操作任意文件)
- 字符流(只能操作纯文本文件,用windows记事本打开能读的懂,那么这样的文件就是纯文本文件)
2.3 字节流
2.3.1 字节输出流
```java //public FileOutputStream(File file) : 往file类型的路径中写入数据 File file = new File(“day10_demo\a.txt”); //1.创建字节输出流对象 FileOutputStream outputStream = new FileOutputStream(file); //public FileOutputStream(String name) : 往String类型的路径中写入数据 FileOutputStream outputStream = new FileOutputStream(“day10_demo\b.txt”);
// 1 void write(int b) 一次写一个字节数据//写入数据fos.write(99);fos.write(100);fos.write(101);//2 void write(byte[] b) 一次写一个字节数组数据//定义一个数组byte[] bys = {65, 66, 67, 68, 69};fos.write(bys);//3 void write(byte[] b, int off, int len) 一次写一个字节数组的部分数据byte[] bys = {65, 66, 67, 68, 69};fos.write(bys,1,2);//2代表读取长度//换行fos.write(97);//由于字节流无法写入一个字符串,把字符串转成字节数组写入fos.write("\r\n".getBytes());//追加数据 通过构造方法 : public FileOutputStream(String name,boolean append)
<a name="qj0n9"></a>#### 2.3.2 字节输入流```javapublic class FileInputStreamDemo7 {public static void main(String[] args) throws IOException {//创建输入流对象FileInputStream fis = new FileInputStream("D:\\ljh\\Pictures\\Saved Pictures\\均衡模式.jpg");//创建输出流对象FileOutputStream fos = new FileOutputStream("day10_demo\\copy.jpg");//创建一个数组byte[] bytes = new byte[1024];int len;//每次读到数据的个数//读数据while ((len= fis.read(bytes))!=-1){fos.write(bytes,0,len);//写数据}fis.close();fos.close();}}
2.3.3 字节缓冲流如何使用 ?
public class BufferedStreamDemo1 {public static void main(String[] args) throws IOException {//创建高效的字节输入流BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\ljh\\Pictures\\Saved Pictures\\均衡模式.jpg"));//创建高效的字节输出流BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day10_demo\\b.jpg"));//一次读一个一个字节/* int by;while ((by=bis.read())!=-1){bos.write(by);}*///一次读一个数组byte[] bytes = new byte[1024];int len;//每次真实读到数据的个数while ((len=bis.read(bytes))!=-1){bos.write(bytes,0,len);}//释放资源bis.close();bos.close();}}
3. Properties集合
3.1 Properties集合特有的方法 ?
public class PropertiesDemo2 {public static void main(String[] args) {Properties p = new Properties();//Object setProperty(String key, String value) 设置集合的键和值,都是String类型,相当于put方法p.setProperty("aa","11");p.setProperty("bb","22");p.setProperty("cc","33");//Set<String> stringPropertyNames() 从该属性列表中返回一个不可修改的键集,其中键及其对应的值是字符串 , 相当于keySet方法Set<String> stringSet = p.stringPropertyNames();for (String key : stringSet) {//String getProperty(String key) 使用此属性列表中指定的键搜索属性 , 相当于get方法Object value = p.getProperty(key);System.out.println(key+":"+value);}}}
3.2 Properties集合和IO相关的功能 ?
public class PropertiesDemo3 {public static void main(String[] args) throws IOException {//创建集合对象Properties p = new Properties();p.setProperty("aa","11");p.setProperty("bb","22");p.setProperty("cc","33");//void store(OutputStream out, String comments) 把集合中的键值对,以字节流形式写入文件中 , 参数二为注释p.store(new FileOutputStream("day10_demo\\a.txt"),"AAA");//void load(InputStream inStream) : 以字节流形式 , 把文件中的键值对, 读取到集合中p.load(new FileInputStream("day10_demo\\a.txt"));System.out.println(p);}}
4. ResourceBundle加载属性文件
public class ResourceBundleDemo {public static void main(String[] args) {//1.属性集名称不含扩展名。//2.属性集文件是在src目录中的//ResourceBundle bundle = ResourceBundle.getBundle("user");//通过键,获取对应的值String key = bundle.getString("username");String value = bundle.getString("password");System.out.println(key);System.out.println(value);}}
