zoukankan      html  css  js  c++  java
  • 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("
    ");//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();
        }
    }

     

  • 相关阅读:
    SOAP webserivce 和 RESTful webservice 对比及区别(转载)
    JavaWeb工程中web.xml基本配置(转载学习)
    iframe 自适应
    SQL分组求每组最大值问题的解决方法收集 (转载)
    关于试用jquery的jsonp实现ajax跨域请求数据的问题
    解决Mysql连接池被关闭 ,hibernate尝试连接不能连接的问题。 (默认mysql连接池可以访问的时间为8小时,如果超过8小时没有连接,mysql会自动关闭连接池。系统发布第二天访问链接关闭问题。
    Hadoop编译源码(面试重点)
    Hadoop学习(二)自己编译Hadoop安装包
    代理模式实现方式及优缺点对比
    zookeeper
  • 原文地址:https://www.cnblogs.com/wangweiNB/p/5977165.html
Copyright © 2011-2022 走看看