File{
作用:将文件/文件夹封装成对象,可以将一个已存在或不存在的文件或目录封装成File对象
常用方法{
boolean creatNewFile() 该文件不存在,就创建该文件,返回true。如果文件存在,就不创建,返回false
boolean mkdir() 创建文件夹(目录)
boolean delete() 文件(文件夹)删除,如果文件夹里有内容的话,要先把内容删除该文件夹才能删除
list() 返回一个字符串数组,包含目录内容,所以对象中封装的必须是目录
}
例子
该例子用到递归:函数自身直接或间接地调用了自身/** * 对文件夹进行深度遍历,并用字符串打印出该文件内容 * * 步骤: * 1.获取一个文件夹对象,遍历内容 * 2.判断该文件是一个目录还是一个文件 * 3.是文件就将其存储到容器中 * 4.如果还是一个目录就继续遍历 */ public class BianLi { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("F:\\"); getFilesContent(file, 0); } private static void getFilesContent(File file, int count) { // TODO Auto-generated method stub System.out.println(getSpace(count) + file.getAbsolutePath()); count++; File[] list = file.listFiles(); for(File l : list){ if(l.isDirectory()){ getFilesContent(l, count); }else{ System.out.println(getSpace(count) + l.getAbsolutePath()); } } } private static String getSpace(int count) { // TODO Auto-generated method stub StringBuffer sb = new StringBuffer(); for (int i = 0; i < count; i++) { sb.append(" "); } return sb.toString(); } }
一个功能在被重复使用,并且每次使用时参与的运算结果与上一次调用有关,这时候就使用递归
注意:递归要明确条件,否则容易栈溢出(死递归,即一直在调用函数,不出栈)
}
Properties{
定义:用于操作以键值对形式存在的配置文件
特点{
}1.集合中键和值都是字符串,所以不需要指定泛型
2.集合中的数据可以保存在流中或从流中获取
常用方法{
Set stringPropertyNames()返回容器中键的信息,用Set集合存储。该方法是jdk1.6才出现的
void store(输出流, 描述)将集合的键值对写入输出流关联的文件中,描述信息在文件中以“#”开头,并且不允许写中文否则出现乱码
例子
<pre name="code" class="java"> File file = new File("F:\\a.txt"); BufferedWriter bw = new BufferedWriter( new FileWriter(file)); Properties p = new Properties(); p.put("name1", "21"); p.put("name2", "22"); p.put("name3", "23"); p.put("name4", "24"); p.store(bw, "asdf");
注意:
将输入流关联文件的内容传到集合中(必须保证该文件的数据是以键值对形式存在的)
加载的时候会将以“#‘开头的描述内容屏蔽
例子
File file = new File("F:\\a.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); Properties p = new Properties(); p.load(br); System.out.println(p);
/** * 实现Properties的load方法 * * 步骤: * 1.接收源,容器 * 2.创建缓冲流,接收文件中每一行的内容 * 3.屏蔽以“#”开头的内容 * 4.用split方法分割每行“=”两边的内容 * 5.将分割的内容分别作为容器的键和值存储 */ public static void myLoad(Reader reader, Properties p) throws IOException{ BufferedReader br = new BufferedReader(reader); String line = null; while((line=br.readLine()) != null){ if(line.startsWith("#")) continue; String[] s = line.split("="); p.put(s[0], s[1]); br.close(); } }
}
小练习{
/** * * 仅允许一个程序运行五次。 * 创建一个文件,内部有一个计数器,运行一次,计数器就自增一次,直至计数器为五就停止 * * 步骤: * 1.创建一个文件对象,作为包含计数器的文件 * 2.创建相应的输入流对象,获取文件的内容 * 3.创建容器存储文件内计数器的内容 * 4.修改计数器的值 * 5.创建输出流将文件中计数器的内容重写 * @throws IOException */ private static void OnlyRunFiveTimes() throws IOException { // TODO Auto-generated method stub File file = new File("f:\\a.txt"); if(!file.exists()) file.createNewFile(); BufferedReader br = new BufferedReader(new FileReader(file)); Properties pro = new Properties(); pro.load(br); String str = null; int count; if((str=pro.getProperty("count")) == null){ pro.put("count", "1"); }else{ count = Integer.parseInt(str); if(count == 5){ throw new RuntimeException("使用超过五次"); } count++; pro.setProperty("count", count+""); } BufferedWriter bw = new BufferedWriter(new FileWriter(file)); pro.store(bw, "count~~~~"); br.close(); bw.close(); }
}