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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© wws1214 中级黑马   /  2015-7-17 13:38  /  402 人查看  /  0 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文



1、file
        目录分割符:File.separator
        1)创建
        boolean createNewFile() ;在指定位置创建新文件,如果有同名文件的话,就不创建
        createTempFile(str,str,dir)

        mkdir() 创建文件夹
        mkdirs() 创建多级文件夹
        2)删除
        boolean delete()    删除失败返回false
        void deleteOnExit() 在程序退出后删除
        3)判断
        boolean exists()  文件是否存在
        boolean isFile()
        boolean isDirectory()
        boolean isHidden()
        boolean isAbsolutePath()是否是绝对
        4)获取信息
        getName()
        getPath()
        getParent()

        getAbsolutePath()
        lastModified()
        length()
        rename()  修改文件名

        在判断文件对象是否是文件或者目的时,
        必须要先判断该文件对象封装的内容是否存在
        =》通过exists判断。

2、list()获取当前目录下,所以文件,包括隐藏文件
        1)string[] names=f.list();
          for(string name:names)
                s.o.p(name)
        2)FilenameFilter,用来筛选目录

                File dir=new File("E:\\java_heima");
               
                String[] names=dir.list(new FilenameFilter() {
                       
                        public boolean accept(File dir, String name) {
                                return name.endsWith(".java");
                        }
                });
               
                for(String name:names)
                        System.out.println(name);

        3)listFile  返回是一组对象,可以通过对象获取其属性
例                File dir=new File("E:\\java_heima");
                File[] files=dir.listFiles();
               
                for(File f:files)
                        System.out.println(f.getName()+"==="+f.length());

        4)需求:列出指定文件目录下文件或文件夹,包括子目录中的内容

                File dir=new File("E:\\java_heima");
                showDir(dir);
        }
        public static void showDir(File f) {
                System.out.println(f);
               
                File[] files=f.listFiles();
                for(File file:files)
                {
                        if(file.isDirectory())
                                showDir(file);
                        else {
                                System.out.println(file);
                        }
                }
        }

3、删除一个带内容的目录(从里往外删)
例                File f=new File("E:\\java_heima\\WANG");
                removeFile(f);
        }
        public static void  removeFile(File f) {
                File[] files=f.listFiles();
                for(File file:files)
                {
                        if(file.isDirectory())
                                removeFile(file);
                        else {
                                System.out.println(file.toString()+"=="+file.delete());
                        }
                               
                }
                System.out.println(f+"=="+f.delete());
        }

4、练习
        public static void writeToFile(List<File> list,String javaListFile)throws IOException
        {
                BufferedWriter bufw=null;
                try {
                        bufw=new BufferedWriter(new FileWriter(javaListFile));
                        for(File f:list)
                        {
                                String path=f.getAbsolutePath();
                                bufw.write(path);
                                bufw.newLine();
                                bufw.flush();
                        }
                } catch (IOException e) {
                        // TODO: handle exception
                        throw e;
                }
                finally
                {
                        try {
                                if(bufw!=null)
                                        bufw.close();
                        } catch (IOException e) {
                                // TODO: handle exception
                                throw e;
                        }
                }
        }

