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;
    }
    

      

  • 相关阅读:
    Web负载均衡的几种实现方式
    DNS负载均衡
    NoSQL开篇——为什么要使用NoSQL
    mySQL优化 my.ini 配置说明
    mysql性能优化-慢查询分析、优化索引和配置
    MySQL性能优化的最佳20+条经验
    IOS的UIPickerView 和UIDatePicker
    IOS的KVC
    IOS中的通知NSNotification
    IOS对话框UIAlertView
  • 原文地址:https://www.cnblogs.com/nihaorz/p/6382602.html
Copyright © 2011-2022 走看看