zoukankan      html  css  js  c++  java
  • Java文件读写

    本文整理梳理了,开发中,常用的Java 文件操作需要用到的方法

    1、文件创建

    判读一个文件路径、文件存在与否,不存在,就创建

    (1)目录、路径

    public void createFile(){
            
            String path= "f:\data\测试";//所创建文件的路径
            
            File f = new File(path);
            
            if(!f.exists()){
                
                f.mkdirs();//创建目录
            }
            
            String fileName = "abc.txt";//文件名及类型
            
            File file = new File(path, fileName);
            
            if(!file.exists()){
                
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                
            }
            
        }

    (2)文件

    String fileName = "file/test.txt"; // 定义文件名
            
        File f = new File(fileName);
            
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    2、文件读写

    (1)写入到 .txt文本文件

    /**
     * 保存字符串到文本中
     * 
     * @param str
     */
    public boolean save(String str) {
        boolean flag = false; // 声明操作标记
    
        String fileName = "file/Weather.txt"; // 定义文件名
            
        File f = new File(fileName);
            
        if(!f.exists()){
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
            
        FileWriter fw = null; // 用来写入字符文件的便捷类
        PrintWriter out = null; // 向文本输出流打印对象的格式化表示形式类
    
        try {
            fw = new FileWriter(f, true); // 创建一个FileWriter
            out = new PrintWriter(fw); // 创建一个PrintWriter,以追加方式将内容插入到最后一行
            out.println(str); // 将字符串打印到文本中
            out.flush(); // 刷新缓存
    
            flag = true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 关闭PrintWriter
                if (out != null) {
                    out.close();
                    out = null;
                }
                // 关闭FileWriter
                if (fw != null) {
                    fw.close();
                    fw = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    (2)读取 .txt文本文件

    /**
    *  读取所有区域信息
     */
    public List<Area> findAll() {
        List<Area> areaList = new ArrayList<Area>(0); // 创建结果列表
    
        String fileName = "file/data/Area.txt"; // 定义文件名
    
        try {
            // 创建BufferedReader类,从字符输入流中读取文本
            BufferedReader in = new BufferedReader(new InputStreamReader( new FileInputStream(fileName.toString())));
    
            String line; // 读取文本每行内容
            while ((line = in.readLine()) != null) {
                String[] values = line.split("##"); // 获取各变量数据
    
                Area area = getArea(values); // 根据当前行信息创建Area实体类
                areaList.add(area); // 往结果列表内添加Area信息
    
            }
            in.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return areaList;
    }

    Donate捐赠

    如果我的文章帮助了你,可以赞赏我 6.66 元给我支持,让我继续写出更好的内容)

       

      (微信)                                        (支付宝)

    微信/支付宝 扫一扫

  • 相关阅读:
    CentOS7设置开机自启动命令大全
    CentOS查看何人何时登陆用户
    CentOS显示设置时间命令- date
    CentOS系统命令
    CentOS系统中last命令的作用
    CentOS命令top下你不一定懂的cpu显示信息
    CentOS系统安装后的基础优化
    查看CentOS的网络带宽出口
    storm深入研究
    hadoop学习笔记之-hbase完全分布模式安装-5
  • 原文地址:https://www.cnblogs.com/moonsoft/p/9370797.html
Copyright © 2011-2022 走看看