InputStream
--FileInputStream
--ByteArrayInputStream
--FilterInputStream
---BufferedInputStream
如:BufferedInputStream input=new BufferedInputStream(new FileInputStream("baidu"));
---DataInputStream
如:
DataOutputStream ds=new DataOutputStream(conn.getOutputStream());
ds.writeBytes("\r\n");
ds.writeBytes(encode(value) + "\r\n");
// 对包含中文的字符串进行转码,此为UTF-8。服务器那边要进行一次解码
private String encode(String value) throws Exception {
return URLEncoder.encode(value, "UTF-8");
}
--ObjectInputStream
如:ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(name));
Member member= (Member) objectInputStream.readObject();
OutputStream
--FileOutputStream
--ByteArrayOutputStream
如:ByteArrayOutputStream out = new ByteArrayOutputStream();
for (int n = -1; (n = input.read(buffer)) != -1;)
out.write(buffer, 0, n); //buffer就是输出的数据,如果控制台想打印就直接输出buffer
System.out.println(new String(out.toByteArray()));
--FilterOutputStream
---BufferedOutputStream
如:
BufferedOutputStream out2=new BufferedOutputStream(new FileOutputStream("baidu"));
byte[] buffer = new byte[1024];
for (int k=0;(k = input.read(buffer)) != -1;) {//input 是BufferedInputStream input=new //BufferedInputStream(new FileInputStream("baidu"));
out2.write(buffer, 0, k);
}
---DataOutputStream(基本类型 ,String)
---PrintStream
--ObjectOutputStream
如:Member b=new Member();
ObjectOutputStream objectOut=new ObjectOutputStream(new FileOutputStream(b.getName())) ;
objectOut.writeObject(b);
利用ByteArrayOutputStream复制文件
public static void saveFiles(String origPath, String destPath) throws Exception {
File file = new File(origPath);
if (!file.exists() || file.isDirectory())
throw new FileNotFoundException();
BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file));
ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
byte[] temp = new byte[1024];
int size = 0;
while ((size = in.read(temp)) != -1) {
out.write(temp, 0, size);
}
in.close();
String str1 = out.toString("gbk"); //得到字符串
File file1 = new File(destPath);
FileOutputStream fos = new FileOutputStream(file1);
fos.write(str1.getBytes("utf-8")); //输出
}
Reader
--StringReader
--CharArrayReader
--InputStreamReader (可以指定编码)
--FileReader
--BufferedReader
如:BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream("z1"),System.getProperty("file.encoding")));
Writer
--StringWriter
--CharArrayWriter
--InputStreamWriter (可以指定编码)
--FileWriter
--BufferedWriter
如:BufferedWriter w=new BufferedWriter(new OutputStreamWriter(new FileOutputStream("OutputStreamWriter")));
char[] data=new char[1024];
int length=0;
while((length = input.read(data)) != -1){
output.write(data,0, length);
System.out.println(data);
}
--PrintWriter
如:
PrintWriter w2=new PrintWriter("OutputStreamWriter2");
PrintWriter w3= new PrintWriter(
new OutputStreamWriter(
new FileOutputStream("OutputStreamWriter2"),"UTF-8"));
URL 读写
public void test(){
String boundary = "--------httppost123";
try {
URL url=new URL("http://www.baidu.com/");
// Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("10.100.201.236", 8080)); //代理
HttpURLConnection connection=(HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setConnectTimeout(40000); // 连接超时为10秒
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
connection.connect();
InputStream in = connection.getInputStream();
BufferedInputStream input=new BufferedInputStream(in);
OutputStream out=new FileOutputStream("baidu");
BufferedOutputStream out2=new BufferedOutputStream(out);
byte[] buffer = new byte[1024];
for (int k=0;(k = input.read(buffer)) != -1;) {
out2.write(buffer, 0, k);
}
connection.disconnect();
System.out.println(new String(out.toByteArray()));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}