读取txt内容写到新的txt中

 

public static void main(String[] args) throws SQLException {
		JavaTools jt = new JavaTools();
		jt.createTxt(excuteReadTxt("C:/Users/Administrator/Desktop/oldTxt.txt"), 7, "C:/Users/Administrator/Desktop/newTxt.txt");
		System.out.println("-----------over");
	}

 

createTxt方法:

    public static void createTxt(List<Object[]> values,int objSize,String file){
    	StringBuffer row=null;
    	OutputStreamWriter osw = null;
    	try {
    		osw = new OutputStreamWriter(new FileOutputStream(file,true),"GBK");
    		for(int i=0 ; i<values.size() ; i++){
    			row = new StringBuffer();
    			Object[] obj = values.get(i);
    			for(int j = 0 ; j<objSize ; j++){
    				if(objSize-j == 1){
    					row.append(obj[j]+"\r\n");//换行
    				}else{
    					row.append(obj[j]+"\t");//tab键
    				}
    			}
    			osw.write(row.toString());
    		}
    		values.clear();
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
       	 if(osw!=null)//if(osw!=null)
             try {
             	osw.close();//osw.close();
             } catch (IOException e) {
                 e.printStackTrace();
             }
      }
    }

 excuteReadTxt方法:

/**执行readTxt方法*/
    public static List<Object[]> excuteReadTxt(String filePath){
		Map<Integer,List<Object>> map = readTxt(new File(filePath));
		List<Object[]> objs = new ArrayList<Object[]>();
		Iterator<Entry<Integer, List<Object>>> iterator = map.entrySet().iterator();
		while(iterator.hasNext()){
			Entry<Integer, List<Object>> entery = iterator.next();
			int key = entery.getKey();
			List<Object> values = entery.getValue();
			Object[] ss = new Object[values.size()];
			for(int i = 0 ; i<values.size() ; i++){
				ss[i] = values.get(i);
			}
			objs.add(ss);
		}
		return objs;
    }

 readTxt方法:

   public static Map<Integer,List<Object>>  readTxt(File file){
    	//jt.readTxt(new File("C:/Users/Administrator/Desktop/aaaaa.txt"));
    	BufferedReader br = null;
    	Map<Integer,List<Object>> map = new HashMap<Integer, List<Object>>();
    	List<Object> values = null;
    	int key = 1;
    	try {
    		InputStreamReader read = new InputStreamReader(new FileInputStream(file), "GBK");
    		br = new BufferedReader(read);
    		String temp = null;
    		while ((temp = br.readLine()) != null) {
    			if("".equals(temp)){
    				continue;
    			}
    			values = new ArrayList<Object>();
    			StringTokenizer st = new StringTokenizer(temp);
    			while(st.hasMoreElements()){
    				String num = st.nextToken("\t").trim();
    				values.add(num);
    			}
    			map.put(key, values);
    			key++;
    		}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			try {
				if(null != br){
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return map;
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值