Java的I/O系统

该文章来自我的个人博客:

Java的I/O系统

FilenameFilter

文件过滤器,获取指定的文件

import java.io.File;
import java.io.FilenameFilter;
import java.util.regex.Pattern;
public class Filenamer {
    public static void main(String...args){
        File path=new File(".");
        String[] files;
        if(args.length==0){
            files=path.list();
        }else{
            files=path.list(new DirFilter(args[0]));
        }
        if(files==null){
            return;
        }
        for(String file:files){
            System.out.println("file:"+file);
        }
    }
}
class DirFilter implements FilenameFilter {
    private Pattern pattern;
    public DirFilter(String regex){
        pattern=Pattern.compile(regex);
    }
    @Override
    public boolean accept(File pathname,String name) {
        return pattern.matcher(name).matches();
    }
}

DirFilter重写了FilenameFilter的accept方法根据传入的正则判断出符合条件的文件名,符合返回true,不符合返回false

accept方法供list方法调用,list方法源码如下:

public String[] list(FilenameFilter filter) {
    String names[] = list();
    if ((names == null) || (filter == null)) {
        return names;
    }
    List v = new ArrayList<>();
    for (int i = 0 ; i < names.length ; i++) {
        if (filter.accept(this, names[i])) {
            v.add(names[i]);
        }
    }
    return v.toArray(new String[v.size()]);
}

输入

读取文件到缓存

BufferedReader in=new BufferedReader(new FileReader(fileName));
while((str=in.readLine())!=null){
  strs.append(str);
}

读取字符串到内存

StringReader in=new StringReader(str);
//in。read返回字符的Ascall码,内容读取完则返回-1
while((c=in.read())!=-1){
  System.out.println((char)c);
}

读取字节到内存

//new ByteArrayInputStream()将字节数组转换成字节流
DataInputStream in=new DataInputStream(new ByteArrayInputStream(str.getBytes());
//in.readByte()返回下一个字节,当已经到达末尾,会抛出EOFException异常
while(in.available()!=0){
  System.out.println((char)in.readByte())
}

输出

写内容到文件

PrintWriter out=new PrintWriter(new BufferedWriter(new FileWriter(fileName)));
out.println(str);

存储和恢复数据

try {
    DataOutputStream out=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("test.txt")));
    out.writeChar('c');
    out.writeBoolean(true);
    out.writeDouble(12.3222222);
    out.writeUTF("str");
    out.close();
    DataInputStream in=new DataInputStream(new BufferedInputStream(new FileInputStream("test.txt")));
    //注意读取有顺序区分
    System.out.println(in.readChar()+" "+in.readBoolean()+" "+in.readDouble()+" "+in.readUTF());
    in.close();
} catch (IOException e) {
    e.printStackTrace();
}

c true 12.3222222 str

随机存储访问数据

String fileName="test.txt";
try {
    RandomAccessFile out=new RandomAccessFile(fileName,"rw");
    out.writeDouble(0.0);
    out.writeDouble(0.1);
    out.writeDouble(0.2);
    out.writeDouble(0.3);
    out.writeDouble(0.4);
    out.close();
    RandomAccessFile in=new RandomAccessFile(fileName,"rw");
    System.out.println(in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" ");
    //in.close();
    out=new RandomAccessFile(fileName,"rw");
    //一个double数值为8字节
    out.seek(8);
    out.writeDouble(1.0);
    out.close();
    //in=new RandomAccessFile(fileName,"r");
    //回到开始位置,重新读取数据
    in.seek(0);
    System.out.println(in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" "+in.readDouble()+" ");
    in.close();
}catch (IOException e){
    e.printStackTrace();
}

0.0 0.1 0.2 0.3 0.4
0.0 1.0 0.2 0.3 0.4

你可能感兴趣的:(Java,IO,InputStream,OutputStream,流)