写文件

public class WriteTool 
{
	private static FileWriter writer = null;
	
	private static Long beforeRequest = 0L;
	private static Long afterRequest = 0L;
	private static String state =null;
	private static String number = null;
	private static String stx = null;
	private static String sty = null;
	private static String edx = null;
	private static String edy = null;
	private static Vector<String> vector = new Vector<String>();
//	private static ConcurrentHashMap<Integer, String> map = new ConcurrentHashMap<Integer, String>();
	private static int count = 0;
	
	public static synchronized void writeTXT(String urlString,String saveFile,int length)
	{
		count++;
//		System.out.println("1-->  count:"+count+",number:"+number);
		number = urlString.substring(urlString.indexOf("number")+7);
		stx = urlString.substring(urlString.indexOf("stx=")+4,urlString.indexOf("&sty"));
		sty = urlString.substring(urlString.indexOf("sty=")+4,urlString.indexOf("&edx"));
		edx = urlString.substring(urlString.indexOf("edx=")+4,urlString.indexOf("&edy"));
		edy = urlString.substring(urlString.indexOf("edy=")+4,urlString.indexOf("&chg"));
		
		try 
		{
//			System.out.println(" 2--->  number-->"+number);
			//访问服务器前的时间
			beforeRequest = System.currentTimeMillis();
			URL requestURL = new URL(urlString.substring(0, urlString.indexOf("number")-1));
			HttpURLConnection connection = (HttpURLConnection) requestURL.openConnection();
			connection.setAllowUserInteraction(false);
			connection.setDoInput(true);
			//设置连接超时
			connection.setConnectTimeout(15000);
			connection.setReadTimeout(15000);
//			connection.addRequestProperty("Cache-Control", "no-cache");
			connection.addRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE5.5;Windows NT; DigExt)");
			connection.setRequestProperty(" Content-Type "," application/x-www-form-urlencoded ");
			connection.connect();
			
			System.out.println("    count:"+count+",fcgi:"+number);
			InputStream inputStream = connection.getInputStream();
			FileOutputStream fs = new FileOutputStream(saveFile+"/Route"+number+".fcgi");
			byte[] b = new byte[102400];
			int len = 0;
			int temp = 0;
			while((temp = inputStream.read()) != -1)
			{
				b[len] = (byte)temp;
				len++;
				//访问服务器后的时间
				afterRequest = System.currentTimeMillis() - beforeRequest;
			}
			fs.write(b, 0, len);
			fs.flush();
			inputStream.close();
			fs.close();
			//判断响应码是否是200
			if(connection.getResponseCode() == HttpURLConnection.HTTP_OK)
			{
				if(len > 20)
				{
					state = "ok";
					//将数据写入指定文件
				}
				else 
				{
					state = "ng";
				}
				connection.disconnect();
				
					//填充的数据
					StringBuffer buffer = new StringBuffer();
					buffer.append(number);
					buffer.append(",");
					buffer.append(stx);
					buffer.append(",");
					buffer.append(sty);
					buffer.append(",");
					buffer.append(edx);
					buffer.append(",");
					buffer.append(edy);
					buffer.append(",");
					buffer.append(afterRequest);
					buffer.append(",");
					buffer.append(state);
					/**
					 * 将访问服务器返回的数据添加到Vector
					 * insertElementAt(E obj, int index)  将指定对象作为此向量中的组件插入到指定的 index 处。
					 * add(int index, E element)  在此向量的指定位置插入指定的元素。
					 * set(int index, E element)  用指定的元素替换此向量中指定位置处的元素。
					 * setElementAt(E obj, int index) 将此向量指定 index 处的组件设置为指定的对象。
					 * 当使用insertElementAt()和add()方法进行插入元素的时候,会出现当前插入的数据有值,其他的都为null,
					 * 所以选择set最合适,进行的是替换操作
					 */
					vector.setSize(length);
					vector.set(Integer.parseInt(number.trim())-1, buffer.toString());
				
				if(count == length)
				{
					System.out.println("write-------");
					writeTxt(saveFile, vector);
				}
			}
		} 
		catch (MalformedURLException e1) 
		{
			e1.printStackTrace();
		} 
		catch (SocketTimeoutException e) 
		{
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	/**
	 * 保存txt文件
	 */
	private static void writeTxt(String save,Vector<String> datas){
		//标题
		StringBuffer title = new StringBuffer();
		title.append("径路索引");
		title.append(",");
		title.append("出发地经度");
		title.append(",");
		title.append("出发地纬度");
		title.append(",");
		title.append("目的地经度");
		title.append(",");
		title.append("目的地纬度");
		title.append(",");
		title.append("服务器响应时间");
		title.append(",");
		title.append("请求结果");
		
		try {
			writer = new FileWriter(save+"/Navi"+getDate()+".txt");
			writer.write(title.toString()+"\r\n");
			writer.flush();
				//给文件填充数据
				for (int i = 0; i < datas.size(); i++) 
				{
					writer.write(datas.get(i)+"\r\n");
					writer.flush();
				}
				writer.close();
				vector.clear();
				count = 0;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取系统时间
	 * @return
	 */
	private synchronized static String getDate()
	{
	    Calendar ca = Calendar.getInstance();
	 	int year = ca.get(Calendar.YEAR);//获取年份
	 	int month=ca.get(Calendar.MONTH);//获取月份 
	 	int day=ca.get(Calendar.DATE);//获取日
	 	int minute=ca.get(Calendar.MINUTE);//分 
	 	int hour=ca.get(Calendar.HOUR);//小时 
	 	int second=ca.get(Calendar.SECOND);//秒
	 	      
	 	String date = year + "年" + (month + 1 )+ "月" + day + "日" +hour +"时" +minute + "分" +second+"秒";

	 	return date;
	} 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值