Android开发从零开始之java-IO流

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


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值