首页 > 代码库 > javase(9)_java io系统

javase(9)_java io系统

一、File类

1、file既可以代表一个特定文件的名称,又可以代表一个目录下的一组文件的名称,实际上,FilePath对这个类来说是个更好的名字.
2、目录列表器例:

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;
import java.util.regex.Pattern;

public class DirList {
    public static void main(String[] args) {
        File path = new File("./data/baiduvideo/");
        String[] list;
        if (args.length == 0)
            list = path.list();
        else {
            list = path.list(new DirFilter(args[0]));
        }
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);//按字母排序
        for(String dirItem : list)
            System.out.println(dirItem);
    }
}
class DirFilter implements FilenameFilter {
    private Pattern pattern;
    public DirFilter(String regex){
        pattern = Pattern.compile(regex);
    }
    @Override
    public boolean accept(File dir, String name) {
        return pattern.matcher(name).matches();
    }
}
//这是list方法的内部实现,策略设计模式
/*public String[] list(FilenameFilter filter) {
    String names[] = list();
    if ((names == null) || (filter == null)) {
        return names;
    }
    ArrayList v = new ArrayList();
    for (int i = 0 ; i < names.length ; i++) {
        if (filter.accept(this, names[i])) {
        v.add(names[i]);
    }
}
    return (String[])(v.toArray(new String[v.size()]));
}*/
//匿名内部类改写,可读性不好
public class DirList {
    public static void main(final String[] args) {
        File path = new File("./data/baiduvideo/");
        String[] list;
        if (args.length == 0)
            list = path.list();
        else {
            list = path.list(new FilenameFilter() {
                private Pattern pattern = Pattern.compile(args[0]);
                @Override
                public boolean accept(File dir, String name) {
                    return pattern.matcher(name).matches();
                }
            });
        }
        Arrays.sort(list,String.CASE_INSENSITIVE_ORDER);//按字母排序
        for(String dirItem : list)
            System.out.println(dirItem);
    }
}

File类其它常见用法略.

二、io基本使用

package testio;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;

public class BufferedInputFile {
    //1、缓冲输入文件
    public static String read(String filename) throws IOException{
        //BufferedReader目的是为了提高性能
        BufferedReader in = new BufferedReader(new FileReader(filename));
        String s;
        StringBuilder sb = new StringBuilder();
        while((s=in.readLine())!=null)
            sb.append(s);
            sb.append("\n");//readLine已经将换行符删了
        in.close();
        return sb.toString();
    }
    public static void main(String[] args) throws IOException {
        System.out.println(read("E:/eclipse/testnginx/src/testio/BufferedInputFile.java"));
    }
}
public class MemoryInput {
    //2、从内存输入
    public static void main(String[] args) throws IOException {
        StringReader in = new StringReader(
                BufferedInputFile
                        .read("E:/eclipse/testnginx/src/testio/MemoryInput.java"));
        int c;
        while ((c = in.read()) != -1)
            System.out.print((char) c);
    }
}
public class FormattedMemoryInput {
    //3、格式化内存输入--按字节读取字符,防止出现乱码
    public static void main(String[] args) throws IOException {
        DataInputStream in = new DataInputStream(new ByteArrayInputStream(
                BufferedInputFile.read(
                        "E:/eclipse/testnginx/src/testio/MemoryInput.java")
                        .getBytes()));
        while(true)
            System.out.print((char)in.readByte());
    }
}
public class BasicFileOutput {
    //1、基本文本输出,为了提供格式化机制,使用PrintWriter
    /*static String file = "BasicFileOutput.out";
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new FileReader(
                "E://eclipse//testnginx//src//testio//BasicFileOutput.java"));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                file)));
        String s;
        int lineCount = 1;
        while ((s = in.readLine()) != null)
            out.println(lineCount++ + ":" + s);
        out.close();
        in.close();
    }*/
    //方式2从内存输入
    static String file = "BasicFileOutput.out";
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new StringReader(
                BufferedInputFile.read("E:/eclipse/testnginx/src/testio/BasicFileOutput.java")));
        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
                file)));
        //PrintWriter out = new PrintWriter(file); //PrintWriter的一种快捷方式
        String s;
        int lineCount = 1;
        while ((s = in.readLine()) != null)
            out.println(lineCount++ + ":" + s);
        out.close();
        in.close();
    }
}

 

javase(9)_java io系统