A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 周发建 中级黑马   /  2016-3-28 09:45  /  416 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

[size=16.0000pt](一)练习:深度遍历文件写入到指定文件中
练习2:将指定路径下的文件深度遍历记录到文件中
        /**练习2:将指定路径下的文件深度遍历记录到文件中
         * 1. 深度遍历
         * 2. 过滤器,将符合条件的存储到容器中
         * 3. 对容器中的内容进行遍历并将绝对路径写入文件中
         * 3. 存储到集合中[内存]
         * 4. 持久化到文件
         */
        public static void writeByFile(List<File> list,File dest){
                BufferedWriter bufw = null;
                try {
                        bufw = new BufferedWriter(new FileWriter(dest));
                        if(list != null)
                                for(File file: list){
                                        bufw.write(file.getAbsolutePath());
                                        bufw.newLine();
                                        bufw.flush();
                                }
                        bufw.close();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
        public static void lookFileAllByFile(File dir,List<File> list){
                File[] files = dir.listFiles(new Filter());
                if(files!=null)
                        for(File file: files){
                                //目录
                                if(file.isDirectory()){
                                        lookFileAllByFile(file,list);
                                }
                                //文件
                                else{
                                        list.add(file);
                                }
                        }
        }
[size=16.0000pt](二)其他流对象
        PrintWriter/PrintStream        //打印流,可以直接操作文件和流
        SequenceInputStream                //序列流,对多个流进行合并
SequenceInputStream(Enumeration<? extends InputStream> e)
SequenceInputStream(InputStream s1, InputStream s2)
        ObjectInputStream/ObjectOutputStream       
操作对象的流,被操作的对象需要实现Serializable接口
        将对象写入磁盘,只能将非静态和非瞬态。
        transient        瞬态  //private transient String name;
        static                 静态  //private static String name;
        RandomAccessFile        随机访问文件,自身具备读写方法
管道流,输入输出可以直接连接、结合线程使用
        PipedInputStream/PipedOutputStream
           内存中的流       
        DataInputStream/DataOutputStream        操作基本数据类型
        ByteArrayInputStream/ByteOutputStream        操作字节数组
        CharArrayReader/CharArrayWriter                操作字符数组
        StringReader/StringWriter                操作字符串
[size=16.0000pt](三)练习:分割、合并、配置文件
        /**
         * 练习:文件切割、合并、配置文件
         * 1. 切割
         * 2. 配置文件记录切割文件的名称和切割次数
         * 3. 读取配置文件并合并
         */
        //分割
        public static void partition(File file,Properties pro) throws IOException{
                if(file!=null){
                        pro.setProperty("fileName", file.getName());
                        InputStream in = new FileInputStream(file);
                        String dir = "d:/zhoujian/io";
                        pro.setProperty("path", dir);
                        File path = new File(dir);
                        if(!path.exists()){
                                path.mkdirs();
                        }
                        OutputStream out = null;
                        byte[] buf = new byte[1024*1024];
                        int len = 0;
                        int i = 1;
                        while((len = in.read(buf))!=-1){
                                File temp = new File(dir,i++ + ".part");
                                if(!temp.exists()){
                                        temp.createNewFile();
                                }
                                out = new FileOutputStream(temp);
                                out.write(buf, 0, len);
                                out.flush();
                                out.close();
                        }
                        pro.setProperty("num", i+"");
                        in.close();
                }
        }
        //合并
        public static void merge(File dir,Properties pro) throws IOException{
                if(!dir.exists()){
                        dir.mkdirs();
                }
                String fileName = pro.getProperty("fileName");
                File file = new File(dir, fileName);
                if(!file.exists()){
                        file.createNewFile();
                }
                String path = pro.getProperty("path");
                int num = Integer.parseInt(pro.getProperty("num"));
                List<InputStream> list = new ArrayList<InputStream>();
                for(int i=1;i<num;i++){
                        File temp = new File(path,i+".part");
                        list.add(new FileInputStream(temp));
                }
                Enumeration<InputStream> es = Collections.enumeration(list);
                SequenceInputStream sin = new SequenceInputStream(es);
                OutputStream out = new FileOutputStream(file);
                byte[] buf = new byte[1024*1024];
                int len = 0;
                while((len = sin.read(buf))!=-1){
                        out.write(buf, 0, len);
                        out.flush();
                }
                out.close();
                sin.close();
}
[size=16.0000pt](四)按字节截取字符串
file:///C:/Users/ADMINI~1/AppData/Local/Temp/ksohtml/wpsE064.tmp.jpg
        /**
         * 练习:按字节截取字符串
         * 字符串,汉字的都是负数
         * 1. 判断负数出现的次数,偶数个不舍,奇数个舍弃
         * 2. 例如:        ab你
         *                         a                97
         *                         ab                97 98
         *                         ab                97 98
         *                         ab你                97 98 -69 -20
         */
        public static String getStrNumByByte(String str,int len) throws IOException{
                byte[] buf = str.getBytes("gbk");
                if(len> buf.length){
                        len = buf.length;
                }
                int count = 0;
                for(int i=len-1;i>=0;i--){
                        if(buf<0){
                                count ++;
                        }
                        else{
                                break;
                        }
                }
                if(count %2 ==0){
                        return new String(buf,0,len,"gbk");
                }else{
                        return new String(buf,0,len-1,"gbk");
                }
}

0 个回复

您需要登录后才可以回帖 登录 | 加入黑马