---
title: "java下载文件"
author: "ericwzc"
date: "2012-07-16T18:39:56Z"
tags: []
categories: ["编程语言"]
slug: "java"
draft: false
lastmod: "2012-07-16T21:18:37Z"
id: 1595538
url: "https://ericwzc.iteye.com/blog/1595538"
---

\*Stream ----- byte oriented  
\*der       ----- char oriented

Buffered\*----- add buffer  
Print*     ----- format

 

```
		URL url = new URL(
				"http://docs.oracle.com/javase/tutorial/networking/sockets/examples/KnockKnockServer.java");
		URLConnection uc = url.openConnection();
		InputStream in = null;
		OutputStream out = null;

		try {
			in = new BufferedInputStream(uc.getInputStream());
			out = new BufferedOutputStream(new FileOutputStream("test1"));
			int c;
			while ((c = in.read()) != -1) {
				out.write(c);
			}
		} catch (Exception ex) {
			// do sth
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.close();
			}
		}
```