zoukankan      html  css  js  c++  java
  • java file的一些方法

    file包下的一些方法:
     
        File file = new File("d:\", "tea.txt");
            //文件名
            System.out.println("文件名"+file.getName());
            //路径
            System.out.println("路径"+file.getPath());
            //绝对路径
            System.out.println("绝对路径"+file.getAbsolutePath());
            //判断文件是否存在
            System.out.println("判断文件是否存在"+file.exists());
            //判断file是文件还是文件目录
            System.out.println("判断file是文件还是文件目录:");
            System.out.println(file.isDirectory()?"目录":"文件");
            //判断file是普通文件还是命名管道
            System.out.println("判断file是普通文件还是命名管道:");
            System.out.println(file.isFile()?"普通文件":"命名管道");
            //判断是否为可读文件
            if (file.canRead()) {
                System.out.println("可读文件");
            }else{
                System.out.println("非可读文件");
            }
            //判断是否为可写文件
            if (file.canWrite()) {
                System.out.println("为可写文件");
            }else{
                System.out.println("非可写文件");
            }
            //返回最后修改的时间是距离2000年1月1日0时0分0秒多少秒
            System.out.println(file.lastModified());
        }
        /**
         * 创建文件
         * @param file
         */
        public static void create(File file){
            if (!file.exists()) {
                try {
                    file.createNewFile();
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        }
        /**
         * 删除文件
         * @param file
         */
        public static void delete(File file){
            if (file.exists()) {
                file.delete();
            }
        }
        /**
         * 输出文件夹里所有子文件的名称
         */
        public static void showDirs(File file){
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                for (File file2 : files) {
                    System.out.println(file2.getName());
                }
            }
    一个字节一个字节往外输出的方法:
    FileInputStream fis = new FileInputStream("d:\tea.txt");
            System.out.println("可读取字节数"+fis.available());
            System.out.println("文件内容为:");
            /**
             * 一个字节一个字节往外输出
             */
            int data;
            while ((data=fis.read())!=1) {
                System.out.print(data);
            }
            System.out.println("真实内容");
            while ((data=fis.read())!=-1) {
                System.out.print(String.valueOf((char)data));
            }
            fis.close();
     
    往文本里输入数据的方法:
    如果第二个参数不写默认替换,如果第二个参数传true则是追加输入
    String string = "good good study    day day up";
            byte[] words = string.getBytes();
            //如果第二个参数不写默认替换,如果第二个参数传true则是追加输入
            FileOutputStream fos =new FileOutputStream("d:tea.txt", true);
            fos.write(words,0,words.length);//从words数组的0开始写到最后结束
            fos.close();
     
    异常的浅解:
    public static void main(String[] args) throws FileNotFoundException, IOException {//抛出的两个异常
            // TODO Auto-generated method stub
            ObjectOutputStream oos = null;
            /**
             * try里面放置可能会出现异常的代码块
             */
                try {
                    System.out.println("try里面放置可能会出现异常的代码块");
                    oos = new ObjectOutputStream(new FileOutputStream("d:\tea.txt"));
                    Student stuzhang = new Student("张三", 22, "男", "1222");
                    Teacher teacher1 = new Teacher("z", 50, "", "");
                    Teacher teacher2 = new Teacher("zh", 40, "", "");
                    oos.writeObject(stuzhang);
                    oos.writeObject(teacher1);
                    oos.writeObject(teacher2);
                } catch (FileNotFoundException e) {
                    // 处理FileNotFoundException异常的代码块
                    System.out.println("处理FileNotFoundException异常的代码块");
                }catch (IOException e) {
                    // 处理IOException异常的代码块
                    System.out.println("处理IOException异常的代码块");
                }finally{
                    oos.close();//文件流停止必须写close()
                    System.out.println("不论走不走异常,这段代码都会走");//不论走不走异常,这段代码都会走
                }
     
     
        }
     
     
     
  • 相关阅读:
    Eclipse配置SVN的几种方法及使用详情
    python爬虫实战:基础爬虫(使用BeautifulSoup4等)
    MySQL中case when的基本用法总结
    SQL常见的一些面试题(太有用啦)
    Python应用——自定义排序全套方案
    Hadoop运维
    图形化查看maven的dependency依赖
    mac os x 10.10.3 安装protoc
    创业方向:O2O及移动社交 from 沈博阳
    手动编译安装docker环境,以及偶尔出现的bug
  • 原文地址:https://www.cnblogs.com/zhangdiIT/p/5648780.html
Copyright © 2011-2022 走看看