5、Properties是hashtable的子类
        也就说它具备map集合的特点,而且它里面存储的键值对都是字符串。
       
        是集合中和IO技术相结合的集合容器

        该对象的特点:可以用于键值对形式的配置文件
       
        那么在加载数据时,需要数据有固定格式:键=值(注#不读取)

        public static void setAndGet() {
                Properties prop=new Properties();
                prop.setProperty("wangweisong", "22");
                prop.setProperty("wws", "1214");
               
                System.out.println(prop);
        }
        public static void loadDemo()throws IOException
        {
                Properties props=new Properties();
                FileInputStream fis=new FileInputStream("E:\\java_heima\\buf.txt");
                //将流中的数据加载进集合
                props.load(fis);
                props.setProperty("wangwu", "20");
               
                FileOutputStream fos=new FileOutputStream("E:\\java_heima\\buf.txt");
                props.store(fos, "hahah");
               
                props.list(System.out);
               
                fis.close();
                fos.close();
        }

6、需求:用于记录应用程序运行次数。
        如果使用次数已到,那么给出注册提示

例        public static void main(String[] args)throws IOException
        {
               
                Properties props=new Properties();
               
                File file=new File("E:\\java_heima\\buf.txt");
                if(!file.exists())
                        file.createNewFile();
                FileInputStream fis=new FileInputStream(file);
               
                props.load(fis);
               
                int count=0;
                String value=props.getProperty("time");
               
                if(value!=null)
                {
                        count=Integer.parseInt(value);
                        if(count>5)
                        {
                                System.out.println("pleace exit");
                                return ;
                        }
                }
                       
                count++;
               
                props.setProperty("time", count+"");
               
                FileOutputStream fos=new FileOutputStream(file);
               
                props.store(fos, "");
                fos.close();
                fis.close();
               
        }

7、打印 : PrintWriter PrintStream
        该流提高了打印方法,可以将各种数据类型的数据都原样打印

        字节打印流:
        PrintStream
        构造函数可以接受的参数类型
        1)file对象
        2)字符串路径
        3)字节输出流OutputStream
        4)字符输出流Writer

例 PrintWriter
                BufferedReader bufr=
                        new BufferedReader(new InputStreamReader(System.in));
               
                PrintWriter out=new PrintWriter(System.out,true);
               
                String lineString=null;
                while ((lineString=bufr.readLine())!=null) {
                        if("over".equals(lineString))
                                break;
                        out.println(lineString.toUpperCase());
                       
                }
                bufr.close();       

8、合并流  SequenceInputStream

        public static void main(String[] args) throws IOException
        {
                // TODO Auto-generated method stub
                Vector<FileInputStream> v=new Vector<FileInputStream>();
               
                v.add(new FileInputStream("E:\\java_heima\\demo.txt"));
                v.add(new FileInputStream("E:\\java_heima\\demo2.txt"));
                v.add(new FileInputStream("E:\\java_heima\\demo3.txt"));
               
                Enumeration<FileInputStream> en=v.elements();
               
                SequenceInputStream sis=new SequenceInputStream(en);
               
                FileOutputStream fos=new FileOutputStream("E:\\java_heima\\demo4.txt");
               
                byte[] buf=new byte[1024];
               
                int len=0;
                while((len=sis.read(buf))!=-1)
                {
                        fos.write(buf,0,len);
                }
                fos.close();
                sis.close();
               
        }

9、切割流

        public static void spiltF()throws IOException
        {
                FileInputStream fis=new FileInputStream("E:\\java_heima\\WANG\\demo4.txt");
                FileOutputStream fos=null;
               
                byte[] buf=new byte[20];//控制切割大小 20字节
               
                int len=0;
                int count=1;
                while((len=fis.read(buf))!=-1)
                {
                        fos=new FileOutputStream("E:\\java_heima\\WANG\\part"+(count++)+".txt");
                        fos.write(buf,0,len);
                        fos.close();
                }
                //if(fos!=null)
                fis.close();
                       
        }


当切割完后,在合并
        public static void  merge() throws IOException
        {
                //获取源
                ArrayList<FileInputStream> al=new ArrayList<FileInputStream>();
               
                for(int x=1;x<6;x++)
                        al.add(new FileInputStream("E:\\java_heima\\WANG\\part"+x+".txt"));
                final Iterator<FileInputStream> it=al.iterator();
               
                 Enumeration<FileInputStream> en=new Enumeration<FileInputStream>()
                {
                        public boolean hasMoreElements() {
                                return it.hasNext();
                        }
                        public FileInputStream nextElement() {
                                return it.next();
                        }
                };
               
                SequenceInputStream sis=new SequenceInputStream(en) ;
                //写
                FileOutputStream fos=new FileOutputStream("E:\\java_heima\\WANG\\partAll.txt");
                byte[] buf=new byte[1024];
               
                int len=0;
               
                while((len=sis.read(buf))!=-1)
                        fos.write(buf,0,len);
                fos.close();
                sis.close();
        }

0 个回复

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