1 深度遍历文件夹
package java20;
import java.io.File;
import java.io.FilenameFilter;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* 2017/10/14
* 说明:
*/
public class FileDemo {
public static void main(String[] args) {
File dir = new File("D:\\code");
listAll(dir);
}
/**
* 深度遍历文件夹
* @param dir
*/
private static void listAll(File dir) {
System.out.println("文件夹:"+dir.getAbsolutePath());
File[] files = dir.listFiles();
for(File file : files){
if(file.isDirectory()){
listAll(file);
}else{
System.out.println("文件:"+file.getAbsolutePath());
}
}
}
}
2 Properties
2.1 Properties的特点
- 该集合中的键和值都是字符串类型的。
- 集合中的数据可以保存在流中,或者从流中获取。
2.2 构造方法
2.3 常用方法
public Object setProperty(String key,String value)
public String getProperty(String key)
public Enumeration<?> propertyNames()
public Set<String> stringPropertyNames()
package java20;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* 2017/10/15
* 说明:
*/
public class PropertiesDemo {
public static void main(String[] args) {
Properties pros = new Properties();
pros.setProperty("zhangsan","20");
pros.setProperty("lisi","26");
pros.setProperty("wangwu","27");
//第一种方式获取所有的键值
Enumeration<?> enumeration = pros.propertyNames();
while(enumeration.hasMoreElements()){
String name = (String) enumeration.nextElement();
String value = pros.getProperty(name);
System.out.println(name+":"+value);
}
System.out.println("------------------------------");
//第二种方式获取所有的键值
Set<String> names = pros.stringPropertyNames();
for(String name:names){
String value = pros.getProperty(name);
System.out.println(name+":"+value);
}
}
}
public void store(OutputStream out,String comments) throws IOException
public void store(Writer writer,String comments) throws IOException
package java20;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* 2017/10/15
* 说明:
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
Properties pros = new Properties();
pros.setProperty("zhangsan","20");
pros.setProperty("lisi","26");
pros.setProperty("wangwu","27");
FileWriter fs = new FileWriter("pros.properties");
pros.store(fs,"properties文件");
fs.close();
}
}
public void load(InputStream inStream) throws IOException
public void load(Reader reader) throws IOException
package java20;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* 2017/10/15
* 说明:
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
Properties pros = new Properties();
FileReader fr = new FileReader("pros.properties");
pros.load(fr);
Set<String> names = pros.stringPropertyNames();
for(String name:names){
String value = pros.getProperty(name);
System.out.println(name+":"+value);
}
}
}
package java20;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* 2017/10/15
* 说明:
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
Properties pros = new Properties();
FileReader fr = new FileReader("pros.properties");
pros.load(fr);
pros.setProperty("zhangsan","60");
FileWriter fw = new FileWriter("pros.properties");
pros.store(fw,"修改属性文件");
fw.close();
fr.close();
}
}
- 示例:获取一个应用程序运行的次数,如果超过5次,给出使用次数并体术请注册的信息,并不要运行程序。
package java20;
import java.io.*;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Set;
/**
* 2017/10/15
* 说明:
*/
public class PropertiesDemo {
public static void main(String[] args) throws IOException {
//将所需要的配置文件封装成File对象
File config = new File("count.properties");
if(!config.exists()){
config.createNewFile();
}
FileInputStream fis = new FileInputStream(config);
Properties pros = new Properties();
pros.load(fis);
//从集合中通过键获取次数
String times = pros.getProperty("times");
int count = 0;
if(times != null){
count = Integer.parseInt(times);
if(count >= 5){
System.out.println("使用次数已到,请注册");
return;
}
}
count ++;
//将改变的后的次数重新存储到集合中
pros.setProperty("times",String.valueOf(count));
FileOutputStream fos = new FileOutputStream(config);
pros.store(fos,"");
fos.close();
fis.close();
}
}
3 打印流
3.1 PrintStream
3.1.1 PrintStream简介
- PrintStream为其它输出流添加了功能,使得它们能够方便的打印各种数据值的表现形式。它还提供了其它两项功能。与其它输出流不同,PrintStream永远不会抛出IOException。
- 异常情况仅设置可通过checkError方法测试的内部标志。
- 为了自动刷新,可以创建一个PrintStream,这意味着可在写入byte数组之后调用flush方法,可调用其中一个println方法,或写入一个换行符或字节('\n')。
3.1.2 构造方法
public PrintStream(File file) throws FileNotFoundException
- 创建具有指定文件名称和字符集且不带自动刷新的打印流
public PrintStream(File file,String csn) throws FileNotFoundException UnsupportedEncodingException
public PrintStream(OutputStream out)
public PrintStream(OutputStream out, boolean autoFlush)
public PrintStream(OutputStream out,boolean autoFlush,String encoding) throws UnsupportedEncodingException
public PrintStream(String fileName) throws FileNotFoundException
public PrintStream(String fileName,String csn) throws FileNotFoundException, UnsupportedEncodingException
3.1.3 方法
public PrintStream append(char c)
public PrintStream append(CharSequence csq)
public PrintStream append(CharSequence csq, int start, int end)
public boolean checkError()
protected void clearError()
- 使用指定格式字符串和参数将格式化字符串写入到此输出流中
public PrintStream format(Locale l,String format, Object... args)
- 使用指定格式字符串和参数将格式化字符串写入到此输出流中
public PrintStream format(String format, Object... args)
public void print(boolean b)
public void print(char c)
public void print(char[] s)
public void print(double d)
public void print(double d)
public void print(long l)
public void print(Object obj)
public void print(String s)
- 使用指定格式字符串和参数将格式化的字符串写入此wirter的便捷方法
public PrintWriter printf(Locale l,String format,Object... args)
- 使用指定格式字符串和参数将格式的字符串写入此writer的便捷方法
public PrintWriter printf(String format,Object... args)
public void println(boolean x)
public void println(char x)
public void println(char[] x)
public void println(double x)
public void println(float x)
public void println(int x)
public void println(long x)
public void println(Object x)
public void println(String x)
protected void setError()
public void write(char[] buf)
public void write(char[] buf, int off,int len)
public void write(String s)
public void write(String s,int off,int len)
3.1.4 示例:
package java20;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;
/**
* 2017/10/15
* 说明:
*/
public class PrintStreamDemo {
public static void main(String[] args) throws FileNotFoundException {
PrintStream ps = new PrintStream(new FileOutputStream("aaa.txt"),true);
ps.print(1);
ps.print('a');
ps.println("你好啊");
ps.println("你好啊");
ps.flush();
ps.close();
}
}
3.2 PrintWriter
3.2.1 PrintWriter简介
- 向文本输出流打印对象的格式化表示形式,此类实现了在PrintStream的所有print方法,它不包含用于写入原始字节的方法,对于这些字节,程序应该使用未编码的字节流进行写入。
- 如果启用了自动刷新,则只有调用println、printf或format的其中一个方法时才可能完成此操作,而不是每当正好输出换行符的时候才完成。这些方法使用平台自有的行分隔符概念,而不是换行符。
- 此类中的方法不会抛出IO异常,尽管其某些构造方法可能会抛出异常。
3.2.2 构造方法
- 使用指定文件创建不具有自动刷新的PrintWriter
public PrintWriter(File file) throws FileNotFoundException
- 创建具有指定文件和字符集且不带有自动刷新的PrintWriter
public PrintWriter(File file,String csn) throws FileNotFoundException, UnsupportedEncodingException
- 根据现有的OutputStream创建不带有自动刷新的PrintWriter
public PrintWriter(OutputStream out)
- 根据现有的OutputStream创建PrintWriter
public PrintWriter(OutputStream out,boolean autoFlush)
- 创建具有指定文件名且不带有自动刷新的PrintWriter
public PrintWriter(String fileName) throws FileNotFoundException
- 创建具有制定文件名称和字符集且不带有自动刷新的PrintWriter
public PrintWriter(String fileName,String csn) throws FileNotFoundException, UnsupportedEncodingException
public PrintWriter(Writer out)
public PrintWriter(Writer out, boolean autoFlush)
3.2.3 方法
public PrintWriter append(char c)
public PrintWriter append(CharSequence csq)
public PrintWriter append(CharSequence csq, int start, int end)
public boolean checkError()
protected void clearError()
- 使用指定格式的字符串和参数将一个格式化字符串写入此writer中
public PrintWriter format(Locale l,String format,Object... args)
- 使用指定格式的字符串和参数将一个格式化字符串写入此writer中
public PrintWriter format(String format,Object... args)
public void print(boolean b)
public void print(char c)
public void print(char[] s)
public void print(double d)
public void print(float f)
public void print(long l)
public void print(Object obj)
public void print(String s)
- 使用指定格式字符串和参数将格式化的字符串写入此writer的便捷方法
public PrintWriter printf(Locale l,String format, Object... args)
- 使用指定格式字符串和参数将格式化的字符串写入此writer的便捷方法
public PrintWriter printf(String format,Object... args)
public void println(boolean x)
public void println(char x)
public void println(char[] x)
public void println(double x)
public void println(float x)
public void println(int x)
public void println(long x)
public void println(Object x)
public void println(String x)
protected void setError()
public void write(char[] buf)
public void write(char[] buf,int off, int len)
public void write(String s)
public void write(String s, int off, int len)
4 SequenceInputStream 序列流
4.1 SequenceInputStream简介
- SequenceInputStream表示其他输入流的逻辑串联,它从输入流的有序集合开始,并从第一个输入流开始读取,直到达文件末尾,接着从第二个输入流读取,依次类推,直到达到包含的最后一个输入流的文件末尾处为止。
4.2 构造方法
- 通过参数来初始化新建SequenceInputStream,该参数必须是生成运行时类型为InputStream对象的Enumeration类型参数
public SequenceInputStream(Enumeration<? extends InputStream> e)
- 通过参数来初始化新建SequenceInputStream(将按顺序读取这两个参数,先读取s1,然后读取s2),以提供此SequenceInputStream读取的字节
public SequenceInputStream(InputStream s1, InputStream s2)
4.3 方法
public void close() throws IOException
public int read() throws IOException
public int read(byte[] b, int off, int len) throws IOException
4.4 示例
package java20;
import java.io.*;
import java.util.Vector;
/**
* 2017/10/15
* 说明:
*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
Vector<InputStream> vector = new Vector<>();
vector.add(new FileInputStream("test1.txt"));
vector.add(new FileInputStream("test2.txt"));
vector.add(new FileInputStream("test3.txt"));
SequenceInputStream si = new SequenceInputStream(vector.elements());
byte[] buffer = new byte[1024];
int len = 0;
while((len = si.read(buffer)) != -1){
System.out.println(new String(buffer,0,len));
}
}
}
package java20;
import java.io.*;
import java.util.*;
/**
* 2017/10/15
* 说明:
*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
List<InputStream> list = new ArrayList<>();
list.add(new FileInputStream("test1.txt"));
list.add(new FileInputStream("test2.txt"));
list.add(new FileInputStream("test3.txt"));
Iterator<InputStream> iterator = list.iterator();
Enumeration<InputStream> enumerations = new Enumeration<InputStream>() {
@Override
public boolean hasMoreElements() {
return iterator.hasNext();
}
@Override
public InputStream nextElement() {
return iterator.next();
}
};
SequenceInputStream si = new SequenceInputStream(enumerations);
byte[] buffer = new byte[1024];
int len = 0;
while((len = si.read(buffer)) != -1){
System.out.println(new String(buffer,0,len));
}
}
}
package java20;
import java.io.*;
import java.util.*;
/**
* 2017/10/15
* 说明:
*/
public class SequenceInputStreamDemo {
public static void main(String[] args) throws IOException {
List<InputStream> list = new ArrayList<>();
list.add(new FileInputStream("test1.txt"));
list.add(new FileInputStream("test2.txt"));
list.add(new FileInputStream("test3.txt"));
Enumeration<InputStream> enumerations = Collections.enumeration(list);
SequenceInputStream si = new SequenceInputStream(enumerations);
byte[] buffer = new byte[1024];
int len = 0;
while((len = si.read(buffer)) != -1){
System.out.println(new String(buffer,0,len));
}
}
}