`
flylynne
  • 浏览: 362627 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

格式转换 读写文件

    博客分类:
  • JAVA
阅读更多

转换前的格式:1894297,716,荆州

转换后的格式:insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('0662', 'CDMA-189', '861890080');

 

import java.io.*; 
public class TxtReader { 
	    public static void main(String[] args)   
	    {   
	        String s;   
	        s=calculate();   
	        System.out.println(s);   
	    }   
	    static String calculate()   
	    {   
	        StringBuffer sb=new StringBuffer("");   
	        String desFile = "C:\\fileio.txt";
	        //目标文件---需要写入的文件
	        try{   
	            FileReader reader = new FileReader("C:"+File.separator+"189.txt");   
	            BufferedReader br = new BufferedReader(reader);   
	            PrintWriter out = new PrintWriter(
               	     new FileWriter(
               	       new File(desFile)));
	            String line = null;   
	            while((line = br.readLine()) != null) {   
	            	String[] strs = line.split(",");
	            	byte[] b = strs[0].getBytes();
		    		String tmp;
		    		if(b[1]=='8'&&b[2]=='9'){
		    			tmp="CDMA-189";
		    		}else if(b[1]=='5'&&b[2]=='3'){
		    			tmp="CDMA-153";
		    		}else if(b[1]=='3'&&b[2]=='3'){
		    			tmp="CDMA-133";
		    		}else if(b[1]=='8'&&b[2]=='0'){
		    			tmp="CDMA-180";
		    		}else{
		    			tmp="err";
		    	}
		    		String area ="0"+strs[1];
		    		String route ="86"+strs[0];
	                sb.append("insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('"+area+
	                "','"+tmp+"','"+route+"');" +'\n');  
	                out.write("insert into TA_NUMBER(AREA_ID, NUMBERTYPE, NUM)values('"+area+
	    	                "','"+tmp+"','"+route+"');" +"\r\n");               
	            }  
	            out.flush();
	            out.close();
	            br.close();   
	            reader.close(); 	            
	       }catch(Exception e){   
	            e.printStackTrace();   
	       }   
	        return sb.toString();   
	    }   	   
	}  

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics