java操作txt或者csv等文件

1、java程序读取txt文件

文件格式大致为:

  title_a|title_b|title_c|title_d
  A1|B1|C1|D1
  A2|B2|C2|D2
  A3|B3|C3|D3
  A4|B4|C4|D4

    public List<MyObject> getFildDateList(File file){
        BufferedReader in = null;
        FileInputStream fis = null;
        InputStreamReader is = null;
        try {            
            if(!file.exists()){
                throw new Exception(file.getName()+"不存在!");
            }
            fis = new FileInputStream(file);
            is = new InputStreamReader(fis);
            in = new BufferedReader(is);
            List<MyObject> objectList = new ArrayList<MyObject>();
            String str = in.readLine();//读取第一行
            
            //表头
            String title_a = str.split("\\|")[0];
            String title_b = str.split("\\|")[1];
            String title_c = str.split("\\|")[2];
            String title_d = str.split("\\|")[3];
            
            //循环读取第二行及后边
            while((str=in.readLine()) != null){
                if(!"".equals(str)){
                    String[] ss = str.split("\\|");
                    MyObject ser = new MyObject();
                    ser.setA(ss[0]);
                    ser.setB(ss[1]);
                    ser.setC(ss[2]);
                    ser.setD(ss[3]);                    
                    objectList.add(ser);
                }
            }
            System.out.println("共读取["+objectList.size()+"]行");    
            return objectList;
        } catch (Exception e) {
            throw e;
            System.out.println(e.toString());
            return null;
        } finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(fis!=null){                
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(is!=null){
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2、写文件

  写入类似上边的文件,FileUtils使用的是commons-io-1.4.jar

public void createTxt(List<Object> dataList){
        // 创建对账文件存放目录及对账文件
        String fPath = "C:\\Users\\Administrator\\Desktop";
        File dirFile = new File(fPath);
        if (!dirFile.isDirectory() && !dirFile.exists()) {
            dirFile.mkdirs();
        }

        String fileName = "my.txt";
        String filePath = dirFile.getAbsolutePath() + File.separator + fileName;
        File checkFile = new File(filePath);
        //如果文件已存在,改名备份已存在的文件
        if (checkFile.exists()) {
            File backupFile = new File(filePath + "." + new Date().getTime());
            FileUtils.copyFile(checkFile, backupFile);
            FileUtils.deleteQuietly(checkFile);
        }

        try {
            String firstLine = "title_a" + "|" + "title_b" + "|" + "title_c" + "|" + "title_d" + "\n";

            FileOutputStream output = new FileOutputStream(checkFile);
            //写入第一行
            output.write(firstLine.getBytes());
            //循环写入集合内容
            for (Object data : dataList) {
                output.write((data.toString() + "\n").getBytes());//实体类中写好toString方法
                output.flush();
            }

            output.close();
        } catch (Exception e) {
            logger.error("创建文件失败", e);
        }
    }

3、读取txt记录重复值

1.txt

060603417
185908309
187037450
187110229
190929344
190929432
191318037
191797845
192057173
192057304
592571736

代码:

//读取txt,查找重复行
    public static void main(String[] args) throws Exception {
        File file = new File("C:\\Users\\Administrator\\Desktop\\1.txt");
        FileInputStream fis = new FileInputStream(file);
        InputStreamReader is = new InputStreamReader(fis);
        BufferedReader in = new BufferedReader(is);
        String str = "";
        Map<String, Integer> ll = new HashMap<String, Integer>();
        while ((str = in.readLine()) != null) {
            if (!"".equals(str)) {
                if (ll.containsKey(str)) {
                    System.out.println(str);
                    ll.put(str, ll.get(str)+1);
                    continue;
                }
                ll.put(str, 1);
            }
        }
    }

 

posted @ 2018-12-11 23:35  常新志  阅读(1041)  评论(0编辑  收藏  举报