java 流 写入文件时出现乱码 怎么办?一个简单的小程序
publicclassWorkerInformationextendsJFrameimplementsActionListener{JTextFieldtf1;JText...
public class WorkerInformation extends JFrame implements ActionListener{
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;
JButton b1=null;
JButton b2=null;
public WorkerInformation (){ //构造方法
super("员工基本信息");
setLayout(new FlowLayout());
setBounds(0,0,600,600);
setVisible(true);
setLayout(null);
JLabel l1=new JLabel("工号:");
JLabel l2=new JLabel("姓名:");
JLabel l3=new JLabel("类别:");
JLabel l4=new JLabel("部门:");
JLabel l5=new JLabel("登录密码:");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
tf5 = new JTextField();
JButton b1=new JButton("添加员工基本信息");
JButton b2=new JButton("查找员工基本信息");
add(l1);add(l2);add(l3);add(l4);add(l5);
add(tf1);add(tf2);add(tf3);add(tf4);add(tf5);
add(b1); //将事件对象加入容器中
add(b2);
l1.setBounds(50,50,80,30);
l2.setBounds(50,100,80,30);
l3.setBounds(50,150,80,30);
l4.setBounds(50,200,80,30);
l5.setBounds(50,250,200,30);
tf1.setBounds(150,50,150,30);
tf2.setBounds(150,100,150,30);
tf3.setBounds(150,150,150,30);
tf4.setBounds(150,200,150,30);
tf5.setBounds(150,250,150,30);
b1.setBounds(150,300,150,30);
b2.setBounds(150,350,150,30);
b1.addActionListener(this); //注册当前容器为事件对象监听者
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if((e.getActionCommand()).equals("添加员工基本信息"))
{
System.out.println("你按下了第一个按钮:添加员工基本信息");
int workerNum=Integer.parseInt(tf1.getText());
String workerName=tf2.getText();
String workerType=tf3.getText();
String department=tf4.getText();
String password=tf5.getText();
Worker w=new Worker(workerNum,workerName,workerType,department,password);
System.out.println(workerNum+workerName+workerType+department+password);
try{
//按数据类型读写
String filepath ="D:\\员工基本信息.txt";
FileOutputStream fileout=new FileOutputStream(filepath);
DataOutputStream w_outfile=new DataOutputStream(fileout);
FileInputStream filein=new FileInputStream(filepath);
DataInputStream w_infile=new DataInputStream(filein);
writeFile(w_outfile,w);
w_outfile.close();
}
catch(IOException e1){
e1.printStackTrace();
}
}
else if((e.getActionCommand()).equals("查找员工基本信息"))
{System.out.println("你按下了第二个按钮:查找员工基本信息");}
}
private void writeFile(DataOutputStream w_outfile, Worker w) {
try{
w_outfile.writeInt(w.workerNum); //这些地方写入文件时出现乱码!!!!
w_outfile.writeUTF(w.workerName);
w_outfile.writeUTF(w.workerType);
w_outfile.writeUTF(w.department);
w_outfile.writeUTF(w.password);
}
catch(IOException e1){
System.out.println(e1.toString());
}
}
} 展开
JTextField tf1;
JTextField tf2;
JTextField tf3;
JTextField tf4;
JTextField tf5;
JButton b1=null;
JButton b2=null;
public WorkerInformation (){ //构造方法
super("员工基本信息");
setLayout(new FlowLayout());
setBounds(0,0,600,600);
setVisible(true);
setLayout(null);
JLabel l1=new JLabel("工号:");
JLabel l2=new JLabel("姓名:");
JLabel l3=new JLabel("类别:");
JLabel l4=new JLabel("部门:");
JLabel l5=new JLabel("登录密码:");
tf1 = new JTextField();
tf2 = new JTextField();
tf3 = new JTextField();
tf4 = new JTextField();
tf5 = new JTextField();
JButton b1=new JButton("添加员工基本信息");
JButton b2=new JButton("查找员工基本信息");
add(l1);add(l2);add(l3);add(l4);add(l5);
add(tf1);add(tf2);add(tf3);add(tf4);add(tf5);
add(b1); //将事件对象加入容器中
add(b2);
l1.setBounds(50,50,80,30);
l2.setBounds(50,100,80,30);
l3.setBounds(50,150,80,30);
l4.setBounds(50,200,80,30);
l5.setBounds(50,250,200,30);
tf1.setBounds(150,50,150,30);
tf2.setBounds(150,100,150,30);
tf3.setBounds(150,150,150,30);
tf4.setBounds(150,200,150,30);
tf5.setBounds(150,250,150,30);
b1.setBounds(150,300,150,30);
b2.setBounds(150,350,150,30);
b1.addActionListener(this); //注册当前容器为事件对象监听者
b2.addActionListener(this);
}
public void actionPerformed(ActionEvent e){
if((e.getActionCommand()).equals("添加员工基本信息"))
{
System.out.println("你按下了第一个按钮:添加员工基本信息");
int workerNum=Integer.parseInt(tf1.getText());
String workerName=tf2.getText();
String workerType=tf3.getText();
String department=tf4.getText();
String password=tf5.getText();
Worker w=new Worker(workerNum,workerName,workerType,department,password);
System.out.println(workerNum+workerName+workerType+department+password);
try{
//按数据类型读写
String filepath ="D:\\员工基本信息.txt";
FileOutputStream fileout=new FileOutputStream(filepath);
DataOutputStream w_outfile=new DataOutputStream(fileout);
FileInputStream filein=new FileInputStream(filepath);
DataInputStream w_infile=new DataInputStream(filein);
writeFile(w_outfile,w);
w_outfile.close();
}
catch(IOException e1){
e1.printStackTrace();
}
}
else if((e.getActionCommand()).equals("查找员工基本信息"))
{System.out.println("你按下了第二个按钮:查找员工基本信息");}
}
private void writeFile(DataOutputStream w_outfile, Worker w) {
try{
w_outfile.writeInt(w.workerNum); //这些地方写入文件时出现乱码!!!!
w_outfile.writeUTF(w.workerName);
w_outfile.writeUTF(w.workerType);
w_outfile.writeUTF(w.department);
w_outfile.writeUTF(w.password);
}
catch(IOException e1){
System.out.println(e1.toString());
}
}
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询