当我将文本显示到我的文本区域时,我遇到了文本编码问题。
问题是当有这样的字符时:é à è
我在我的文本区域中? ? ?
有
这是读取我的文件的代码部分:
private void importerActionPerformed(java.awt.event.ActionEvent evt) {
jTabbedPane1.setSelectedIndex(0);
try {
JFileChooser explorer = new JFileChooser(chemin);
int answer = explorer.showOpenDialog(this);
if (answer == JFileChooser.APPROVE_OPTION) {
chemin = explorer.getCurrentDirectory().getAbsolutePath();
String name = explorer.getSelectedFile().getCanonicalPath();
System.out.println("name : "+name);
texte.setText("");
File file = new File(name);
try {
DataInputStream in = new DataInputStream(new FileInputStream(file));
String result = in.readUTF();
texte.setText(result);
in.close();
System.out.println("Erreur la");
} catch (IOException e) {
DataInputStream in = new DataInputStream(new FileInputStream(file));
String result = null;
result = "";
byte[] buff = new byte[2048];
int read = in.read(buff, 0, 2048);
while (read >= 0) {
String substr = new String(buff, 0, read);
result += substr;
read = in.read(buff, 0, 2048);
}
// System.out.println(result);
Charset charset = Charset.forName("UTF-8");
result = charset.decode(charset.encode(result)).toString();
texte.setText(result);
in.close();
//System.out.println("Erreur la2");
}
}
} catch (Exception err) {
JOptionPane.showMessageDialog(this, "Erreur lors du chargement du fichier", "Error", JOptionPane.WARNING_MESSAGE);
}
}
我的文本区域是:texte.setText(result);
你有什么主意吗?