对文件操作得一些总结

原创
01/21 16:02
阅读数 264

1.读取txt文件,然后从数据库从查询数据,并且写到txt文件中

public void getPhonehId() {

        List<String> list = new LinkedList<>();
        //1读取数据库中得图片
        try {
            String encoding = "GBK";
            File file = new File("C:\\Users\\Administrator\\Desktop\\name.txt");
            if (file.isFile() && file.exists()) { //判断文件是否存在
                InputStreamReader read = new InputStreamReader(
                        new FileInputStream(file), encoding);//考虑到编码格式
                BufferedReader bufferedReader = new BufferedReader(read);
                String lineTxt = null;
                while ((lineTxt = bufferedReader.readLine()) != null) {
                    list.add(lineTxt);
                }
                read.close();
            } else {
                System.out.println("找不到指定的文件");
            }
        } catch (Exception e) {
            System.out.println("读取文件内容出错");
            e.printStackTrace();
        }
        System.out.println("文本读取完成:"+list.size()+"==开始查数据库");
        List<Map<String, String>> resoult = new LinkedList<>();

        //方式二:没30000批为一组数据进行取值
        for (int i = 0; i < 4; i++) {
            int start = 30000 * i;
            int end = 30000 * (i + 1);
            if (start > list.size()) {
                start = list.size();
            }
            if (end > list.size()) {
                end = list.size();
            }
            List<String> subList = list.subList(start, end);
            List<Map<String, String>> list1 = houseMapper.selectTBasHouseByFaces(subList);
            List<Map<String, String>> list2 = guardhouseMapper.selectTBasHouseByFaces(subList);
            resoult.addAll(list1);
            resoult.addAll(list2);
            System.out.println("=====完成i组==="+i);
        }

        //方式一:逐个取数据
//        for (String face : list) {
//            Map<String, String> map = houseMapper.selectTBasHouseByFace(face);
//            if(CollectionUtils.isEmpty(map)){
//                map = guardhouseMapper.selectTBasHouseByFace(face);
//            }
//            if(!CollectionUtils.isEmpty(map)){
//                resoult.add(map);
//            }
//        }

        //输出到文件中
        if (resoult.size() > 0) {
            System.out.println("****************************正在写入到磁盘中.......");
            try {
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\out.txt", true), "utf-8"), 10240);
                for (Map<String, String> map : resoult) {
                    String hId = (String) map.get("hId");
                    String face = (String) map.get("face");
                    out.write(hId + "\t" + face + "\r");
                }
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("****************************写入完成.......");
        }

    }

 

2 读取数据库中得图片查看是否是有效连接

public void contrast() {

        String path = "E:\\file2\\";

        List<Map<String, Object>> readErrList = new LinkedList<>();

        //获取数据库中得图片
        List<Map<String, Object>> mapList = houseMapper.queryAllHouseImg();

        mapList.parallelStream().forEach(map -> {
            String faceImg = (String) map.get("faceImg");
            String hId = (String) map.get("hId");
            try {
                URL url = new URL(faceImg);
                int start = faceImg.indexOf("housHeadImg/") + 12;
                int end = faceImg.indexOf("?");
//                BufferedImage image = (BufferedImage) ImageIO.read(url);
                if (end > start) {
                    String fileName = path + faceImg.substring(start, end);
                    File file = new File(path + faceImg.substring(start, end));

//                url = new URL(faceImg);
                    DataInputStream dataInputStream = new DataInputStream(url.openStream());

                    FileOutputStream fileOutputStream = new FileOutputStream(new File(fileName));
                    ByteArrayOutputStream output = new ByteArrayOutputStream();

                    byte[] buffer = new byte[10240];
                    int length;

                    while ((length = dataInputStream.read(buffer)) > 0) {
                        output.write(buffer, 0, length);
                    }
                    fileOutputStream.write(output.toByteArray());
                    dataInputStream.close();
                    fileOutputStream.close();
                } else {
                    readErrList.add(map);
                }
            } catch (MalformedURLException e) {
                map.put("err", e.getMessage());
                readErrList.add(map);
            } catch (IOException e) {
                map.put("err", e.getMessage());
                readErrList.add(map);
            }
        });

        System.out.println("****错误数据数量:*****" + readErrList.size());

        //输出到文件中
        if (readErrList.size() > 0) {
            System.out.println("****************************有图片链接无法找到正在写入到磁盘中.......");
            try {
                Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("C:\\Users\\Administrator\\Desktop\\out.txt", true), "utf-8"), 10240);

                for (Map<String, Object> map : readErrList) {
                    String faceImg = (String) map.get("faceImg");
                    String hId = (String) map.get("hId");
                    String err = (String) map.get("err");
                    out.write(hId + "\t" + faceImg + "\t" + err + "\r");
                }
                out.flush();
                out.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("****************************写入完成.......");
        }

    }

 

 

 

展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部