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;
}
}
【java工具类】FileUtil
最新推荐文章于 2024-10-03 08:23:52 发布