首页>代码>java swing实现的学生教师选课管理系统代码>/NotePadTest/src/FileOperation/FileOperation.java
package FileOperation;
import Main.MainFrame;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;
/**
 *@description ��ϣ�ʵ�ֶ��ļ��˵��µ�ȫ������
 * @author С·
 * @date 2013-05-07
 */
public class FileOperation {
    private MainFrame mf;
    public FileOperation(MainFrame mf)
    {
        this.mf = mf;
    }
    //ʵ�ְ�Ŀ���ı����ݶ�����ǰ�ı����еķ�������Ҫ�ѱ��򿪵��ļ���·����������
    public void readTo()
    {
           JFileChooser chooser = new JFileChooser("C:/Documents and Settings/Administrator/����/�¼��±��������ļ���");
           chooser.setSelectedFile(new File("*.txt"));//����Ĭ��ѡ���ļ����
           FileNameExtensionFilter filter = new FileNameExtensionFilter("txt & TXT�ı��ļ�", "txt", "TXT");//���ÿ�ѡ�ļ���׺��
           chooser.setAcceptAllFileFilterUsed(false);//ȡ�������ļ�ѡ��
           chooser.setFileFilter(filter);
           while(chooser.showOpenDialog(mf)==0)
           {
              //��ʱ�ļ��϶���.txt��ʽ����.TXT��ʽ�ģ���˲���Ҫ���жϺ�׺����,ֻ��Ҫ�ж��Ƿ�ѡ�����ĵ��ļ�
              File file = chooser.getSelectedFile();
              mf.set_tempfile(file);//���浱ǰ�򿪵��ļ���·��
              if(file.exists())
               {
                  mf.setTitle(file.getName());
                  try {
                       BufferedReader reader = new BufferedReader(new FileReader(file.getAbsoluteFile()));
                  try {
                      String tmp = reader.readLine();
                      while(tmp!=null)
                       {
                           mf.getMainText().append(tmp+"\n");
                           tmp = reader.readLine();
                       }
                       } catch (IOException ex) {
                                JOptionPane.showMessageDialog(mf, "��ȡ�ļ�ʧ�ܣ�");
                            }
                           
                            //������
                        } catch (FileNotFoundException ex) {
                            JOptionPane.showMessageDialog(mf, "��ȡ�ļ�·��ʧ�ܣ�");
                        }
                    break;
                }
          }
          //�ɹ���ȡ�ļ�֮����changed��ʼֵΪfalse
           mf.set_changed(false);
    }
    //ʵ�ְѵ�ǰ�ı��������д�뵽Ŀ��·���µķ���
    public Boolean writeBack(File file)
    {
        try {
                file.createNewFile();
                FileWriter writer = new FileWriter(file);
                BufferedWriter bufferwriter = new BufferedWriter(writer);
                mf.getMainText().selectAll();
                String [] txt = mf.getMainText().getSelectedText().split("\n");
                for(int i = 0;i<txt.length;i++)
                {
                    bufferwriter.write(txt[i]);
                    bufferwriter.newLine();
                }
                bufferwriter.close();
                writer.close(); 
                mf.set_changed(false);//��дһ��֮�󣬴�ʱ��ǰ�ı�û�б��޸�
                return true;
            } catch (IOException ex) {
            JOptionPane.showMessageDialog(mf, "�����ļ�ʧ�ܣ�");
            return false;
            }
    }
    //ʵ��ɾ���ļ�
    public void deleteFile(File file)
    {
        if(!file.delete())
        {
            JOptionPane.showMessageDialog(mf, "�˳�ʱɾ���ļ�����", "����",JOptionPane.ERROR_MESSAGE);
        }
    }
    //ʵ���½��ļ�
    public void createFile()
    {
        if(mf.get_changed())
        {
            int result = JOptionPane.showConfirmDialog(mf, "�ļ��Ѿ��޸ģ��Ƿ񱣴浱ǰ�ļ���",
                    "ѯ��",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(result==0)
            {
                 //���ñ��淽��
                saveFile();
                mf.setFile();//�½��ĵ�
                mf.getMainText().setText("");
                mf.set_changed(false);
            }
            else if(result==1)
            {
                mf.setFile();
                mf.getMainText().setText("");
                mf.set_changed(false);
            }
        }
        else
        {
            mf.setFile();
            mf.getMainText().setText("");
            mf.set_changed(false);
        }
    }
    //ʵ�ִ��ļ�
    public void openFile()
    {
        if(mf.get_changed())
        {
            int result = JOptionPane.showConfirmDialog(mf, "�ļ��Ѿ��޸ģ��Ƿ񱣴浱ǰ�ļ���",
                    "ѯ��",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(result==0)
            {
                //���ѡ�񱣴棬���ȱ��浱ǰ�ļ����ٰ�Ҫ�򿪵��ļ������ֶ��뵽��ǰ�ı�����
                saveFile();
                mf.getMainText().setText("");
                readTo();
            }
            else if(result==1)
            {
                mf.getMainText().setText("");
                readTo();
            }
        }
        else
        {
            //���ǰ�ļ�û���޸ģ���ֱ�Ӵ�Ŀ���ļ�
            mf.getMainText().setText("");
            readTo();
        }
    }
    //ʵ�ֱ����ļ�
    public void saveFile()
    {
        writeBack(mf.get_tempfile());
        int length = mf.getMainText().getText().length();
        mf.getMainText().setCaretPosition(length);
    }
    //ʵ���ļ����Ϊ
    public void saveasFile()
    {
        JFileChooser chooser = new JFileChooser("C:/Documents and Settings/Administrator/����/�¼��±��������ļ���");
        chooser.setSelectedFile(new File("*.txt"));//����Ĭ��ѡ���ļ����
        FileNameExtensionFilter filter = new FileNameExtensionFilter("txt & TXT�ı��ļ�", "txt", "TXT");//���ÿ�ѡ�ļ���׺��
        chooser.setAcceptAllFileFilterUsed(false);//ȡ�������ļ�ѡ��
        chooser.setFileFilter(filter);
        while( chooser.showSaveDialog(mf)==0)
        {
            File file = chooser.getSelectedFile();
            //��ú�׺���.��ʼ������(lastIndexOf(".")����.���ַ������һ�γ��ֵ����ֵ����subString(int beginIndex))
            String str = file.getName().substring(file.getName().lastIndexOf("."));
            if(str.equals(".txt")||str.equals(".TXT"))
            {
                if(file.exists())
                {
                     int result = JOptionPane.showConfirmDialog(mf, "�ļ��Ѵ��ڣ��Ƿ񸲸��ļ���","ѯ��",JOptionPane.YES_NO_OPTION,JOptionPane.QUESTION_MESSAGE);
                     if(result==0)
                     {
                         //��ɾ���Ŀ¼�µ�ԭ�ļ������½�һ��ͬ����ļ������д������
                         file.delete();
                         if(writeBack(file))
                         {
                             mf.getMainText().setText("");//������
                             break; //�رմ���
                         }
                     }
                }
                else
                {
                    if(writeBack(file))
                    {
                        mf.getMainText().setText("");//������
                        break;
                    }
                }
            }
            else
            {
                JOptionPane.showMessageDialog(mf, "��������ļ����ʽ���ԣ�Ϊ����������ļ������������룡");
            }
        }
        //����֮���޸ı���
        mf.setTitle("�¼��±�");
        //�޸�changedΪfalse
        mf.set_changed(false);
    }
    //ʵ��ҳ�����÷���
    public void pageSetup()
    {
        //���Ҹ�Windows�µļ��±��д˹���һ����Ի�����Ҫ��һ���Ľ�
        JOptionPane.showConfirmDialog(null,"�������ִ�����ӡ����ص���������ҳ�����û��ӡһ��"
                + "�ĵ���֮ǰ��������Ѿ���װ��ӡ���������ڰ�װ��ӡ����", "ѯ��", 
                JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
    //ʵ�ִ�ӡ����
    public void print()
    {
        //�鿴��û�а�װ��ӡ��
        JOptionPane.showConfirmDialog(mf,"�������ִ�����ӡ����ص���������ҳ�����û��ӡһ��"
                   + "�ĵ���֮ǰ��������Ѿ���װ��ӡ���������ڰ�װ��ӡ����", "ѯ��", 
                   JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE);
    }
    //ʵ���˳�����
    public void exit()
    {
        if(mf.get_changed() == true)
        {
            int result = JOptionPane.showConfirmDialog(null, "�ļ��Ѿ��޸ģ��Ƿ����˳�֮ǰ�����ļ���",
                    "ѯ��",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE);
            if(result == JOptionPane.OK_OPTION)
            {
                saveFile();
                mf.dispose();
                System.exit(0);
            }
            else if(result==JOptionPane.CANCEL_OPTION)
            {
               //�����棬�����д����ļ�
               mf.dispose();
               System.exit(0);
            }
        }
        else
        {
            int result = JOptionPane.showConfirmDialog(mf, "��ȷ��Ҫ�˳���", "ѯ��", JOptionPane.OK_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null);
            if(result==JOptionPane.OK_OPTION)
            {
                mf.dispose();
                System.exit(0);
            }  
        }
    }
}
最近下载更多
xxxjjn  LV1 2024年6月16日
queueandstack  LV3 2022年12月2日
沙发床上的  LV1 2022年5月30日
183077946781  LV1 2021年11月15日
尹恒yingying  LV18 2021年10月12日
任天晴  LV1 2021年7月10日
ncy123456789  LV6 2021年7月6日
毫无qiushengyu  LV2 2021年7月2日
祁康伟  LV1 2021年7月1日
周星星哥哥在外面  LV1 2021年6月15日
最近浏览更多
曾多次  LV1 2024年12月8日
krispeng  LV14 2024年9月15日
xxxjjn  LV1 2024年6月16日
mikrokosmos_2024  LV1 2024年6月15日
梁小贤  LV1 2024年4月27日
ruo12138  LV1 2024年4月11日
潘潘123456  LV2 2024年3月2日
lsglsg9  LV23 2023年12月29日
lshlsh 2023年12月25日
暂无贡献等级
sunshine9920  LV12 2023年10月20日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友