请教,JAVA记事本中实现“另存为”功能,怎样才能得到目录呢?
就是当点击“另存为”的时候,怎样才能得到保存的目录路径?JFileChooserjfc=newJFileChooser();jfc.setDialogTitle("打开目...
就是当点击“另存为”的时候,怎样才能得到保存的目录路径?
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("打开目录");
int result = jfc.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
try{
FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile().getName(),true);
fos.write(jaf.getText().getBytes());
fos.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
我这段代码只能得到文件的路径,得不到目录路径。请高手帮忙解决下 展开
JFileChooser jfc = new JFileChooser();
jfc.setDialogTitle("打开目录");
int result = jfc.showOpenDialog(null);
if(result == JFileChooser.APPROVE_OPTION){
try{
FileOutputStream fos = new FileOutputStream(jfc.getSelectedFile().getName(),true);
fos.write(jaf.getText().getBytes());
fos.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
我这段代码只能得到文件的路径,得不到目录路径。请高手帮忙解决下 展开
展开全部
你看看 这个行不http://hi.baidu.com/aimilin/blog/item/24c705ed46f9e42d63d09f6b.html
class OpenClass extends Frame
{
NoteBookFrame notebookframe = null;
FileDialog filedialog_open,filedialog_save;
File file1,file2; //文件字节流对象
FileReader file_reader;//文件字符流对象
FileWriter tofile;
BufferedReader in ;//文件行读取 写入对象
BufferedWriter out;
String s;
boolean issave = false;//用于判断是否是第一次保存
String filesave = null,savename = null; //用于存放文件地址 和文件名
String fileopen = null,filename = null;//用于存放打开文件地址 和文件名
PrintJob p=null;//声明一个要打印的对象
Graphics g=null;//要打印的对象
OpenClass(NoteBookFrame p )
{
notebookframe = p;
filedialog_open = new FileDialog(notebookframe,"打开文件对话框",FileDialog.LOAD);
filedialog_save = new FileDialog(notebookframe,"保存文件对话框",FileDialog.SAVE);
filedialog_open.setVisible(false);
filedialog_save.setVisible(false);
//notebookframe = new NoteBookFrame();
filedialog_open.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_open.setVisible(false); }
}
);//打开文件对话框适配器
filedialog_save.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false); }
}
);//保存文件对话框适配器
}
public void New()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
}
notebookframe.textarea.setText("");
}
public void Open()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
if(n == JOptionPane.NO_OPTION)
{
notebookframe.textarea.setText("");
}
}
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();//返回文件对话框中显示的文件所属的目录
filename = filedialog_open.getFile();//返回当前文件对话框中显示的文件名的字符串表示 如果不存在就返回NULL
try
{
if(filename!=null)file1 = new File(fileopen,filename);
if(file1.isFile())//判断文件是否是正常的文件而不是目录
{
notebookframe.textarea.setText("");
file_reader = new FileReader(file1);
in = new BufferedReader(file_reader);//每次读取一行
while((s = in.readLine())!=null)
notebookframe.textarea.append(s+'\n');
in.close();
file_reader.close();
}
}
catch (IOException e2){ }
}
public void Save()
{
if(!issave&&filename == null)
{
filedialog_save.setVisible(true);
filesave = filedialog_save.getDirectory();
savename = filedialog_save.getFile()+".txt";
issave = true;
}
if(!savename.equals(null+".txt"))
{
try{
if(filename!= null){filesave = fileopen; savename = filename;}
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void AsSave()
{
int n=-2;
filedialog_save.setVisible(true);
String filesave = filedialog_save.getDirectory();
String savename = filedialog_save.getFile()+".txt";
if(!savename.equals(null+".txt"))
{
try{
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void SetUp()
{
JOptionPane.showMessageDialog(this,"在打印的时候用到!","警告对话框",JOptionPane.WARNING_MESSAGE);
}
public void Print()
{
p = getToolkit().getPrintJob(notebookframe,"ok",null);//创建一个Printfjob 对象 p
g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象
g.translate(120,200);//改变组建的位置
notebookframe.textarea.printAll(g);
p.end();//释放对象 g
}
}
// 打开类
class OpenClass extends Frame
{
NoteBookFrame notebookframe = null;
FileDialog filedialog_open,filedialog_save;
File file1,file2; //文件字节流对象
FileReader file_reader;//文件字符流对象
FileWriter tofile;
BufferedReader in ;//文件行读取 写入对象
BufferedWriter out;
String s;
boolean issave = false;//用于判断是否是第一次保存
String filesave = null,savename = null; //用于存放文件地址 和文件名
String fileopen = null,filename = null;//用于存放打开文件地址 和文件名
PrintJob p=null;//声明一个要打印的对象
Graphics g=null;//要打印的对象
OpenClass(NoteBookFrame p )
{
notebookframe = p;
filedialog_open = new FileDialog(notebookframe,"打开文件对话框",FileDialog.LOAD);
filedialog_save = new FileDialog(notebookframe,"保存文件对话框",FileDialog.SAVE);
filedialog_open.setVisible(false);
filedialog_save.setVisible(false);
//notebookframe = new NoteBookFrame();
filedialog_open.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_open.setVisible(false); }
}
);//打开文件对话框适配器
filedialog_save.addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ filedialog_save.setVisible(false); }
}
);//保存文件对话框适配器
}
public void New()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
}
notebookframe.textarea.setText("");
}
public void Open()
{
if(!notebookframe.textarea.getText().equals(""))
{
int n = JOptionPane.showConfirmDialog(notebookframe,"是否保存原有的信息?","询问对话框",JOptionPane.YES_NO_OPTION);
if(n == JOptionPane.YES_OPTION)
{
Save();
}
if(n == JOptionPane.NO_OPTION)
{
notebookframe.textarea.setText("");
}
}
filedialog_open.setVisible(true);
fileopen = filedialog_open.getDirectory();//返回文件对话框中显示的文件所属的目录
filename = filedialog_open.getFile();//返回当前文件对话框中显示的文件名的字符串表示 如果不存在就返回NULL
try
{
if(filename!=null)file1 = new File(fileopen,filename);
if(file1.isFile())//判断文件是否是正常的文件而不是目录
{
notebookframe.textarea.setText("");
file_reader = new FileReader(file1);
in = new BufferedReader(file_reader);//每次读取一行
while((s = in.readLine())!=null)
notebookframe.textarea.append(s+'\n');
in.close();
file_reader.close();
}
}
catch (IOException e2){ }
}
public void Save()
{
if(!issave&&filename == null)
{
filedialog_save.setVisible(true);
filesave = filedialog_save.getDirectory();
savename = filedialog_save.getFile()+".txt";
issave = true;
}
if(!savename.equals(null+".txt"))
{
try{
if(filename!= null){filesave = fileopen; savename = filename;}
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void AsSave()
{
int n=-2;
filedialog_save.setVisible(true);
String filesave = filedialog_save.getDirectory();
String savename = filedialog_save.getFile()+".txt";
if(!savename.equals(null+".txt"))
{
try{
file2 = new File(filesave,savename);
tofile = new FileWriter(file2);
out = new BufferedWriter(tofile);
out.write(notebookframe.textarea.getText(),0,notebookframe.textarea.getText().length());
out.close();
tofile.close();
}
catch (IOException e2){ }
}
}
public void SetUp()
{
JOptionPane.showMessageDialog(this,"在打印的时候用到!","警告对话框",JOptionPane.WARNING_MESSAGE);
}
public void Print()
{
p = getToolkit().getPrintJob(notebookframe,"ok",null);//创建一个Printfjob 对象 p
g = p.getGraphics();//p 获取一个用于打印的 Graphics 的对象
g.translate(120,200);//改变组建的位置
notebookframe.textarea.printAll(g);
p.end();//释放对象 g
}
}
// 打开类
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询