import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
class idlg extends Dialog{ //输入对话框
JTextField ar=new JTextField(40);
Button bt=new Button("确定"),bt1=new Button("导入"),bt2=new Button("保存");
idlg(){
super(new Frame(),"输入数组",true);
setLayout(new FlowLayout(1,20,20));
add(ar);
add(bt);
add(bt1);
add(bt2);
setSize(465,200);
bt.addActionListener(new ActionListener(){ //输入数组
public void actionPerformed(ActionEvent e){
String inp = ar.getText();
java.util.StringTokenizer token = new java.util.StringTokenizer(inp, " ,", false);
int[] numbers = new int[token.countTokens()];
for(int i = 0; token.hasMoreTokens(); i++) {
try {
numbers[i] = Integer.parseInt(token.nextToken());
} catch(NumberFormatException nfe) {
ar.setText("请输入数字,并以逗号或空格分开。");
return;
}
}
// inarr=numbers;
setVisible(false);
}
});
bt1.addActionListener(new ActionListener(){ //导入数组
public void actionPerformed(ActionEvent e){
String name="";
FileDialog open=new FileDialog(new Frame(),"导入",FileDialog.LOAD);
if(e.getSource()==bt1){
open.show();
if(open.getFile()==null){return;}
name=open.getDirectory()+File.separator+open.getFile();
FileInputStream fis=null;
String str=null;
try{
fis=new FileInputStream(name);
int size=fis.available();
byte[] bytes=new byte[size];
fis.read(bytes);
str=new String(bytes);
}catch(IOException e1){}
finally{
try{
fis.close();
}catch(IOException e2){}
}
if(str!=null){
ar.setText(str);
}
}
}
});
bt2.addActionListener(new ActionListener(){ //保存数组
public void actionPerformed(ActionEvent e){
String name="";
FileOutputStream fos=null;
String str=ar.getText();
FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE);
if(e.getSource()==bt1){
save.show();
if(save.getFile()==null){return;}
name=save.getDirectory()+File.separator+save.getFile();
try{
fos=new FileOutputStream(name);
fos.write(str.getBytes());
}catch(IOException e1){}
finally{
try{
fos.close();
}catch(IOException e2){}
}
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
}
问题是无法保存文件,点击保存、输入文件名保存后,在保存目录下找不到文件。请大虾们帮帮忙!!
import java.awt.*;
import java.awt.event.*;
import java.lang.String;
class idlg extends Dialog{ //输入对话框
JTextField ar=new JTextField(40);
Button bt=new Button("确定"),bt1=new Button("导入"),bt2=new Button("保存");
idlg(){
super(new Frame(),"输入数组",true);
setLayout(new FlowLayout(1,20,20));
add(ar);
add(bt);
add(bt1);
add(bt2);
setSize(465,200);
bt.addActionListener(new ActionListener(){ //输入数组
public void actionPerformed(ActionEvent e){
String inp = ar.getText();
java.util.StringTokenizer token = new java.util.StringTokenizer(inp, " ,", false);
int[] numbers = new int[token.countTokens()];
for(int i = 0; token.hasMoreTokens(); i++) {
try {
numbers[i] = Integer.parseInt(token.nextToken());
} catch(NumberFormatException nfe) {
ar.setText("请输入数字,并以逗号或空格分开。");
return;
}
}
// inarr=numbers;
setVisible(false);
}
});
bt1.addActionListener(new ActionListener(){ //导入数组
public void actionPerformed(ActionEvent e){
String name="";
FileDialog open=new FileDialog(new Frame(),"导入",FileDialog.LOAD);
if(e.getSource()==bt1){
open.show();
if(open.getFile()==null){return;}
name=open.getDirectory()+File.separator+open.getFile();
FileInputStream fis=null;
String str=null;
try{
fis=new FileInputStream(name);
int size=fis.available();
byte[] bytes=new byte[size];
fis.read(bytes);
str=new String(bytes);
}catch(IOException e1){}
finally{
try{
fis.close();
}catch(IOException e2){}
}
if(str!=null){
ar.setText(str);
}
}
}
});
bt2.addActionListener(new ActionListener(){ //保存数组
public void actionPerformed(ActionEvent e){
String name="";
FileOutputStream fos=null;
String str=ar.getText();
FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE);
if(e.getSource()==bt1){
save.show();
if(save.getFile()==null){return;}
name=save.getDirectory()+File.separator+save.getFile();
try{
fos=new FileOutputStream(name);
fos.write(str.getBytes());
}catch(IOException e1){}
finally{
try{
fos.close();
}catch(IOException e2){}
}
}
}
});
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
dispose();
System.exit(0);
}
});
}
}
问题是无法保存文件,点击保存、输入文件名保存后,在保存目录下找不到文件。请大虾们帮帮忙!!
解决方案 »
- 关于一道static的程序不太理解
- java求解21位数的花朵数
- [奇怪]一个JAVA日期检查的问题
- 关于文件上传,能否评价一下三种上传方法的各自优缺点?(jspsmart,commonupload,orilly)
- 测试JProgressBar的问题,为什么用一个JButton来控制就会出错?
- 开发B/S架构的视频音频聊天(会议)系统,技术上是否可行?
- 高手近来 问下ISO 10646和unicode的区别 在线等
- 初学者,编译没有问题,却不能运行?
- 调查加请教:J2EE软件工程师应具备的核心技术能力
- 关于java网络编程的问题 如何传递一个有图片,有声音的网页给一个本地端口!!!SOS!!!
- ArrayIndexOutOfBoundsException
- JFrame 键盘事件问题
fos=new FileOutputStream(name);
fos.write(str.getBytes());
}catch(IOException e1){
ex.printStackTrace(); // 把你的异常打印出来。呵呵!
}
FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE);
if(e.getSource()==bt1){
上面这段是不是应该是像下面:
String str=ar.getText();
FileDialog save=new FileDialog(new Frame(),"保存",FileDialog.SAVE);
if(e.getSource()==bt2){ 这样应该就ok的拉,别的都没啥问题
if(e.getSource()==bt2){
可以打开“保存”对话框和输入文件名进行保存,但是在保存目录下找不到文件。