本帖最后由 姚玉鹏 于 2012-6-8 10:25 编辑
- import java.awt.*;
- import java.awt.event.*;
- import java.io.*;
- class test
- {
- private Frame f;
- private File fe;
- private MenuBar mb;
- private Menu m;
- private MenuItem mload,msave,mclose;
- private TextArea ta;
- test()
- {
- ini();
- }
- public void ini()
- {
- f=new Frame();
- ta=new TextArea();
- mb=new MenuBar();
- m=new Menu("文件");
- mload=new MenuItem("打开");
- msave=new MenuItem("保存");
- mclose=new MenuItem("关闭");
- f.setBounds(200,100,500,400);
- f.setLayout(new FlowLayout());
- f.add(ta);
- f.setMenuBar(mb);
- mb.add(m);
- m.add(mload);
- m.add(msave);
- m.add(mclose);
- method();
- f.setVisible(true);
- }
- public void method()
- {
- f.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }});
- mclose.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- System.exit(0);
- }
- });
- mload.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- try{
- FileDialog fdg=new FileDialog(f,"",FileDialog.LOAD);
- fdg.setVisible(true);
- String dirpath=fdg.getDirectory();
- String filepath=fdg.getFile();
-
- if(dirpath==null||filepath==null){
- //System.out.println(dirpath+"::::"+filepath);
- return;}
- else{
- //System.out.println(dirpath+"::::"+filepath);
- ta.setText("");
- fe=new File(dirpath+filepath);
- byte[] b=new byte[1024*1024];
- int len;
- BufferedInputStream bis=new BufferedInputStream(new FileInputStream(fe));
- while((len=bis.read(b))!=-1)
- {
- ta.append(new String(b,0,len)+"\r\n");
-
- }}
- }
- catch (Exception ex)
- {
- throw new RuntimeException("。。。。");
- }
-
- }
-
- }
- );
- msave.addActionListener(new ActionListener(){
- public void actionPerformed(ActionEvent e){
- try
- {
- String textstr=ta.getText();
- if(fe==null)
- {
- FileDialog fdg=new FileDialog(f,"",FileDialog.SAVE);
- fdg.setVisible(true);
- String path=fdg.getDirectory()+fdg.getFile();
- // System.out.println(fdg.getDirectory());打印结果为null;
- // System.out.println(fdg.getFile());打印结果为null;
- // System.out.println(path);打印结果为nullnull;
- if(path==null))//这步判断无效....
- return;
- else
- fe=new File(path);
- }
- PrintStream ps=new PrintStream(new FileOutputStream(fe),true);
- ps.println(textstr);
- }
- catch (Exception ex)
- {
- throw new RuntimeException(";;;;");
- }
- }
- });
- }
- public static void main(String[] args)
- {
- new test();
- }
- }
复制代码 null+null为什么等于nullnull
这里
// System.out.println(fdg.getDirectory());打印结果为null;
// System.out.println(fdg.getFile());打印结果为null;
// System.out.println(path);打印结果为nullnull;
if(path==null))//这步判断无效....
return;
难道是自动强转么???????
|
|