获取文件夹下的文件名
public static List<String> getFiles(String path) {
List<String> files = new ArrayList<String>();
File file = new File(path);
File[] tempList = file.listFiles();
for (int i = 0; i < tempList.length; i++) {
if (tempList[i].isFile()) {
String fileName = tempList[i].getName();
files.add(fileName);
}
if (tempList[i].isDirectory()) {
}
}
return files;
}
获取文件的长度
@Override
public long getFileLength(File file) {
FileReader fr = null;
BufferedReader br = null;
long fileSize = 0;
try {
fr = new FileReader(file);
br = new BufferedReader(fr);
String line = br.readLine();
while(line != null){
fileSize += line.length();
line = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(br != null){
br.close();
}
if(fr != null){
fr.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return fileSize;
}
获取可以分割的文件数量
@Override
public int getSplitFileNum(long fileByte,String fileParh, long size){
fileByte = getFileLength(new File(fileParh));
if(size < fileByte){
if(fileByte % size == 0){
return (int) (fileByte/size);
}else{
return (int) (fileByte/size) + 1;
}
}
return 1;
}
分割文件
@Override
public String[] splitFile(File srcFile, int splitFileNum, long size) throws IOException {
splitFileNum = getSplitFileNum(getFileLength(srcFile), srcFile.toString(), size);
if (splitFileNum <= 0) {
return null;
}
FileReader fr = null;
BufferedReader br = null;
long readNum = 0;
String[] splits = new String[splitFileNum];
try {
fr = new FileReader(srcFile);
br = new BufferedReader(fr);
int i = 0;
while (splitFileNum > i) {
String name = null;
String nameLast = null;
if (srcFile.getName().indexOf(".") != -1) {
name = srcFile.getName().substring(0, srcFile.getName().indexOf("."));
int last = srcFile.getName().lastIndexOf(".");
nameLast = srcFile.getName().substring(last);
} else {
name = srcFile.getName();
}
splits[i] = srcFile.getParent() + "/" + name + "_" + i + nameLast;
File wfile = new File(splits[i]);
if (!wfile.exists()) {
wfile.getParentFile().mkdirs();
wfile.createNewFile();
}
FileWriter fw = new FileWriter(wfile, false);
BufferedWriter bw = new BufferedWriter(fw);
String line = br.readLine();
int flush = 0;
while (line != null) {
if (line.trim().length() == 0) {
line = br.readLine();
continue;
}
readNum += line.length();
if (i + 1 == splitFileNum) {
bw.write(line);
bw.newLine();
} else {
if (readNum >= size) {
bw.write(line);
bw.newLine();
break;
} else {
bw.write(line);
bw.newLine();
}
}
line = br.readLine();
if (flush % 100 == 0) {
bw.flush();
}
}
bw.flush();
fw.flush();
bw.close();
fw.close();
readNum = 0;
i++;
}
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
try {
if (br != null) br.close();
if (fr != null) fr.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
br = null;
fr = null;
}
}
return splits;
}
提取文件名
public static String getFileNameNoEx(String filename) {
if ((filename != null) && (filename.length() > 0)) {
int dot = filename.lastIndexOf('.');
if ((dot >-1) && (dot < (filename.length()))) {
return filename.substring(0, dot);
}
}
return filename;
}