Android开发从零开始之java-IO流
2013-02-07 01:03
267 查看
package test; import java.io.*; import java.net.*; import org.jsoup.helper.HttpConnection; public class javaIO流 { /** * 作者:钟志钢 * 功能:JavaIO流 * 日期:2013-1-6 * 1, 分类:字节流(二进制,图片,音乐,视频等)和字符流(文本文件:字符,如String等),输入流与输出流 * 2, 字节流InputStream是所有输入流的父类-ByteArrayInputStream、 * StringBufferInputStream、FileInputStream,PipedInputStream * PipedInputStream管道流是从与其它线程共用的管道中读取数据 * 3, 字节流与字符流的转换:InputStreamReader与InputStreamWriter * 2, 文件流FileInputStream与FileOutputStream,切记文件流用完要关闭 * */ public static void main(String[] args) throws Exception{ javaIO流 javaio = new javaIO流(); } public javaIO流() throws Exception{ //文件流:内在与磁盘之间文本文件交互 File f = new File("e:/mytxt.txt"); if(f.exists()){//存在 if(f.isFile()){//是文件 getFileInfo(f);//获得文件相关信息 //String content = getFileContent(f);//得到文件内容 }else if(f.isDirectory()){//是文件夹 System.out.println("文件夹路径" + f.getAbsolutePath()); //获得文件夹下所有文件的集合 File [] filse = f.listFiles(); //获得文件的详细信息 for(File fs : filse){ if(fs.isFile()){ getFileInfo(fs); }else{ System.out.println("文件夹路径" + f.getAbsolutePath()); } } } }else if(!f.exists()){//不存在 f.createNewFile();//创建一个文件 String s = "我是javaIO流-流入文件"; //writeFileContent(f, s); //f.mkdir();//创建一个目录 } //从网上下载文件保存到本地(不分类型) String url = "http://bs.baidu.com/handian/gong1.mp3";//下载地址 String myfile = "e:/my.mp3";//定义一个详细的文件地址用来装将要下载的文件 saveFile(url, myfile); //从网上获得网页(html)内容 String html = getHtml("http://zhidao.baidu.com/question/209423466.html"); System.out.println(html); } private String getHtml(String string) { String html = null; URL url = null; ByteArrayOutputStream bos = null; InputStream is = null; try { url = new URL(string); HttpURLConnection con = (HttpURLConnection) url.openConnection(); //con.setReadTimeout(5*1000); con.setRequestMethod("GET"); is = con.getInputStream(); byte [] buffer = new byte[1024]; //捕获内存缓冲区的数据,转换成字节数组。 bos = new ByteArrayOutputStream(); int len = 0; while((len = is.read(buffer)) != -1){ bos.write(buffer, 0 , len); } byte [] data = bos.toByteArray(); html = new String(data); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(bos != null){ } try { bos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if(is != null){ try { is.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } return html; } private void saveFile(String url, String myfile) { File file = new File(myfile);//根据提供的文件地址建立一个文件 DataInputStream dis = null;//打开一个二进制输入流,用来从网上下载数据到内存 DataOutputStream dos = null;//打开一个二进制输出流,用来保存数据到本地 //连接网络 URL urls = null; try { urls = new URL(url); //打开数据连接 HttpURLConnection connection = (HttpURLConnection) urls.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5*1000);//5秒连接超时 //开始读取数据 dis = new DataInputStream(connection.getInputStream()); //准备保存数据在本地 dos = new DataOutputStream(new FileOutputStream(file)); byte[] buffer = new byte[1024]; int len = 0; while((len = dis.read(buffer)) != -1){ dos.write(buffer, 0, len);//一次保存1K数据,直到完成 } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(dis != null){ try { dis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }if(dos != null){ try { dos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private void writeFileContent(File f, String s) { FileOutputStream fos = null; try { fos = new FileOutputStream(f); fos.write(s.getBytes("GBK"));//把s以GBK(当前程序编码)编码方式写入文件 } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fos!=null){ try { fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private String getFileContent(File f) { String content = ""; FileInputStream fis = null; try { fis = new FileInputStream(f); int l = fis.available();//获得文件长度 byte [] bytes = new byte[l]; content = new String(bytes, 0 , fis.read(bytes));//读取数据 System.out.println(content); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); }finally{ if(fis!=null){//切记关闭数据流 } try { fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return content; } public void getFileInfo (File f){ System.out.println("文件名称" + f.getName()); System.out.println("文件大小" + f.length()); System.out.println("文件路径" + f.getAbsolutePath()); System.out.println("文件所在文件夹" + f.getParent()); } }
相关文章推荐
- Java游戏开发框架LGame-0.2.6发布(含PC及Android版)
- Java(Android)游戏开发框架LGame-0.2.7发布
- C\C++ 程序员从零开始学习Android - 个人学习笔记(四) - java基础 - 数据类型、变量、字符串、数组
- Java游戏开发框架LGame-0.2.6发布(含PC及Android版)
- Android开发从零开始视频教程
- C\C++ 程序员从零开始学习Android - 个人学习笔记(二) - java基础 - 从源代码到运行
- 扩充你的android——使用java开发,还有git
- [原创]Android 基于NDK的JNI开发 C调用java和java调用C的进阶教程
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- Android开发:关于导入ApiDemos后没有R.java文件的问题
- android 应用开发不能自动生成R.java的原因
- 解决新建的Android项目gen目录下没有R.java文件--Android开发经验
- Android-NDK开发之基础--Android JNI实例代码(一)-- 在JNI中执行Java方法--C/C++调用Java
- Android网络应用开发从零开始做起
- android开发问题:java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected imp
- 在Windows上搭建Android的JAVA开发环境图文教程(Eclipse版本)
- Android Java开发环境搭建
- Android-NDK开发之基础--Android JNI有关Java类命名方式
- 开发 Standalone Android Java 应用程序
- java案例开发集锦之io流与多线程的文件删除