java下读取指定地址文件后并输出文件内容

        File file=new File(path);
        FileInputStream in = null;
        try { 
            in = new FileInputStream(file);
            //创建字节数组,用于接收从文件中读取的字节
            int size=in.available();
            byte buf[] = new byte[size];
            String string = ""; //接收字节转化的字符串
            int length = in.read(buf);
            string = new String(buf,"GB2312");//将字节转化成字符串
//            JSONObject json = JSONObject.fromObject(string);
            OrderDetailsRes orderDetailsRes=JSON.parseObject(string,OrderDetailsRes.class);
            orderService.resolveOrderData(orderDetailsRes);
            } catch (IOException ex) {
            ex.printStackTrace();
            }finally {
                if (in!=null) {
                    try {
                        in.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

文件内容为String,则输出也为String。

猜你喜欢

转载自blog.csdn.net/longloveqing/article/details/81868463