java笔记__读取文件内容

    public JSONObject toGetFileData(Integer model){
        //用于存储文件内容
        String jsonString = "";
        InputStream is = null;
        try {
            is = new FileInputStream("D:\\data\\xxxxx.txt");
            //用来保存每行读取的内容
            String line = null;
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            //读取第一行
            line = reader.readLine();
            //如果line为空说明读完了
            while (line != null) {
                //将读到的内容添加到 jsonString 中
                jsonString += line;
                //添加换行符
                jsonString += "\n";
                //读取下一行
                line = reader.readLine();
            }
            reader.close();
            is.close();
        } catch (FileNotFoundException e) {
            e.printstacktrace();
        } catch (IOException e) {
            e.printstacktrace();
        }
        JSONObject jsonObject = JSONObject.parSEObject(jsonString);
        return jsonObject;
    }

 

相关文章

HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...