java获取网页信息时候,将复制的字符串添加到JTextArea()里面如何做,不知道代码有什么错误,帮忙看下
privatevoidshowFile(){URLurl=null;FileOutputStreamfos=null;InputStreamis=null;Stringi...
private void showFile()
{
URL url = null;
FileOutputStream fos = null;
InputStream is = null;
String infile = null;
try
{
for (int i = 0; i < 1; i++)
{
url = new URL(jtfURL.getText().trim());
byte bytes[] = new byte[1024 * 1000];
int index = 0;
is = url.openStream();
int count = is.read(bytes, index, 1024 * 100);
while (count != -1)
{
index += count;
count = is.read(bytes, index, 1);
}
fos = new FileOutputStream(infile);
fos.write(bytes, 0, index);
}
//读取一行,即可将行文本区域
while (infile != null) {
jtaFile.append(infile + '\n');
}
jlblStatus.setText("File loaded successfully");
}
catch (FileNotFoundException e) {
jlblStatus.setText("URL " + url + " not found.");
}
catch (IOException e) {
jlblStatus.setText(e.getMessage());
}
finally {
try {
is.close();fos.close();
}
catch (IOException ex) {}
}
}
不知道怎么讲fos中写入的字符转到JTextArea()中~ 展开
{
URL url = null;
FileOutputStream fos = null;
InputStream is = null;
String infile = null;
try
{
for (int i = 0; i < 1; i++)
{
url = new URL(jtfURL.getText().trim());
byte bytes[] = new byte[1024 * 1000];
int index = 0;
is = url.openStream();
int count = is.read(bytes, index, 1024 * 100);
while (count != -1)
{
index += count;
count = is.read(bytes, index, 1);
}
fos = new FileOutputStream(infile);
fos.write(bytes, 0, index);
}
//读取一行,即可将行文本区域
while (infile != null) {
jtaFile.append(infile + '\n');
}
jlblStatus.setText("File loaded successfully");
}
catch (FileNotFoundException e) {
jlblStatus.setText("URL " + url + " not found.");
}
catch (IOException e) {
jlblStatus.setText(e.getMessage());
}
finally {
try {
is.close();fos.close();
}
catch (IOException ex) {}
}
}
不知道怎么讲fos中写入的字符转到JTextArea()中~ 展开
2个回答
展开全部
不用输出到文件中去,可以使用StringBuilder 保存在内存中
StringBuilder sBuilder = new StringBuilder();
int count = is.read(bytes, index, 1024 * 100);
while ((count =is.read(bytes))!= -1) {
sBuilder.append(new String(bytes,0,count));
}
输出到TextArea
jTextArea.setText(sBuilder.toString());
StringBuilder sBuilder = new StringBuilder();
int count = is.read(bytes, index, 1024 * 100);
while ((count =is.read(bytes))!= -1) {
sBuilder.append(new String(bytes,0,count));
}
输出到TextArea
jTextArea.setText(sBuilder.toString());
追问
追答
缺东西,应该是取得数据长度计算错误
int count = 0;
while ((count =is.read(bytes))!= -1) {
sBuilder.append(new String(bytes,0,count));
}
你试试这个,上面的代码把第一次读的丢失了,
如果是有乱码需要 做encoding
String st = new String(bytes,0,count,"UTF-8");
sBuilder.append(st);
你要先查一下,页面的编码设置是什么,如果是GBK
可以要写成
String st = new String(bytes,0,count,"GBK");
2013-08-23
展开全部
fos = new FileOutputStream(infile);
fos.write(bytes, 0, index);
}
//读取一行,即可将行文本区域
while (infile != null) {
jtaFile.append(infile + '\n');
}
--------------------------------修改成
fos = new FileOutputStream(infile);
fos.write(bytes, 0, index);
jtaFile.append(infile + "\n");
}
fos.write(bytes, 0, index);
}
//读取一行,即可将行文本区域
while (infile != null) {
jtaFile.append(infile + '\n');
}
--------------------------------修改成
fos = new FileOutputStream(infile);
fos.write(bytes, 0, index);
jtaFile.append(infile + "\n");
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询