现在的位置: 首页 > 综合 > 正文

从网络上下载图片及文件读写操作

2017年11月15日 ⁄ 综合 ⁄ 共 2387字 ⁄ 字号 评论关闭

将一个字符串写入文件中:

        /**  
	 * save file  
	 * @param toSaveString  
	 * @param filePath  
	 */
	public static void saveFile(String toSaveString, String filePath){
		try{
			File saveFile = new File(filePath);
			if (!saveFile.exists()){
			      File dir = new File(saveFile.getParent());
				  dir.mkdirs();
				  saveFile.createNewFile();
			}
		
		FileOutputStream outStream = new FileOutputStream(saveFile);
		outStream.write(toSaveString.getBytes());
		outStream.close();
		}catch(FileNotFoundException e){
			e.printStackTrace();
		}catch(IOException e1){
			e1.printStackTrace();
		}
	}	

从文件中读取内容,并返回字符串:

/**  
 * read file content 
 * @param filePath  
 * @return file's content  
 */

	public static String readFile(String filePath){
	     String str = "";
		 try{
				File readFile = new File(filePath);
				if(!readFile.exists()){
				return null;
			}
			FileInputStream inStream = new FileInputStream(readFile);
			
			ByteArrayOutputStream stream = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int length = -1;
			while ((length = inStream.read(buffer)) != -1){
			     stream.write(buffer, 0, length);
			}
			str = stream.toString();
			stream.close();
			inStream.close();
			return str;
		}catch (FileNotFoundException e){
			e.printStackTrace();
			return null;
		}catch(IOException e){
			e.printStackTrace();
			return null;
		}
	}

将一个输入流来保存到文件:

public static void saveIsFile(InputStream is,String path){
		
		File file=new File(MainActivity.apath+"/"+path.substring(31,path.length()-4));  
		try {
			if(!file.exists()){
				System.out.println("savefile--->>>>"+MainActivity.apath+"/"+path.substring(31,path.length()-4));
				FileOutputStream outb=new FileOutputStream(file);
				  int bytesRead;   
				  
	              byte[] buf = new byte[4 * 1024];   
	              while((bytesRead = is.read(buf))!=-1){   
	                 outb.write(buf,0,bytesRead);   
	              }   
	             outb.flush();   
	             outb.close();   
	             is.close();   
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
}

从文件中读取内容并返回一个输入流:

public static InputStream readGifFile(String path){
	    try{
			File readFile = new File(MainActivity.apath+"/"+path.substring(31,path.length()-4));
			if(!readFile.exists()){
			return null;
		}
			InputStream inStream = new FileInputStream(readFile);
			return inStream;
		 }catch(Exception e){
			 System.out.println(e.toString());
		 }
		return null;
 }

从网络上获取图片:
public static InputStream getGifInputStream(final String path,Context context){
		mContext = context;
		// 从网络上获取图片   
        try {
	
	     URL url = new URL(path);   
	     HttpURLConnection conn = (HttpURLConnection) url.openConnection();   
	     conn.setConnectTimeout(5000);   
	     conn.setRequestMethod("GET");
	     conn.setDoInput(true); 
		  if (conn.getResponseCode() == 200) {   
		        final InputStream is = conn.getInputStream(); 
		         return is;   
		     }  
	} catch (Exception e) {
	// TODO Auto-generated catch block
		System.out.println(e.toString());
		e.printStackTrace();
	}   
		return null;
	}
	

抱歉!评论已关闭.