将编码转成gbk或者gb2312试一下啊。在写入时也应该转换

解决方案 »

  1.   

    raf.writeChars(new String(rstemp.getString(2).getBytes("GB2312"),"ISO-8859-1"));
    还是不行啊,还是乱码?该怎么转换啊?
      

  2.   

    无论是写入还是读出建议先字符转换。
    尝试:
    raf.writeChars(new String(rstemp.getString(2).getBytes("ISO8859-1"),"GB2312"));或者:
    raf.writeChars(new String(rstemp.getString(2).getBytes("ISO8859-1"));
      

  3.   

    显示若还是乱码,再执行转换试试:
    str=new String (str.getBytes("ISO-8859-1"),"gb2312");
    若还是不成,试着写入读出都反过来转看看
    其实乱码不外乎字符集的转换
      

  4.   

    我給你一個我寫的小程序,用它去打開你的文件看看.(你可選擇不同的編碼方式打開你的文件)
    package untitled1;import javax.swing.UIManager;public class FormatsChange {
      boolean packFrame = false;  //Construct the application
      public FormatsChange() {
        Frame1 frame = new Frame1();
        //Validate frames that have preset sizes
        //Pack frames that have useful preferred size info, e.g. from their layout
        if (packFrame)
          frame.pack();
        else
          frame.validate();
        frame.setSize(600,550);
        frame.setVisible(true);
      }  //Main method
      public static void main(String[] args) {
        try  {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        }
        catch(Exception e) {
        }
        new FormatsChange();
      }
    } package untitled1;import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.event.*;
    import com.borland.jbcl.layout.*;
    import java.io.*;
    public class Frame1 extends JFrame implements ActionListener{
      XYLayout xYLayout1 = new XYLayout();
      JComboBox jComboBox1 = new JComboBox();
      JLabel jLabel1 = new JLabel();
      JButton jButton1 = new JButton();
      JTextArea jTextArea1 = new JTextArea();
      JLabel jLabel2 = new JLabel();
      JComboBox jComboBox2 = new JComboBox();
      JLabel jLabel3 = new JLabel();
      JButton jButton2 = new JButton();
      String[] selectEncoding=new String[7];
      JScrollPane jcs=null;
      JPanel jp=new JPanel();  //Construct the frame
      public Frame1() {
        enableEvents(AWTEvent.WINDOW_EVENT_MASK);
        try  {
          jbInit();
        }
        catch(Exception e) {
          e.printStackTrace();
        }
      }  //Component initialization
      private void jbInit() throws Exception  {
        this.getContentPane().setLayout(xYLayout1);
        this.setTitle("Frame Title");
        xYLayout1.setHeight(576);
        xYLayout1.setWidth(559);
        jLabel1.setText("選擇打開文件的編碼方式");
        jButton1.setText("瀏覽");
        jTextArea1.setText("jTextArea1");
        jLabel2.setText("按");
        jLabel3.setText("另存文件");
        jButton2.setText("ok");
        jp.setLayout(new BorderLayout());
        jp.add("Center",jTextArea1);
        jcs=new JScrollPane(jp);
        this.getContentPane().add(jComboBox1, new XYConstraints(161, 22, 218, 26));
        this.getContentPane().add(jButton1, new XYConstraints(398, 21, 137, 28));
        this.getContentPane().add(jLabel1, new XYConstraints(20, 25, 135, 23));
        this.getContentPane().add(jcs, new XYConstraints(28, 78, 509, 373));
        this.getContentPane().add(jLabel2, new XYConstraints(28, 475, 24, 29));
        this.getContentPane().add(jComboBox2, new XYConstraints(48, 479, 168, -1));
        this.getContentPane().add(jLabel3, new XYConstraints(223, 484, 56, 21));
        this.getContentPane().add(jButton2, new XYConstraints(295, 479, 173, 27));
        selectEncoding[0]="GB2312";
        selectEncoding[1]="Big5";
        selectEncoding[2]="EUC_TW";
        selectEncoding[3]="CNS11643";
        selectEncoding[4]="Cp948";
        selectEncoding[5]="ISO2022CN";
        selectEncoding[6]="Unicode";    jButton1.addActionListener(this);
        jButton2.addActionListener(this);    for(int i=0;i<7;i++){
          jComboBox1.addItem(selectEncoding[i]);
          jComboBox2.addItem(selectEncoding[i]);
          }
        }  //Overridden so we can exit on System Close
      protected void processWindowEvent(WindowEvent e) {
        super.processWindowEvent(e);
        if(e.getID() == WindowEvent.WINDOW_CLOSING) {
          System.exit(0);
        }
      }  public void actionPerformed(ActionEvent event){
        String encoding=(String)jComboBox1.getSelectedItem();
        DataInputStream is=null;
        InputStreamReader dis=null;
        DataOutputStream os=null;
        OutputStreamWriter dos=null;
        if(event.getSource().equals(jButton1)){
          FileDialog fd=new FileDialog(this,"SelectFile",0);
          fd.show();
          if(fd.getFile()!=null){
           String path=fd.getDirectory();
           String sfile=fd.getFile();
           jTextArea1.setText("");
           jTextArea1.setForeground(Color.blue);
           try{
             is=new DataInputStream(new FileInputStream(path+sfile));
             dis=new InputStreamReader(is,encoding);
             char[] ss=new char[1024];
             int k=dis.read(ss);
             while(k!=-1){
               jTextArea1.append(new String(ss,0,k));
               ss=new char[1024];
               k=dis.read(ss);
             }
             dis.close();
             is.close();
           }catch(Exception e){
             System.out.println(e.getMessage());
             }
           }
        }    if(event.getSource().equals(jButton2)){
          encoding=(String)jComboBox2.getSelectedItem();
          FileDialog fd=new FileDialog(this,"SaveFile",1);
          fd.show();
          if(fd.getFile()!=null){
           String path=fd.getDirectory();
           String sfile=fd.getFile();
            try{
             os=new DataOutputStream(new FileOutputStream(path+sfile));
             dos=new OutputStreamWriter(os,encoding);
             String tText=jTextArea1.getText();
             dos.write(tText);
             dos.close();
             os.close();
           }catch(Exception e){
             System.out.println(e.getMessage());
             }
           }
        }
      }
    }                           如果成功了別忘了給分
      

  5.   

    我写入的是一个TXT文件,如果我只想让它双击打开后就能看到数据,该怎么做呢?
    raf.writeChars(new String(rstemp.getString(2).getBytes("GB2312"),"Default"));
    不行.
      

  6.   

    我用的是XP中文版,应该是GB2312吧.数据库是SQL7.应该没有问题啊.
      

  7.   

    我說的是你的平台是檢體(中文中國)還是繁體(中文台灣)
    如果是檢體平台那你輸出的時候最好將輸出六轉換成GB2312
    如果是繁體平台那你輸出的時候最好將輸出六轉換成ms950
    你可以看看我上變的那端代碼;也可以看看蔡学镛专栏(CSDN首頁上有)
    中關羽編碼的文章