package chj.io1;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
/**
* 指定字符集
* @author Administrator
*
*/
public class ConvertTest02 {
public static void main(String[] args) {
//操作网络流,下载百度的源代码
BufferedReader reader=null;
BufferedWriter writer=null;
try{reader=
new BufferedReader(
new InputStreamReader(
new URL("https://www.baidu.com").openStream(),"UTF-8"));
writer=
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("baidu.html"),"UTF-8"));
//操作(读取)
String msg="";
while((msg=reader.readLine())!=null) {
// System.out.println(msg);
writer.write(msg);
writer.newLine();
}
writer.flush();
}catch(IOException e) {
System.out.println("操作异常");
}finally {
if(null!=writer){
try {
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(null!=reader) {
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
【JAVA】IO之转换流:转换指定字符集
最新推荐文章于 2023-01-24 22:43:46 发布