【java工具類】FileUtil

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

 

public class FileUtils {
    public static String getFilePrefix(String fileName){
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(0, splitIndex);
    }
   
    public static String getFileSufix(String fileName){
        int splitIndex = fileName.lastIndexOf(".");
        return fileName.substring(splitIndex + 1);
    }
   
    public static void copyFile(String inputFile,String outputFile) throws FileNotFoundException{
        File sFile = new File(inputFile);
        File tFile = new File(outputFile);
        FileInputStream fis = new FileInputStream(sFile);
        FileOutputStream fos = new FileOutputStream(tFile);
        int temp = 0; 
        try { 
            while ((temp = fis.read()) != -1) { 
                fos.write(temp); 
            }
        } catch (IOException e) { 
            e.printStackTrace(); 
        } finally{
            try {
                fis.close();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     *
     * @param fileName
     * @return
     */
    public static byte[] readFileByBytes(String fileName) {
        File file = new File(fileName);
        InputStream in = null;
        byte[] res = new byte[200];
        int lenght = (int)file.length();
        try {
            in = new FileInputStream(file);
            int tempbyte;
            int i = 0;
            while ((tempbyte = in.read()) != -1) {
                res[i] = (byte) tempbyte;
                i++;
            }
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
        byte[] ret = new byte[lenght];
        for(int i = 0;i < lenght;i++){
            ret[i] = res[i];
        }
        return ret;
    }
}


發佈了70 篇原創文章 · 獲贊 2 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章