• 【JAVA】文件各行打乱


    给定一个文件,把文件 里的各行打乱,并验证其正确性,时间紧迫,随手写写
            String path = "/Users/guangyi.zgy/Desktop/scene_2khas_8kno_query_1W.csv";
            List<String>  newList=new ArrayList<String>();
            //打开文件
            File file = new File(path);
                try {
                    InputStream instream = new FileInputStream(file);
                    if (instream != null)
                    {
                        InputStreamReader inputreader = new InputStreamReader(instream);
                        BufferedReader buffreader = new BufferedReader(inputreader);
                        String line;
                        //分行读取
                        int j = 0;
                        while (( line = buffreader.readLine()) != null) {
                            newList.add(line);
                        }
                        instream.close();
    
                        String[] newA = new String[newList.size()];
                        for (String str : newList) {
                            Random random = new Random();
                            int a = random.nextInt(newList.size());
                            if (newA[a] == null) {
                                newA[a] =  str;
                            } else {
                                for (int i = 0; i < newA.length; i++) {
                                    if (newA[i] == null) {
                                        newA[i] = str;
                                        break;
                                    }
                                }
                            }
                        }
                        String pathNew = "/Users/guangyi.zgy/Desktop/NEW_scene_2khas_8kno_query_1W.csv";
                        File fileNew = new File(pathNew);
                        fileNew.createNewFile();
    
                        OutputStream outputStream = new FileOutputStream(fileNew);
                        BufferedWriter bf = new BufferedWriter(new OutputStreamWriter(outputStream));
    
                        boolean firstLine = true;
                        for (String str : newA) {
                            if (firstLine) {
                                bf.write(str);
                                firstLine = false;
                                continue;
                            }
                            if (str != null) {
                                bf.write("
    " + str);
                            } else {
                                System.out.println("有空行出现");
                            }
                        }
                        bf.close();
    
                        for (String str : newA) {
                            int count = 0;
                            for (String str1 : newA) {
                                if (StringUtils.equals(str1.trim(), str.trim())) {
                                    count ++;
                                }
                            }
                            if (count > 1) {
                                System.out.println("有多个相同字符");
                            }
                        }
                    }
                }
                catch (java.io.FileNotFoundException e)
                {
                }
                catch (IOException e)
                {
                }
  • 相关阅读:
    operator模块和functools模块
    函数注解
    用户定义的可调用类型、从定位参数到仅限关键字参数
    可调用对象
    nxos启动的初始化和https访问nx-api
    网络安全基础之网络协议与安全威胁
    华为AC中服务集命令解释配置
    转:图解ARP协议(四)代理ARP原理与实践(“善意的欺骗”)
    windows下python3 python2 共存下安装virtualenvwrapper
    关于网络安全学习的网站
  • 原文地址:https://www.cnblogs.com/garinzhang/p/10182314.html
Copyright © 2020-2023  润新知