java操做txt或者csv等文件

一、java程序讀取txt文件java

文件格式大體爲:

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

    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();
                }
            }
        }
    }

二、寫文件spa

  寫入相似上邊的文件,FileUtils使用的是commons-io-1.4.jarcode

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);
        }
    }

三、讀取txt記錄重複值blog

1.txtget

060603417
185908309
187037450
187110229
190929344
190929432
191318037
191797845
192057173
192057304
592571736

代碼:it

//讀取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);
            }
        }
    }
相關文章
相關標籤/搜索