如题! 问下如何解决写入文本文件时的乱码问题 乱码只有1种 为黑点 成长方形
代码如下
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;
public class SimpleClient { public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.yahoo.com.cn");
client.executeMethod(method); // 打印服务器返回的状态 System.out.println(method.getStatusLine()); // 打印返回的信息
StringBuffer rets = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream()));
String s = "";
while ((s = br.readLine()) != null) {
rets.append(s + "\n");
}
s = "";
br.close();
try {
FileOutputStream path = new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\xxx.txt", true);
OutputStreamWriter in = new OutputStreamWriter(path, "gb2312");
BufferedWriter bw = new BufferedWriter(in);
bw.write(rets.toString());
} catch (IOException e) {
e.printStackTrace();
}
// 释放连接 method.releaseConnection(); }
}
代码如下
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;import org.apache.commons.httpclient.*;import org.apache.commons.httpclient.methods.*;
public class SimpleClient { public static void main(String[] args) throws IOException { HttpClient client = new HttpClient(); HttpMethod method = new GetMethod("http://www.yahoo.com.cn");
client.executeMethod(method); // 打印服务器返回的状态 System.out.println(method.getStatusLine()); // 打印返回的信息
StringBuffer rets = new StringBuffer();
BufferedReader br = new BufferedReader(new InputStreamReader(method
.getResponseBodyAsStream()));
String s = "";
while ((s = br.readLine()) != null) {
rets.append(s + "\n");
}
s = "";
br.close();
try {
FileOutputStream path = new FileOutputStream("C:\\Documents and Settings\\Administrator\\桌面\\xxx.txt", true);
OutputStreamWriter in = new OutputStreamWriter(path, "gb2312");
BufferedWriter bw = new BufferedWriter(in);
bw.write(rets.toString());
} catch (IOException e) {
e.printStackTrace();
}
// 释放连接 method.releaseConnection(); }
}
解决方案 »
- 对于一个类的构造方法,一般小括号都带有一个参数,那么这个参数在内存中的解释是什么?用途是什么?
- CGLIB动态代理生成问题
- XMLReader 解析远程流问题,急救。。。
- 成立java开发团队
- 谁知道Bits和LinkedList这2个类是做什么的?麻烦您说一下!!
- 除了Unsafe类,java如何操作内存
- 郁闷中已经抽了5支烟了,能帮我搞定让我干啥都行!
- 目前已公认JAVA在PC机上比C++ 慢20倍, 请问J2em在PDA上面会比PDA的专用开发工具慢多少呢?
- springmvc怎么读取本地图片显示到JSP呢?我现在用的流读取。请各位大拿帮忙看下!
- Java中List元素全部被最后一个覆盖的问题
- SWT Table如何编辑空的单元格
- 数组复制
P.S. 写完文件没关闭流
String getResponseBody = method.getResponseBodyAsString();
就直接返回网页的内容了,不用自己一行一行去读
win xp sp2
jdk6u17
file.encoding=GBK
参考一下一个简单的记事本import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;//加滚动条
import javax.swing.JTextArea; /** 记事本:包括菜单:
文件:新建/打开/保存/另存/退出
编辑:撤销/剪切/复制/粘贴/删除/查找/替换/全选
帮助:关于记事本
*/
public class Notepad implements ActionListener{
JFrame jf = new JFrame("记事本");
JTextArea jta = new JTextArea(10,20);//记事本,显示纯文本的多行区域
public Notepad(){
String[] menuLabel ={"文件", "编辑","帮助"};
String[][] miLabel ={{"新建", "" , "打开", "保存", "另存","" , "退出"},
{"撤销", "剪切", "复制", "粘贴", " 删除", "","查找", "替换", "" ,"全选"}, {" 关于记事本"} };
JMenuBar jmb = new JMenuBar();
for(int i=0;i<menuLabel.length;i++){
JMenu jm = new JMenu(menuLabel[i]);
jmb.add(jm);
for(int j=0;j<miLabel[i].length;j++){
if("".equals(miLabel[i][j])){
jm.addSeparator();
}else{
JMenuItem jmi = new JMenuItem(miLabel[i][j]);
jm.add(jmi);
jmi.addActionListener(this);
}
}
}
jf.setJMenuBar(jmb);//JFrame加菜单栏时用setJmenuBar()
jf.add(new JScrollPane(jta));//加滚动条
jf.setLocation(300,300);//设计出现的位置
jf.setSize(400,400);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
@Override
public void actionPerformed(ActionEvent e) {
String comm = e.getActionCommand();
if("新建".equals(comm)){
jta.setText("");
}else if("打开".equals(comm)){
//FileDialog 类显示一个对话框窗口,用户可以从中选择文件
//由于它是一个模式对话框,当应用程序调用其 show 方法来显示对话框时,
//它将阻塞其余应用程序,直到用户选择一个文件
FileDialog fd = new FileDialog(jf,"请选择要打开的文件",FileDialog.LOAD);
fd.setVisible(true);
}else if("保存".equals(comm)){
}else if("另存".equals(comm)){
}else if("退出".equals(comm)){
}else if("撤销".equals(comm)){
}else if("剪切".equals(comm)){
}else if("复制".equals(comm)){
}else if("粘贴".equals(comm)){
}else if("删除".equals(comm)){
}else if("查找".equals(comm)){
}else if("替换".equals(comm)){
}else if("全选".equals(comm)){
}else{//关于记事本
}
}
public static void main(String[] args) {
new Notepad();
}
}
楼主用“\n”来表示一行,用notepad打开就会在每一个“\n”处看到黑块。
改用“\r\n”表示一行就可以了。
{
try
{
File file = new File(path);
FileWriter fw = new FileWriter(file);
fw.write(text.getText());
fw.close();
} catch (IOException e)
{
System.out.println("保存文件出错");
}
}