1.inputsream对象转字节数组方法 public static byte[] toByteArray(InputStream in) throws IOException { ByteArrayOutputStream out=new ByteArrayOutputStream(); byte[] buffer=new byte[1024*4]; int n=0; while ( (n=in.read(buffer)) !=-1) { out.write(buffer,0,n); } return out.toByteArray(); } 2.读取远程文件保存到本地 /** * 下载远程文件并保存到本地 * * @param remoteFilePath 远程文件路径 * @param localFilePath 本地文件路径 */ public boolean downloadFile(String remoteFilePath, String localFilePath) { URL urlfile = null; HttpURLConnection httpUrl = null; BufferedInputStream bis = null; BufferedOutputStream bos = null; try { File f = new File(localFilePath); File parent = f.getParentFile(); if(parent!=null&&!parent.exists()){ parent.mkdirs(); } f.createNewFile(); urlfile = new URL(remoteFilePath); httpUrl = (HttpURLConnection) urlfile.openConnection(); httpUrl.connect(); bis = new BufferedInputStream(httpUrl.getInputStream()); bos = new BufferedOutputStream(new FileOutputStream(f)); int len = 2048; byte[] b = new byte[len]; while ((len = bis.read(b)) != -1) { bos.write(b, 0, len); } if(bis!=null){ bis.close(); } bos.flush(); httpUrl.disconnect(); } catch (Exception e) { e.printStackTrace(); return false; } finally { try { if(bis!=null){ bis.close(); } if(bos!=null){ bos.close(); } } catch (IOException e) { e.printStackTrace(); } } return true; }
String result = ""; DefaultHttpClient httpclient = new DefaultHttpClient(); HttpGet httpget = new HttpGet(url); try { HttpResponse response = httpclient.execute(httpget); HttpEntity entity = response.getEntity(); // 显示结果 BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent(), "UTF-8")); String line ; while ((line = reader.readLine()) != null) { result = line; } entity.consumeContent(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
相关推荐
NULL 博文链接:https://chong0660.iteye.com/blog/2367232
主要介绍了Ruby常用文件操作方法,本文讲解了新建文件、读取文件、删除、重命名文件、目录操作等常用文件操作方法,需要的朋友可以参考下
封装了包括所有常用的文件操作方法如:读文件,写文件,查看文件夹大小,树状展示文件夹中文件目录,拷贝文件,复制文件,删除文件,创建文件,递归删除文件夹中文件,获取指定文件属性
C#中常用的经典文件操作方法
一些文件的常用操作 writeDate(Context context,InputStream is, File file, String charSet) getDataFromAssets(Context context,String path, String charSet) getText(Context context, String path, String ...
C#中常用的经典文件操作方法.doc C#中常用的经典文件操作方法.doc
个人关于c语言中的文件操作的常用方法小结
描述了几种常用的C# 文件操作方法,使你对用C#操作文件非常容易上手
《Java文件操作大全》电子书 本文汇集常用文件操作方法,包括文件的建立/检查与删除,目录的建立/检查与删除,取出目录中文件,文件属性的取得,逐行读取数据等等。
File类,是一个静态类,主要是来提供一些函数库用的。静态实用类,提供了很多静态的方法,支持对文件的基本操作,包括创建,拷贝,移动,删除和 打开一个文件。
Mstar安卓方案常用修改和操作方法(以MSD6A628为例)
用C#编写处理文件操作时候的语句用法……提供多种不同的属性方法。
Excel-VBA操作文件四大方法 本文主要介绍四种常用的方法: 1、利用Excel对象来处理文件; 2、利用VBA文件处理语句来处理文件; 3、利用FileSystemObject对象来处理文件; 4、利用API函数来处理文件。
集中常用的文件和文件夹操作方法。如复制、剪切、查找、删除等。对文件夹的建立、打开等。用起来很简便
数据恢复可以有效的恢复因为误删错删以及文件出错而引起的文件资料丢失,本文介绍了数据恢复的原理以及具体的方法,常用软件。
C#对文件的常用操作,包含对文件和文件夹的各种处理方法