美文网首页
Java文件的读取

Java文件的读取

作者: yxCassiel | 来源:发表于2019-10-17 10:22 被阅读0次

调用方法

    //读取json文件
    String a = getDatafromFile();
    JSONArray objects = JSONObject.parseArray(a);
    //        Object o = objects.get(0);
        for(Object o:objects) {
        message message = JSONObject.parseObject(o.toString(), message.class);
        System.out.println();
      }


        //写文件
        message m = new message();
        m.setMixStationId("1");
        m.setFlag("高级超标");
        m.setMixStationName("fjfjfgyu拌合站");
        List<message> dto =new ArrayList<>();
        dto.add(m);
        m.setMixStationId("2");
        dto.add(m);

        writeFileDto(dto);

读取文件

    public static message readFileDto(){
        File file = new File("/Users/yexin/Desktop/t.json");
        if(!file.exists()){
            return null;
        }
        message dto = null;
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(file),"UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line;
            while((line = br.readLine()) != null){
                sb.append(line);
            }
            dto = JSONObject.parseObject(sb.toString(),message.class);
            br.close();
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return dto;
    }








    private String getDatafromFile(/*String fileName*/) {

        String Path="/Users/yexin/Desktop/t.json";
        BufferedReader reader = null;
        String laststr = "";
        try {
            FileInputStream fileInputStream = new FileInputStream(Path);
            InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            reader = new BufferedReader(inputStreamReader);
            String tempString = null;
            while ((tempString = reader.readLine()) != null) {
                laststr += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return laststr;
    }

写文件


    /**
     * 将对象写入缓存文件中
     * @author
     */
    public static void writeFileDto(List<message> dto){
        File file = new File("/Users/yexin/Desktop/t.json");
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        try {
            String jsonStr = JSON.toJSONString(dto);
            BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file),"UTF-8"));
            bw.write(jsonStr);
            bw.flush();
            bw.close();
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            e.printStackTrace();
        }catch (IOException e) {
            e.printStackTrace();
        }
    }








    private void saveDataToFile(String fileName,String data) {
        BufferedWriter writer = null;
        File file = new File("d:\\"+ fileName + ".json");
        //如果文件不存在,则新建一个
        if(!file.exists()){
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        //写入
        try {
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file,false), "UTF-8"));
            writer.write(data);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if(writer != null){
                    writer.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("文件写入成功!");
    }

相关文章

  • java读取properties文件总结

    一、java读取properties文件 1.1 java读取properties文件代码测试

  • java IO 读取数据

    java IO 读取数据 提供集中读取数据的方法 java7 读取文件的API

  • Java Web技术经验总结(十六)

    使用Java读取文件时,要评估文件的大小,避免因为文件过大而造成OOM,参见:Java高效读取文件 RateLim...

  • java读取大文件解决思路

    1. java 读取大文件的困难 java 读取文件的一般操作是将文件数据全部读取到内存中,然后再对数据进行操作。...

  • Java读取文件方法汇总

    这篇文章主要为大家详细介绍了Java读取文件方法,按字节读取文件内容、按字符读取文件内容、随机读取文件内容等,具有...

  • Java读取文件方法汇总

    这篇文章主要为大家详细介绍了Java读取文件方法,按字节读取文件内容、按字符读取文件内容、随机读取文件内容等,具有...

  • 5、GeoTrellis-读取栅格文件

    读取单波段的栅格 读取多波段栅格 使用流读取栅格 通过流读取的tif,单个小文件可以完整的读取,大文件则Java虚...

  • java XML解析——DOM方式

    参照:XML解析——DOM方式Java文件操作①——XML文件的读取

  • Python 进阶——什么是上下文管理器

    错误读取文件 java思维读取文件 使用with读取文件 上下文管理器语法 一个类在 Python 中,只要实现以...

  • Properties 类的用法

    java.uti.properties项目主要用它来读取配置文件,可以读取以 .properties 结尾的文件或...

网友评论

      本文标题:Java文件的读取

      本文链接:https://www.haomeiwen.com/subject/mdmvyctx.html