public static String UnicodeToChinese(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("ISO8859_1"),"gb2312");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}public static String ChineseToUnicode(String s){
try{
if(s==null||s.equals("")) return "";
String newstring=null;
newstring=new String(s.getBytes("gb2312"),"ISO8859_1");
return newstring;
}
catch(UnsupportedEncodingException e)
{
return s;
}
}
{
try
{
String temp_p=str;
byte[] temp_t=temp_p.getBytes("ISO8859-1");
String temp=new String(temp_t);
return temp;
}
catch(Exception e)
{
e.printStackTrace() ;
}
return "null";
}
在j2me中找不到gb2312的,我想找转化的原码
在moto j2me中找不到gb2312的,我想找转化的原码
烤下來,一用就明白
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());
}
}
}
}
}