zoukankan      html  css  js  c++  java
  • Java常用工具方法

    以GET请求形式获取文本文件内容

    /**
     * 以GET请求形式获取文本文件内容
     * @param url http下载地址,比如http://www.abc.com/123.css
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String getFileContent(String url) throws ClientProtocolException, IOException{
    	HttpClient httpCient = new DefaultHttpClient();
    	HttpGet httpGet = new HttpGet(url);
    	HttpResponse httpResponse = httpCient.execute(httpGet);
    	if (httpResponse.getStatusLine().getStatusCode() == 200) {
    		HttpEntity entity = httpResponse.getEntity();
    		String response = EntityUtils.toString(entity,"utf-8");
    		return response;
    	}
    	return null;
    }
    

    创建新文件

    /**
     * 创建新文件
     * @param file
     * @throws IOException
     */
    public static void createNewFile(File file) throws IOException{
    	/**
    	 * 如果父目录不存在即创建
    	 */
    	if(!file.getParentFile().exists()) {
    		file.getParentFile().mkdirs();
    	}
    	file.delete();
    	file.createNewFile();
    }
    

    给文本文件追加内容

    /**
     * 给文本文件追加内容
     * @param content
     * @param file
     * @return
     * @throws Exception
     */
    public static boolean appendTxtFile(String content, File file) throws Exception {
    	boolean append = false;
    	try {
    		if (file.exists()){
    			append = true;
    		}
    		FileWriter fw = new FileWriter(file, append);
    		// 创建字符输出流对象
    		BufferedWriter bf = new BufferedWriter(fw);
    		// 创建缓冲字符输出流对象
    		bf.append(content);
    		bf.flush();
    		bf.close();
    	} catch (IOException e) {
    		e.printStackTrace();
    	}
    	return append;
    }
    

      

  • 相关阅读:
    51nod 1185 威佐夫游戏 V2
    51nod 1212 无向图最小生成树
    51nod 1242 斐波那契数列的第N项
    51nod 1240 莫比乌斯函数
    51nod 1256 乘法逆元
    51nod 1264 线段相交
    51nod 1265 四点共面
    51nod 1298 圆与三角形
    51nod 2006 飞行员配对
    CGLIB介绍与原理
  • 原文地址:https://www.cnblogs.com/nihaorz/p/6382602.html
Copyright © 2011-2022 走看看