Java读文件,写文件

12

23

45

	
	public static void main(String[] args) {
		int COUNT = 5;
		String pathname = "E:/浙江营销平台/2.txt";
 		log.info("上传文件路劲为:"+pathname);
 		//读文件
 		File file = new File("E:/浙江营销平台/1.txt");

 		//写文件
 		File file1 = new File(pathname);
 		FileReader fr= null;
        FileReader fr2= null;
        BufferedReader br = null;
        BufferedReader br2 = null;
        try {
            fr=new FileReader(file);
            fr2=new FileReader(file);
            br=new BufferedReader(fr);
            br2=new BufferedReader(fr2);
            String mobileNumber ="";
//            Set<String> set = new HashSet<>();
            
            int fileCount = 0;
            while ((mobileNumber = br.readLine())!=null) {
            	fileCount++;
            }
            log.info("行数:"+fileCount);
            if(fileCount<=COUNT){
            	log.info("==============进入到if ===============");
            	Set<String> set = MarketeeTaskService.insertRedisBuf( br2);
	    		 writeMobilesFile(set, file1, pathname);
        	}if(fileCount > COUNT){
        		log.info("==============进入到else=================");
        		for (int i = 0; i < (fileCount / COUNT) + 1; i++) {
        			log.info("==========for===========");
        			Set<String> set = MarketeeTaskService.insertRedisBuf( br2, i);
        			Iterator<String> setValueLs = set.iterator();
        			while(setValueLs.hasNext()){
        				log.info("set==" + setValueLs.next());
        			}
		    		writeMobilesFile(set, file1, pathname);
	    		}
        	}
        }catch(Exception e){
        		log.error("出错");
        }finally{
        	
        }
        
        log.info("完成!!!");
	}
        
        public static Set<String> insertRedisBuf(BufferedReader br){
        	String mobileNumber = "";
        	Set<String> set = new HashSet<String>();
        	try {
				while ((mobileNumber = br.readLine())!=null) {
//       		 redisConnection.sAdd(key.getBytes("UTF-8"),mobileNumber.getBytes("UTF-8"));
					log.info("mobileNumber====" + mobileNumber);
					set.add(mobileNumber);
				}
				return set;
			} catch (IOException e) {
				e.printStackTrace();
			}
        	return set;
        }
        
        public static Set<String> insertRedisBuf(BufferedReader br, int line  ){
        	log.info("============insertRedisBuf=================");
        	String mobileNumber = "";
        	Set<String> set = new HashSet<String>();
        	try {
        		int count = line * 5;
				while ((mobileNumber = br.readLine())!=null) {
	           		 if((count == line * 5 || count > line * 5) && count < (line+1) * 5){
	           			log.info("mobileNumber====" + mobileNumber);
						set.add(mobileNumber);
						if(count==((line+1) * 5 -1)){
							break;
						}
	        		 }else{
	        			 break;
	        		 }
	           		count++;
           		 }
				return set;
			} catch (IOException e) {
				e.printStackTrace();
			}
        	return set;
        }
   	 
        
        private static Map<String,Object> writeMobilesFile(Set<String> finalSet, File file, String filePath) throws Exception {
    		Map<String,Object> mobileMap = new HashMap<>();

    		
    		WirterUtils.writeTxt(finalSet, filePath, file);

    		mobileMap.put("rows","22");
    		mobileMap.put("filePath",file.getAbsolutePath());
    		return mobileMap;
    	}
	
	

WirterUtils



import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class WirterUtils {
    public static void writeTxt(Set rows, String fileName, File file) throws IOException {
        // 写入txt
        try {
            Writer fw = null;
            if(file == null){
                file = new File(fileName);
            }

            try {
                fw = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");
                PrintWriter pw = new PrintWriter(fw);
                for (Object line : rows) {
                    String lineMap = (String) line;
                    StringBuffer linebuffer = new StringBuffer();
                    linebuffer.append(lineMap);
                    linebuffer.append("\r\n");
                    pw.printf(linebuffer.toString(), "\r\n");
                }
                pw.flush();
                pw.close();
            } catch (Exception e) {
                throw e;
            }finally {
                if(fw!=null) {
                    fw.close();
                }
            }
        } catch (Exception e) {
            throw e;
        }
    }

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值