如题! 问下如何解决写入文本文件时的乱码问题 乱码只有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(); }
}

解决方案 »

  1.   

    很正常,没遇到lz所说的问题。
    P.S. 写完文件没关闭流
      

  2.   

    client.executeMethod(method); 
    String getResponseBody = method.getResponseBodyAsString();
    就直接返回网页的内容了,不用自己一行一行去读
      

  3.   

    OutputStreamWriter in = new OutputStreamWriter(path, "gb2312"); 换个字符集看看?
      

  4.   

    我把上面的代码编译运行了一遍,没有出现lz遇到的问题。
    win xp sp2
    jdk6u17
    file.encoding=GBK
      

  5.   

    你用txt后缀文件打开就有小黑点
      

  6.   

    lz很刻苦,深夜还用功学习。
    参考一下一个简单的记事本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();
    }
    }
      

  7.   

    就是回车换行的问题。
    楼主用“\n”来表示一行,用notepad打开就会在每一个“\n”处看到黑块。
    改用“\r\n”表示一行就可以了。
      

  8.   

    http://blog.csdn.net/JavaAlpha/archive/2009/05/08/4161752.aspx楼主可以参考一下void writerFile(String path) // 保存文件内容
     {
      try
      {
       File file = new File(path);
       FileWriter fw = new FileWriter(file);
       fw.write(text.getText());
       fw.close();
      } catch (IOException e)
      {
       System.out.println("保存文件出错");
      }
     }