⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 applicationfileoperator.java

📁 这是一个用java写的用于操作文件的代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
        javax.swing.JFileChooser fjc;
        fjc = new javax.swing.JFileChooser(filepath);
        fjc.setDialogTitle("选择保存目录");
        fjc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        if (fjc.APPROVE_OPTION != fjc.showOpenDialog(this)) {
            return;
        }
        filepath = fjc.getSelectedFile().getPath();
        String selfilename = file.getName();
        /* String str = "chenchen.doc.000.pki";
                 String index = str.substring(0,str.indexOf(".")+1);
                 String str1 = str.substring(index.length());
                 String str2 = str1.substring(0,str1.indexOf("."));
                 String str3 = index+str2;
                 System.out.println("str3:"+str3);
         */
        // System.out.println(selfilename);
        String selfilename1 = selfilename.substring(0,
                selfilename.indexOf(".") + 1);
        //System.out.println(selfilename1);

        String selfilename2 = selfilename.substring(selfilename1.length());
        //System.out.println(selfilename2);

        String selfilename3 = selfilename2.substring(0,
                selfilename2.indexOf("."));

        //System.out.println(selfilename3);
        String selfilename4 = selfilename1 + selfilename3;
        //  System.out.println("selfilename4:"+selfilename4);
        String savefile = fjc.getSelectedFile().getAbsolutePath() + "/" +
                          selfilename4;
        //  selfilename.substring(0, selfilename.lastIndexOf("."));
        if (new File(savefile).exists()) {
            int javaok = JOptionPane.showConfirmDialog(this,
                    "文件名 " + new File(savefile).getName() +
                    " 的文件已经存在,你要覆盖原来的文件吗?", "警告!", JOptionPane.OK_CANCEL_OPTION);
            if (javaok == JOptionPane.CANCEL_OPTION) {
                return;
            }
        }
        byte[] b = new byte[1024];
        int read = 0;
        FileOutputStream fos = new FileOutputStream(savefile);
        FileInputStream fis;
        File[] f = file.getParentFile().listFiles();
        for (int i = 0; i < f.length; i++) {
            if (!f[i].getName().startsWith(selfilename4)) {
                continue;
            }
            fis = new FileInputStream(f[i]);
            while ((read = fis.read(b, 0, b.length)) > 0) {
                fos.write(b, 0, read);
                fos.flush();
            }
            fis.close();
        }
        fos.close();
        fos = null;
        fis = null;
        JOptionPane.showMessageDialog(this, "合并完成");
    }

    private void SplitTotalCount_changedUpdate(DocumentEvent e) throws
            Exception {
        File file = new File(this.SplitSource.getText());
        if (!file.exists() || this.SplitTotalCount.getText().equals("")) {
            return;
        }
        long l = Long.parseLong(this.SplitTotalCount.getText());
        if (l <= 0) {
            return;
        }
        this.SplitEachSize.setText(String.valueOf(file.length() / l));
    }

    private void SplitEachSize_changedUpdate(DocumentEvent e) throws Exception {
        File file = new File(this.SplitSource.getText());
        if (!file.exists() || this.SplitEachSize.getText().equals("")) {
            return;
        }
        double l = Double.parseDouble(this.SplitEachSize.getText());
        if (l <= 0) {
            return;
        }
        this.SplitTotalCount.setText(fileCount((double) file.length(), l));
    }

    private void SplitJButtonClick(ActionEvent e) { //选择要拆分的源文件
        javax.swing.JFileChooser fjc;
        fjc = new javax.swing.JFileChooser(filepath);
        fjc.addChoosableFileFilter(new myFilter("*.jpg,*.gif",
                                                "jpg,gif Files(*.jpg,*.gif)")); //文件过滤
        fjc.addChoosableFileFilter(new myFilter("*.rm,*.rmvb",
                                                "real Files(*.rm,*.rmvb)")); //文件过滤
        fjc.addChoosableFileFilter(new myFilter("*.mpg,*.mpeg",
                                                "mpg Files(*.mpg,*.mpeg)")); //文件过滤
        fjc.addChoosableFileFilter(fjc.getAcceptAllFileFilter());
        if (fjc.APPROVE_OPTION != fjc.showOpenDialog(this)) {
            return;
        }
        filepath = fjc.getSelectedFile().getPath();
        File file = fjc.getSelectedFile();
        double filesize = file.length();
        String size = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        if (filesize >= 1024d * 1024d * 0.8d) {
            size = df.format(filesize / (1024d * 1024d)) + "MB";
        } else if (filesize >= 1024d * 0.8d) {
            size = df.format(filesize / 1024d) + "KB";
        } else {
            size = filesize + "Bytes";
        }
        this.splitSourceSize.setText("源文件大小: " + size);
        this.SplitSource.setText(file.getAbsolutePath());
        this.SplitTotalCount.setText(fileCount(filesize, 102400));
        this.SplitEachSize.setText("102400");
    }

    private void coalitionJButtonClick(ActionEvent e) { //选择要合并的首文件
        javax.swing.JFileChooser fjc;
        fjc = new javax.swing.JFileChooser(filepath);
        fjc.removeChoosableFileFilter(fjc.getAcceptAllFileFilter()); //移除默认的select All
        fjc.addChoosableFileFilter(new myFilter("*.000.others",
                                                "拆分首文件(*.000.others)")); //文件过滤
        if (fjc.APPROVE_OPTION != fjc.showOpenDialog(this)) {
            return;
        }
        filepath = fjc.getSelectedFile().getPath();
        File file = fjc.getSelectedFile();
        String selfilename = file.getName();
        this.coalitionSource.setText(file.getAbsolutePath());
        selfilename = selfilename.substring(0, selfilename.lastIndexOf("."));
        File[] f = file.getParentFile().listFiles();
        double filesize = 0;
        int filetotalcount = 0;
        for (int i = 0; i < f.length; i++) {
            if (!f[i].getName().startsWith(selfilename)) {
                continue;
            }
            filetotalcount++;
            filesize += f[i].length();
        }
        String size = "";
        java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");
        if (filesize >= 1024d * 1024d * 0.8d) {
            size = df.format(filesize / (1024d * 1024d)) + "MB";
        } else if (filesize >= 1024d * 0.8d) {
            size = df.format(filesize / 1024d) + "KB";
        } else {
            size = filesize + "Bytes";
        }
        this.coalitionTotalSize.setText("文件总大小: " + size);
        this.coalitionTotalCount.setText("文件总数目: " + filetotalcount);
    }

    private String fileCount(double filesize, double eachsize) {
        int i = (int) (filesize / eachsize) + (filesize % eachsize > 0 ? 1 : 0);
        return String.valueOf(i);
    }

    public class NumberDocument extends PlainDocument { //对文本框的输入进行输入限制,构造PlainDocument实现
        public void insertString(int offs, String str, AttributeSet a) throws
                BadLocationException {
            char[] source = str.toCharArray();
            char[] result = new char[source.length];
            int j = 0;
            for (int i = 0; i < result.length; i++) {
                if (Character.isDigit(source[i])) {
                    result[j++] = source[i];
                }
            }
            super.insertString(offs, new String(result, 0, j), a);
        }
    }


    public static void main(String args[]) throws Exception {
        Font font = new Font("宋体", Font.PLAIN, 15);
        UIManager.put("Button.font", font);
        UIManager.put("ToggleButton.font", font);
        UIManager.put("RadioButton.font", font);
        UIManager.put("CheckBox.font", font);
        UIManager.put("ColorChooser.font", font);
        UIManager.put("ToggleButton.font", font);
        UIManager.put("ComboBox.font", font);
        UIManager.put("ComboBoxItem.font", font);
        UIManager.put("InternalFrame.titleFont", font);
        UIManager.put("Label.font", font);
        UIManager.put("List.font", font);
        UIManager.put("MenuBar.font", font);
        UIManager.put("Menu.font", font);
        UIManager.put("MenuItem.font", font);
        UIManager.put("RadioButtonMenuItem.font", font);
        UIManager.put("CheckBoxMenuItem.font", font);
        UIManager.put("PopupMenu.font", font);
        UIManager.put("OptionPane.font", font);
        UIManager.put("Panel.font", font);
        UIManager.put("ProgressBar.font", font);
        UIManager.put("ScrollPane.font", font);
        UIManager.put("Viewport", font);
        UIManager.put("TabbedPane.font", font);
        UIManager.put("TableHeader.font", font);
        UIManager.put("TextField.font", font);
        UIManager.put("PasswordFiled.font", font);
        UIManager.put("TextArea.font", font);
        UIManager.put("TextPane.font", font);
        UIManager.put("EditorPane.font", font);
        UIManager.put("TitledBorder.font", font);
        UIManager.put("ToolBar.font", font);
        UIManager.put("ToolTip.font", font);
        UIManager.put("Tree.font", font); //以上设置是为了解决中文问题
        // UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); //设置可视风格
        UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
        //new ApplicationFileOperator(null,"",true);
    }
}


//************************************************文件选择过滤器
class myFilter extends javax.swing.filechooser.FileFilter {
    String extension = "", description = "";
    public myFilter(String extension, String description) {
        if (extension != null) {
            this.extension = extension;
        }
        if (description != null) {
            this.description = description;
        }
    }

    public String getDescription() {
        return this.description;
    }

    public boolean accept(File file) {
        if (file.isDirectory() || extension.equals("")) {
            return true;
        }
        String[] s = extension.replaceAll("[*]", "").split("[,]");
        for (int i = 0; i < s.length; i++) {
            if (file.getName().toLowerCase().endsWith(s[i].toLowerCase())) {
                return true;
            }
        }
        return false;
    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -