欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費電子書(shū)等14項超值服

開(kāi)通VIP
java幾個(gè)實(shí)用的文件操作方法

幾個(gè)實(shí)用的文件操作方法

一,比較兩個(gè)文件內容(按字節)

public static boolean compareFile(File file1, File file2)
public static boolean compareFile(String file1, String file2)

按字節一個(gè)一個(gè)比較兩個(gè)文件(可以比較二進(jìn)制文件,文本文件等任何8-bit文件)


二,復制文件(按字節)

public static void copyFile(String src, String dest)
public static void copyFile(File src, File dest)

按字節復制文件(里面也寫(xiě)了個(gè)按字符復制,寫(xiě)了后覺(jué)得沒(méi)什么用,僅限于復制文本文件,而且還要轉一次Unicode,效率也低)


三,移動(dòng)文件

public static File moveFile(String scr, String dir)
public static File moveFile(File scr, File dir)

發(fā)現java.io包里居然沒(méi)移動(dòng)文件方法(里面的 File.renameTo() 提供了移動(dòng)和重命名的方法,用起來(lái)不方便,本方法只是在File.renameTo()上做點(diǎn)修改),直接給出目標目錄就可以


四,重命名文件

public static File renameFile(String file, String name)
public static File renameFile(File file, String name)

使用java.io.File.renameTo()方法得兩個(gè)文件都指定同一文件夾,否則變成移動(dòng)且重命名了,覺(jué)得不方便,這個(gè)方法只要給出新文件名即可,本方法也是在File.renameTo()上做點(diǎn)修改)


五,文件與String互轉

public static boolean stringToFile(String src, String file)
public static String fileToString(String file)

這兩個(gè)方法就是String與文件的相互轉換,可以實(shí)現隨機讀寫(xiě),而 IOstream 流只能順序讀寫(xiě),雖然有 java.io.RandomAccessFile 類(lèi),但那是面向字節的,不是面向字符,用來(lái)處理文本不方便(不同系統或文件字符編碼不同,也就是單個(gè)字符長(cháng)度定義不同),而轉成String就是統一轉成二字節的Unicode,進(jìn)而實(shí)現隨意定位讀取。
但對于大文件全部轉成 String JVM就可能支持不了了。


六,打印文件相關(guān)信息

public static void fileInfo(String name)
 
package hartech;

import java.io.*;

/**
  * <p>Description: JTL‘s File ToolKit</p>
  *
  * <p>Copyright: Copyright (c) 2006 hartech.cn</p>
  *
  * <p>Website: www.hartech.cn</p>
  *
  * <p>Page: http://www.hartech.cn/blog/blogview.asp?logID=77</p>
  *
  * @author JTL.zheng@gmail.com
  * @version 1.0
  */
public class JFile {

    /**
      * print the informations of the file input
      * @param f File
      */
    public static void fileInfo(String name) {
        fileInfo(new File(name));
    }

    public static void fileInfo(File f) {
        J.p("--------- Attributes of the files ----------");
        if (f == null) {
            J.pw("a null reference!!");
        }
        else if (!f.exists()) {
            J.pw(f.toString() + " file Not Found!");
        }
        else if (f.isFile()) {
            StringBuffer length = new StringBuffer(String.valueOf(f.length()));
            int i = length.length() - 3;
            while (i > 0) {
                length.insert(i, ",");
                i -= 3;
            }
            J.p("\"" + f.toString() + "\" is a File.");
            J.p("Name: \t\t" + f.getName());
            J.p("Readable: \t" + f.canRead());
            J.p("Writable: \t" + f.canWrite());
            J.p("AbsolutePath:\t" + f.getAbsolutePath());
            J.p("Parent:\t\t" + f.getAbsoluteFile().getParent());
            J.p("Length:\t\t" + length + " bytes");
        }
        else {
            J.p("\"" + f.toString() + "\" is a Directory");
            J.p("Name: \t\t" + f.getName());
            J.p("Readable: \t" + f.canRead());
            J.p("Writable: \t" + f.canWrite());
            J.p("AbsolutePath:\t" + f.getAbsolutePath());
            J.p("Parent:\t\t" + f.getAbsoluteFile().getParent());
            J.p("Subfiles:\t" + f.list().length);
        }
        J.p("-------------- fileInfo END ---------------");
    }

    /**
      * compare file byte by byte<br>
      * can compare any file(binary file,text file...)
      * @param file1 File
      * @param file2 File
      * @return boolean
      */
    public static boolean compareFile(String file1, String file2) {
        return compareFile(new File(file1), new File(file2));
    }

    public static boolean compareFile(File file1, File file2) {
        BufferedInputStream in1 = null, in2 = null;
        try {
            in1 = new BufferedInputStream(new FileInputStream(
                    file1));
            in2 = new BufferedInputStream(new FileInputStream(
                    file2));
            int i;
            while ( (i = in1.read()) != -1) {
                if (i != in2.read()) {
                    return false;
                }
            }
            if (in2.read() != -1) {
                return false;
            }
            return true;
        }
        catch (FileNotFoundException ex) {
            J.pw("File not found!");
        }
        catch (IOException ex) {
            J.pw("IOException!");
        }
        finally {
            try {
                in1.close();
                in2.close();
            }
            catch (IOException ex1) {
                J.pw("IOException when closing!");
            }
        }
        return false;
    }

    /**
      * the java.io.File.renameTo(File,String newname)
      * actually move the file to newname‘s path and rename it which is inconvenient.<br>
      * this method just rename the file and keep where it is,ignore the move<br>
      * and return the new File‘s reference<p>
      * eg. file = renameFile(file,"NewName.xxx");
      * @param file File
      * @param name String the newName
      * @return File the new File‘s reference
      */
    public static File renameFile(String file, String name) {
        return renameFile(new File(file), name);
    }

    public static File renameFile(File file, String name) {
        File newname;
        if (file == null || !file.exists()) {
            J.pw("File not found!");
            return null;
        }
        if (file.getParent() == null) {
            newname = new File(name);
            file.renameTo(newname);
        }
        else {
            newname = new File(file.getParentFile(), name);
            file.renameTo(newname);
        }
        J.p("rename is done: " + file + " -> " + newname);
        return newname;
    }

    /**
      * use java.io.File.renameTo(File,String newname) to move file<p>
      * parameters must be a file and a directory<br>
      * return a reference point to the new file
      * @param scr String
      * @param dir String
      * @return File a reference point to the new file
      */
    public static File moveFile(String scr, String dir) {
        return moveFile(new File(scr), new File(dir));
    }

    public static File moveFile(File scr, File dir) {
        if (scr == null || dir == null) {
            J.pw("a null reference!");
            return null;
        }
        if (!scr.exists() || !dir.exists() || scr.isDirectory() || dir.isFile()) {
            J.pw("not file or directory or not exist!");
            return null;
        }
        File f = new File(dir, scr.getName());
        if (f.exists()) {
            J.pw("target file has existed!");
        }
        scr.renameTo(f);
        J.p("move file done: " + scr + " -> " + f);
        return f;
    }

    /**
      * turn file to String<p>
      * maybe you can use it to access file randomly through the string<br>
      * but it maybe fault when trun a big file to string
      * @param file String
      * @return String
      */
    public static String fileToString(String file) {
        String lineStr = "", string = "";
        int i;
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            while ( (i = in.read()) != -1) {
                string += (char) i;
            }
            string = string.trim();
            return string;
        }
        catch (FileNotFoundException ex) {
            J.pw("File Not Found!");
        }
        catch (IOException ex) {
            J.pw("IO exception!");
        }
        finally {
            try {
                in.close();
            }
            catch (IOException ex1) {
                J.pw("IOException when closing!");
            }
        }
        return null;
    }

    /**
      * write the string to file<br>
      * if fail return false else return true
      * @param src String
      * @param file String
      * @return boolean
      */
    public static boolean stringToFile(String src, String file) {
        BufferedWriter out = null;
        try {
            out = new BufferedWriter(new FileWriter(file));
            out.write(src);
            return true;
        }
        catch (Exception ex) {
            J.pw("IO exception!");
        }
        finally {
            try {
                out.close();
            }
            catch (IOException ex) {
                J.pw("IOException when closing!");
            }
        }
        return false;
    }

    /**
      * only used to copy character files<br>
      * local char -> int -> unicode -> int -> local char
      * @param src String
      * @param dest String
      */
    static public void copyFileByChar(String src, String dest) {
        String lineStr;
        BufferedReader in = null;
        BufferedWriter out = null;
        try {
            in = new BufferedReader(new FileReader(src));
            out = new BufferedWriter(new FileWriter(dest));
            while ( (lineStr = in.readLine()) != null) {
                out.write(lineStr);
                out.newLine();
            }
            J.p("copy is done !");
        }
        catch (FileNotFoundException ex) {
            J.pw("File Not Found!");
        }
        catch (IOException ex) {
            J.pw("IO exception!");
        }
        finally {
            try {
                in.close();
                out.close();
            }
            catch (IOException ex1) {
                J.pw("IOException when closing!");
            }
        }

    }

    /**
      * copy file by byte<br>
      * can copy any file,because any file is made of bytes<br>
      * bytes -> int -> bytes
      * @param src String
      * @param dest String
      */
    public static void copyFile(String src, String dest) {
        copyFile(new File(src), new File(dest));
    }

    public static void copyFile(File src, File dest) {
        int b;
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(src));
            out = new BufferedOutputStream(new FileOutputStream(
                    dest));
            while ( (b = in.read()) != -1) {
                out.write(b);
            }
            J.p("CopyFile is done: " + src + " -> " + dest);
        }
        catch (FileNotFoundException ex) {
            J.pw("File Not Found!");
        }
        catch (IOException ex) {
            J.pw("IO exception!");
        }
        finally {
            try {
                in.close();
                out.close();
            }
            catch (IOException ex1) {
                J.pw("IOException when closing!");
            }
        }
    }
}

本站僅提供存儲服務(wù),所有內容均由用戶(hù)發(fā)布,如發(fā)現有害或侵權內容,請點(diǎn)擊舉報。
打開(kāi)APP,閱讀全文并永久保存 查看更多類(lèi)似文章
猜你喜歡
類(lèi)似文章
jar文件替換
回憶Java 之 文件讀寫(xiě)及性能比較總結
java如何打開(kāi)word文檔_如何在Java中打開(kāi)和操作Word文檔/模板?
Java io Android文件操作工具 FileUtils.java
關(guān)于java中創(chuàng )建文件,并且寫(xiě)入內容
Java創(chuàng )建TXT文件并進(jìn)行讀、寫(xiě)、修改操作
更多類(lèi)似文章 >>
生活服務(wù)
分享 收藏 導長(cháng)圖 關(guān)注 下載文章
綁定賬號成功
后續可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服

欧美性猛交XXXX免费看蜜桃,成人网18免费韩国,亚洲国产成人精品区综合,欧美日韩一区二区三区高清不卡,亚洲综合一区二区精品久久