关于流问题

lashengcrh 2007-10-23 02:04:18
try {
URL conn= new URL("http://www.sina.com/");
InputStream in=conn.openStream();
int n=in.available();
byte[] buf=new byte[1024];
while((n=in.read(buf))>=0){
System.out.write(buf,0,n);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
}

1、如何将in中的数据写入文件呢?
2、如果网页上有中文,如何正确写入文件而不会出现乱码?
谢谢各位指点
...全文
130 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
tubage408 2007-11-02
  • 打赏
  • 举报
回复
package demo;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;

public class IoTest {

/**
* @param args
*/
public static void main(String[] args) {
InputStream is = null;
String str = new String();
FileWriter fw = null;
BufferedWriter bw = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
URL url = new URL("http://www.sina.com/");
is = url.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
}catch(IOException e1){
e1.printStackTrace();
}
isr = new InputStreamReader(is);
br = new BufferedReader(isr);
try {
fw = new FileWriter("D:\\test\\1.html");
bw = new BufferedWriter(fw);
while((str = br.readLine())!=null){
System.out.println(str);
bw.write(str);
bw.newLine();
bw.flush();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bw.close();
fw.close();
br.close();
isr.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
「已注销」 2007-11-02
  • 打赏
  • 举报
回复
用字符流,不要用字节流直接读取,否则很容易出现非ascii码被截断出现乱码
lashengcrh 2007-11-02
  • 打赏
  • 举报
回复
up
lashengcrh 2007-11-01
  • 打赏
  • 举报
回复
而且下载的网页格式很乱,不知如何下载完全的,单一的网页文件?
lashengcrh 2007-11-01
  • 打赏
  • 举报
回复
<!-- END Nielsen//NetRatings SiteCensus V5.2 -->
</body>
ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2
JDWP exit error AGENT_ERROR_NO_JNI_ENV(183): [../../../src/share/back/util.c:820]
</html>
debug:
生成成功(总时间:1 分钟 46 秒)

我在netbean中测试以上代码,最后出现报错,怎么办?
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复
少了一行

再来


FileOutputStream fout = null;

try {
fout = new FileOutputStream("d://temp//1.html");
URL conn= new URL("http://www.sina.com/");
InputStream in=conn.openStream();
int n=in.available();
byte[] buf=new byte[1024];
while((n=in.read(buf)) >=0){
System.out.write(buf,0,n);
fout.write(buf);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fout != null) {
try {
fout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
hivvyeah 2007-10-24
  • 打赏
  • 举报
回复

try {
fout = new FileOutputStream("d://temp//1.html");
URL conn= new URL("http://www.sina.com/");
InputStream in=conn.openStream();
int n=in.available();
byte[] buf=new byte[1024];
while((n=in.read(buf)) >=0){
System.out.write(buf,0,n);
fout.write(buf);
}
in.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(fout != null) {
try {
fout.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