package cra.base.util;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
/**
*
* @author
*
*/
public class FileUtil {
private int fileIndex = 1;
private int index = 1;
private File targetFile;
private String filePath;
public static void main(String[] args) {
FileUtil fileUtil = new FileUtil();
String filePath = "d:/user/80002253/桌面/gradle";
fileUtil.setFilePath(filePath);
fileUtil.getBytesToFile2(filePath);
}
/**
* @param filePath
* 文件夹路径
*/
public void getBytesToFile2(String filePath) {
File file = new File(filePath);
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file2 : files) {
file = file2;
getBytesToFile2(file2.getAbsolutePath());
}
} else {
if (index % 5 == 0) {
String absolutePath = targetFile.getAbsolutePath();
targetFile = new File(absolutePath.substring(0,
absolutePath.lastIndexOf("."))
+ fileIndex++
+ absolutePath.substring(absolutePath.lastIndexOf(".")));
getBytesToFile(file, targetFile);
} else {
getBytesToFile(file, targetFile);
}
index++;
}
}
/**
* 生成文件
*
* @param filePath
* 资源文件路径
*/
public void getFile(String filePath) {
File file = new File(filePath);
File[] files = file.listFiles();
for (File f : files) {
transfer(f);
}
}
/**
* 转换文件成正常形态
*
* @param file
*/
private void transfer(File file) {
BufferedReader br = null;
FileOutputStream fos = null;
try {
br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(">>");
fos = new FileOutputStream(split[0]);
String[] chars = split[1].split(",");
for (String s : chars) {
fos.write(Integer.parseInt(s));
fos.flush();
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (br != null) {
br.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
*
* @param file
* @param filePath
*/
private byte[] getBytesToFile(File file, File targetFile) {
InputStream is = null;
BufferedWriter bufferedWriter = null;
byte[] buffer = null;
try {
is = new FileInputStream(file);
byte[] b = new byte[1000];
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bufferedWriter = new BufferedWriter(
new FileWriter(targetFile, true));
int n;
while ((n = is.read(b)) != -1) {
bos.write(b, 0, n);
}
bos.close();
buffer = bos.toByteArray();
bufferedWriter.write(file.getAbsolutePath() + ">>");
for (int i = 0; i < buffer.length; i++) {
int c = buffer[i];
if (i == buffer.length - 1) {
bufferedWriter.write(c + "");
break;
}
bufferedWriter.write(c + ",");
}
bufferedWriter.newLine();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (bufferedWriter != null) {
bufferedWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return buffer;
}
public String getFilePath() {
return filePath;
}
public void setFilePath(String filePath) {
this.filePath = filePath;
targetFile = new File(this.filePath + "3" + "/file.txt");
}
}
将文件转换为char类型的文件
最新推荐文章于 2023-02-16 22:58:23 发布