Java指定编码读取与保存文件

在XP平台,指定GBK读取,解决乱码问题

import java.awt.*;
import java.awt.event.*;
import java.io.*;

import javax.swing.*;
import javax.swing.border.*;

public class Match_File extends JFrame implements ActionListener {

private TextArea Txt;
private FileDialog saveDia, openDia;
private File file;
JButton Open_File, Save_File, Exit;
JLabel JL1, JL2,FileInfo,FilePath;
JTextField JT1;

public Match_File() {
this.setLayout(new FlowLayout(FlowLayout.LEFT));

Txt = new TextArea("请点击打开文件,导入数据", 10, 42);

openDia = new FileDialog(this, "打开", FileDialog.LOAD);
saveDia = new FileDialog(this, "保存", FileDialog.SAVE);

add(showBorder1(new TitledBorder("文件类型")));

this.add(Txt);
add(showBorder2());
int width = Toolkit.getDefaultToolkit().getScreenSize().width;
int height = Toolkit.getDefaultToolkit().getScreenSize().height;
this.setLocation(width / 2 - 300, height / 2 - 150);

this.setVisible(true);
this.setSize(335, 310);
this.setResizable(false);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setTitle("词条过滤器");
}

public JPanel showBorder2() {
JPanel JP = new JPanel();

JP.setLayout(new FlowLayout());
FileInfo = new JLabel(" 文件路径: ");
FilePath = new JLabel(" ");
JT1 = new JTextField(35);
JT1.setEditable(false);

JP.add(FileInfo);
JP.add(JT1);
return JP;
}
public JPanel showBorder1(Border B) {
JPanel JP = new JPanel();
JP.setLayout(new FlowLayout());
Open_File = new JButton(" 打开文件 ");
Open_File.addActionListener(this);
JL1 = new JLabel(" ");
Save_File = new JButton(" 保存文件 ");
Save_File.addActionListener(this);
JL2 = new JLabel(" ");
Exit = new JButton("退出");
Exit.addActionListener(this);
JP.add(Open_File);
JP.add(JL1);
JP.add(Save_File);
JP.add(JL2);
JP.add(Exit);
JP.setBorder(B);
return JP;
}
public static void main(String[] args) {

try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
.getInstalledLookAndFeels()) {
if ("Windows Classic".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception ex) {
ex.printStackTrace();
}

java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Match_File().setVisible(true);
}
});

}

@Override
public void actionPerformed(ActionEvent e) {

if (e.getSource() == this.Open_File) {
openDia.setVisible(true);
String dirPath = openDia.getDirectory();
String fileName = openDia.getFile();
System.out.println(dirPath + "...." + fileName);
if (dirPath == null || fileName == null)
return;
Txt.setText("");
File file = new File(dirPath, fileName);
String Path = dirPath+fileName;
FileInfo.setText("文件打开路径:");
//FilePath.setText(Path);
JT1.setText(Path);
try {

BufferedReader bufr = new BufferedReader(new InputStreamReader(
new FileInputStream(file), "gbk"));
String line = null;
while ((line = bufr.readLine()) != null) {
String[] New_Tmp = line.split("=");
if (New_Tmp.length > 1) {
Txt.append(New_Tmp[1] + "\r\n");
} else {
Txt.append(line + "\r\n");
}

}
bufr.close();
} catch (IOException ex) {

throw new RuntimeException("读取失败");
}

} else if (e.getSource() == this.Save_File) {

if (file == null) {
saveDia.setVisible(true);
String dirPath = saveDia.getDirectory();
String fileName = saveDia.getFile();

if (dirPath == null || fileName == null)
{
return;
}
file = new File(dirPath, fileName);
String Path = dirPath+fileName;
FileInfo.setText("文件保存路径:");
//FilePath.setText(Path);
JT1.setText(Path);
}


try {

OutputStreamWriter write = new OutputStreamWriter(
new FileOutputStream(file), "gbk");
BufferedWriter bufw = new BufferedWriter(write);

String text = Txt.getText();
bufw.write(text);
bufw.close();
JOptionPane.showMessageDialog(null, "文件已保存");

} catch (Exception ex) {
ex.printStackTrace();
}

} else {
JOptionPane.showMessageDialog(null, "程序现在退出");
System.exit(0);
}
}
}


[img]http://dl2.iteye.com/upload/attachment/0101/5214/a25ddb68-ffff-35e4-a947-d7ac728ba503.jpg[/img]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值