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

📄 filetools.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
                    pathList.add(path1 + s);
                }
            }

            for (int i = 0; i < pathList.size(); i++) {
                String s = (String) pathList.get(i);
                s = setFinalDirectorySlash(s);
                String[] fList = fileList(s, fileFilter1);
                for (int j = 0; j < fList.length; j++) {
                    String file = fList[j];
                    returnValue.add(s + file);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        return returnValue;
    }*/


    /**
     * Delete a set of files. E.g "c:\temp\*.bak"
     */
    public static void deleteFiles(String path1, String fileFilter1) {
        if ((path1 == null) || (path1.trim().length() == 0)) {
            //log.debug("path is NULL. Nothing will be deleted");
            return;
        }

        if (fileFilter1 == null) {
            //log.debug("fileFilter is NULL. Nothing will be deleted");
            return;
        }


        String[] fileList = fileList(new FileParserSettings(path1, fileFilter1));
        //log.debug(fileList.length + " files found to delete");
        for (int i = 0; i < fileList.length; i++) {
            //log.debug("delete: \"" + fileList[i] + "\"");
            deleteFile(setFinalDirectorySlash(path1) + fileList[i]);
        }
    } // end method

    /**
     * true if fileName is a directory
     */
    public static boolean isDirectory(String fileName1) {
        boolean ret = false;

        try {
            File orgFile = new File(fileName1);

            ret = orgFile.isDirectory();
        } catch (Exception e) {
            //log.error(e);
            e.printStackTrace();
        }

        return ret;
    } // end method


    public static void copyFile(String source1, String dest1)
            throws java.io.FileNotFoundException,
            java.io.IOException {
        BufferedInputStream fis = new BufferedInputStream(new FileInputStream(source1));
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(dest1));

        byte buf[] = new byte[BUFFER_SIZE];
        int availableBytes;

        while ((availableBytes = fis.read(buf)) > -1) {
            fos.write(buf, 0, availableBytes);
        }

        fis.close();
        fos.flush();
        fos.close();
    }

    /**
     * copies the file from an inputstream to a file
     *
     * @param source1
     * @param dest1
     * @throws java.io.FileNotFoundException
     * @throws java.io.IOException
     */
    public static void copyStream(InputStream source1, String dest1)
            throws java.io.FileNotFoundException,
            java.io.IOException {
        BufferedInputStream fis = new BufferedInputStream(source1);
        BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(dest1));

        byte buf[] = new byte[BUFFER_SIZE];
        int availableBytes;

        while ((availableBytes = fis.read(buf)) > -1) {
            fos.write(buf, 0, availableBytes);
        }

        fis.close();
        fos.flush();
        fos.close();
    }

    /**
     * copies the file from an inputstream to a String
     */
    public static String copyStreamToString(InputStream a_stream, String defaultValue) {
        String returnValue = defaultValue;

        try {
            InputStreamReader in = new InputStreamReader(a_stream, "UTF-8");

            char buf[] = new char[BUFFER_SIZE];
            int availableBytes;

            StringBuffer strBuf = new StringBuffer();
            while ((availableBytes = in.read(buf)) > -1) {
                in.read(buf, 0, availableBytes);

                //String str = new String(buf, 0, availableBytes);
                strBuf.append(buf);
            }
            returnValue = strBuf.toString();
        } catch (Exception e) {
            //log.error(e);
            e.printStackTrace();
        }
        return returnValue;
    }


    // copies a file from the jarfile to a tempdirectory and returns the path
    public static String extractFromJarFile(String jarFile1, String filename1)
            throws java.io.IOException {
        String filename = filename1;
        JarFile file = new JarFile(jarFile1);
        ZipEntry theEntry = new ZipEntry(filename);
        InputStream in = file.getInputStream(theEntry);
        if (in == null) {
            if (!noDebug)
            //log.debug("no entry: " + filename);
                filename = filename.replace('/', '\\');
            if (!noDebug)
            //log.debug("try with: " + filename);
                theEntry = new ZipEntry(filename);
            in = file.getInputStream(theEntry);
        }

        String tempDir = System.getProperty("user.dir");
        filename = filename.replace('\\', '/');
        File f = new File(filename);
        String name = f.getName();
        //log.debug("Property-Name: <" + name + ">");
        String destination = setFinalDirectorySlash(tempDir) + name;

        if (!noDebug) {
            //log.debug("Jar: " + jarFile1 + ", " + FileTools.existsFile(jarFile1));
            //log.debug("name: " + filename);
            //log.debug(in.toString());
            //log.debug("" + in.available());
            //log.debug(destination);
        }

        if (in != null) {
            copyStream(in, destination);
            in.close();
            in = null;
        } else {
            if (!noDebug) {
                //log.error("unable to locate entry \"" + filename + "\" in archive \"" +
                //        jarFile1 + "\"");
            }

            File newFile = new File(destination);
            newFile.createNewFile();
        }

        return destination;
    }


    /**
     * open a Filedialog box, select a single file and return the name *
     */
    private static String showFileDialog(String directory1, String[] extension1,
                                         String[] description1, boolean addAll1,
                                         boolean isOpenDialog, String defaultValue1) {
        String returnValue = defaultValue1;

        JFileChooser chooser = new JFileChooser();

        if (!addAll1) {
            chooser.removeChoosableFileFilter(chooser.getAcceptAllFileFilter());
        }

        ExampleFileFilter filter = null;

        /*
        if (addAll1) {
           filter = new ExampleFileFilter();
           filter.addExtension("*");
           filter.setDescription("All Files");
           chooser.setFileFilter(filter);
        }
        */


        for (int i = 0; i < extension1.length; i++) {
            filter = new ExampleFileFilter();
            filter.addExtension(extension1[i]);
            filter.setDescription(description1[i]);
            chooser.setFileFilter(filter);
        }

        chooser.setCurrentDirectory(new File(directory1));

        int actionPerformed;
        if (isOpenDialog) {
            actionPerformed = chooser.showOpenDialog(null);
        } else {
            actionPerformed = chooser.showSaveDialog(null);
        }
        //log.debug("action: " + actionPerformed + ", APP: " + JFileChooser.APPROVE_OPTION);


        if (actionPerformed == JFileChooser.APPROVE_OPTION) {
            File filename = chooser.getSelectedFile();
            try {
                returnValue = filename.getCanonicalPath();
            } catch (Exception e) {
                //log.error(e);
            }

            //System.out.println("File: " + returnValue);
        }

        //log.debug("File: " + returnValue);
        return returnValue;
    }

    /**
     * open a Filedialog box, select a single file and return the name *
     */
    public static String showOpenDialog(String directory1, String[] extension1,
                                        String[] description1, boolean addAll1,
                                        String defaultValue1) {
        return showFileDialog(directory1, extension1, description1, addAll1, true, defaultValue1);
    }


    /**
     * open a Filedialog box, select a single file and return the name *
     */
    public static String showOpenDialog(String directory1, String extension1,
                                        String description1, boolean addAll1,
                                        String defaultValue1) {
        String[] extension = new String[1];
        String[] description = new String[1];

        extension[0] = extension1;
        description[0] = description1;

        return showOpenDialog(directory1, extension, description, addAll1, defaultValue1);
    }

    /**
     * open a Save file Filedialog box, select a single file and return the name *
     */
    public static String showSaveDialog(String directory1, String[] extension1,
                                        String[] description1, boolean addAll1,
                                        String defaultValue1) {
        return showFileDialog(directory1, extension1, description1, addAll1, false, defaultValue1);
    }

    /**
     * open a Filedialog box, select a single file and return the name *
     */
    public static String showSaveDialog(String directory1, String extension1,
                                        String description1, boolean addAll1,
                                        String defaultValue1) {
        String[] extension = new String[1];
        String[] description = new String[1];

        extension[0] = extension1;
        description[0] = description1;

        return showSaveDialog(directory1, extension, description, addAll1, defaultValue1);
    }


    public static void removeOddLinesFromFile(String input1, String output1) {
        try {
            if (!existsFile(input1)) {
                return;
            }

            File file = new File(output1);

            BufferedReader fis = new BufferedReader(new FileReader(input1));
            //BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(target));
            BufferedWriter fos = new BufferedWriter(new FileWriter(output1));

            String line;
            boolean doInsert = true;
            while ((line = fis.readLine()) != null) {
                line = line.trim();

                if (doInsert) {
                    fos.write(line);
                    fos.newLine();
                }

                doInsert = !doInsert;
            }

            fis.close();
            fos.flush();
            fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    } // end method

    // throw exeptions
    public static InputStream openFile(String a_fileName) throws FileNotFoundException {
        InputStream returnValue = null;

        if (a_fileName == null) {
            throw new NullPointerException("fileName is null");
        }

        if (!existsFile(a_fileName)) {
            throw new UnsupportedOperationException("file \"" + a_fileName + "\" does'nt exist");
        }

        String fileName = a_fileName;
        if ((fileName != null) && (existsFile(a_fileName))) {
            FileInputStream fis = new FileInputStream(fileName);
            returnValue = new DataInputStream(fis);
        }


        return returnValue;
    }

    /**
     * open the given file an returns the DataInputStream. Does not throw but display exceptions
     */
    public static InputStream openFile(String a_fileName, InputStream defaultInputStream) {
        InputStream returnValue = defaultInputStream;

        try {
            returnValue = openFile(a_fileName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }


    /**
     * returns the suffix if a file. E.g. "test.txt" will return "TXT"
     *
     * @return returns the suffix of a file in upper case, e.g. "TXT", "PDF", ...
     */
    public static String getFileSuffix(String a_filename) {
        String returnValue = "";

        if (a_filename != null) {
            StringTokenizer tokens = new StringTokenizer(a_filename, ".", false);
            String lastToken = returnValue;
            while (tokens.hasMoreTokens()) {
                lastToken = tokens.nextToken();
            }
            returnValue = lastToken;
        }

        returnValue = returnValue.toUpperCase();
        return returnValue;
    }

    /**
     * returns the prefix if a file. E.g. "test.txt" will return "test"
     *
     * @return returns the prefix of a file in upper case, e.g. "filename"
     */
    public static String getFilePrefix(String a_filename) {
        String returnValue = "";

        String suffix = getFileSuffix(a_filename);
        returnValue = a_filename.substring(0, a_filename.length() - suffix.length() - 1);

        return returnValue;
    }

    /**
     * return the domain for this uri.
     * For web addresses this is the main domain. e.g. "http://www.google.at"
     *
     * @param a_uri
     * @return
     */
    public static String getDomain(String a_uri) {
        String returnValue = null;
        try {
            URI uri = new URI(a_uri);
            String host = uri.getHost();
            int port = uri.getPort();
            String scheme = uri.getScheme();
            String authority = uri.getAuthority(); // e.g. c: for files
            String userInfo = uri.getUserInfo();
            String query = null;
            String fragement = null;
            URI root = new URI(scheme, authority, "/", query, fragement);
            returnValue = root.toString();
        } catch (URISyntaxException e) {
            //log.error(e);
            e.printStackTrace();
        }

        return returnValue;
    }
}

⌨️ 快捷键说明

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