File类
深度遍历文件夹
File dir=new File();
list All(dir);
File[] files=files[x].listFiles();
递归
函数自身直接或者间接调用了自身
一个功能重复被使用,并且每次使用时,参与运算的结果和上一次调用有关,这时可用递归
注意:
1.递归一定明确条件,否则容易出现栈溢出
2.注意递归的次数
删除目录
原理:必须从最里面开始往外删除
需要进行深度遍历
Properties集合
基本功能:持久的属性集
特点:1.该集合中的键和值都是字符串类型
2.集合中的数据可以保存到流中,或者从流中获取
通常该集合用于操作以键值对形式存在的配置文件
集合的存和取
创建集合 Properties prop=new Properties()
存储元素 prop.SetProperty();
package cn.itcast.io.p2.properties;
import java.util.Properties;
import java.util.Set;
public class PropertiesDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
}
public static void propertiesDemo() {
//创建一个Properties集合
Properties prop=new Properties();
//储存元素
prop.setProperty("zhangsan", "30");
prop.setProperty("lisi", "31");
prop.setProperty("wangwu", "36");
prop.setProperty("zhaoliu", "20");
//取出所有元素
Set<String> names=prop.stringPropertyNames();
for(String name:names) {
String value=prop.getProperty(name);
System.out.println(name+":"+value);
}
}
}
list方法
store方法
修改配置信息
package cn.itcast.io.p2.properties;
//Properties集合练习
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import javax.management.RuntimeErrorException;
public class PropertiesTest {
public static void main(String[] args) throws IOException {
getAppCount();
}
public static void getAppCount() throws IOException {
//将配置文件封装成File对象
File confile=new File("count.properties");
if(!confile.exists()) {
confile.createNewFile();
}
FileInputStream fis=new FileInputStream(confile);
Properties prop=new Properties();
prop.load(fis);
//从集合中通过建获取次数
String value=prop.getProperty("time");
//定义计数器,记录获取到的次数
int count=0;
if(value!=null) {
count=Integer.parseInt(value);
if(count>=5) {
//System.out.println("使用次数已到,请注册,给钱");
// return;
throw new RuntimeException("使用次数已到,请注册,给钱") ;
}
}
count++;
//将改变后的次数重新存储到集合中
prop.setProperty("time", count+"");
FileOutputStream fos=new FileOutputStream(confile);
prop.store(fos, "");
fos.close();
fis.close();
}
}
集合中的数据来自一个文件
注意:必须要保证该文件中的数据是键值对
需要使用到读取流
FileInputStream fis=new FileInputStream("info.txt");
//使用load方法
prop.load(fis);
prop.list(System.out);
打印流
PrintWriter 与 PrintStream
PrintStream
- 提供多种打印,可以多种数据类型值进行打印,并保持数据的表示形式
- 他不抛IO异常
构造函数接收三种类型的值:
1。字符串路径
2.File对象
3.字节输出流
PrintWriter
package cn.itcast.io.p2.properties;
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class PrintWriterDemo {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader bufr=new BufferedReader(new InputStreamReader(System. in));
PrintWriter out =new PrintWriter(new FileWriter("out.txt"),true);
String line=null;
while((line=bufr.readLine())!=null) {
if("over".equals(line))
break;
out.println(line.toUpperCase());
}
out.close();
bufr.close();
}
}
构造函数参数
- 字符串路径
- File对象
- 字节输出流
- 字符输出流
序列流
对多个流进行合并
/*序列流:将多个流进行逻辑串联(合并成为一个流,使操作变得更加方便,因为多个源变成了一个源)
*SequenceInputStream sis=new SequenceInputStream(InputStream s1, InputStream s2) ;
*当要合并三个或三个以上的流时,需要使用枚举作为参数
*SequenceInputStream sis=new SequenceInputStream(Enumeration<? extends InputStream> e);
*/
@Test//把三个文件中的数据写入到一个文件中
public void sequenceInputStreamDemo() throws IOException{
InputStream is1=new FileInputStream("1.txt");
InputStream is2=new FileInputStream("2.txt");
InputStream is3=new FileInputStream("3.txt");
Collection<InputStream> col=new ArrayList<InputStream>();
col.add(is1);
col.add(is2);
col.add(is3);
//import java.util.Collections;这个包中有很多操作集合的方法
Enumeration<InputStream> e = Collections.enumeration(col);
//把is1,is2,is3三个流合并成为了一个流sis
SequenceInputStream sis=new SequenceInputStream(e);
OutputStream is=new FileOutputStream("seq.txt");
int len=0;
byte b[]=new byte[4];
while((len=sis.read(b))!=-1){ //先把合并流中的数据读取到字节数组b中
is.write(b,0,len); //在把字节数组中的数据写入到seq文件夹
}
}