|
@ -43,7 +43,7 @@ public class FileUtil {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
public static boolean writeFile(String filePath,InputStream fis, String encoding) throws IOException {
|
|
|
public static boolean writeFile(String filePath, InputStream fis, String encoding) throws IOException {
|
|
|
File file = new File(filePath);
|
|
|
if (!file.getParentFile().exists()) {
|
|
|
if (!file.getParentFile().mkdirs()) {
|
|
@ -87,13 +87,14 @@ public class FileUtil {
|
|
|
|
|
|
/**
|
|
|
* file转string
|
|
|
*
|
|
|
* @param file 文件
|
|
|
* @return string类型流
|
|
|
*/
|
|
|
public static String convertFileToString(File file) {
|
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
if(file.isFile()&&file.exists()) {
|
|
|
if (file.isFile() && file.exists()) {
|
|
|
InputStreamReader read = null;
|
|
|
try {
|
|
|
read = new InputStreamReader(new FileInputStream(file), "UTF-8");
|
|
@ -122,20 +123,19 @@ public class FileUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 清空文件内容
|
|
|
*
|
|
|
* @param filePath 文件路径
|
|
|
* @param content 写入内容
|
|
|
* @param content 写入内容
|
|
|
*/
|
|
|
public static void clearInfoForFile(String filePath,String content) {
|
|
|
File file =new File(filePath);
|
|
|
public static void clearInfoForFile(String filePath, String content) {
|
|
|
File file = new File(filePath);
|
|
|
try {
|
|
|
if(!file.exists()) {
|
|
|
if (!file.exists()) {
|
|
|
file.createNewFile();
|
|
|
}
|
|
|
FileWriter fileWriter =new FileWriter(file);
|
|
|
FileWriter fileWriter = new FileWriter(file);
|
|
|
fileWriter.write(content);
|
|
|
fileWriter.flush();
|
|
|
fileWriter.close();
|
|
@ -145,36 +145,34 @@ public class FileUtil {
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 记录文件最后读取的位置
|
|
|
* @param readPath 被读取文件路径
|
|
|
* @param savePath 保存被读取文件的最后位置 的文件路径
|
|
|
*
|
|
|
* @param readPath 被读取文件路径
|
|
|
* @param savePath 保存被读取文件的最后位置 的文件路径
|
|
|
*/
|
|
|
public static Integer saveFilePos(String readPath, String savePath) {
|
|
|
boolean result=false;
|
|
|
File readFile=new File(readPath);
|
|
|
Integer lenth=null;
|
|
|
try {
|
|
|
if(readFile.exists()){
|
|
|
public static Integer saveFilePos(String readPath, String savePath) {
|
|
|
boolean result = false;
|
|
|
File readFile = new File(readPath);
|
|
|
Integer lenth = null;
|
|
|
try {
|
|
|
if (readFile.exists()) {
|
|
|
InputStream inputStream = new FileInputStream(readPath);
|
|
|
lenth = inputStream.available();
|
|
|
clearInfoForFile(savePath,"");//清空内容
|
|
|
writeFile(savePath,lenth.toString(),"UTF-8");//重新写入标识
|
|
|
clearInfoForFile(savePath, "");//清空内容
|
|
|
writeFile(savePath, lenth.toString(), "UTF-8");//重新写入标识
|
|
|
}
|
|
|
}
|
|
|
catch (Exception e) {
|
|
|
} catch (Exception e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
return lenth;
|
|
|
}
|
|
|
|
|
|
public static Integer getFileSize(String path) {
|
|
|
Integer size=0;
|
|
|
InputStream inputStream=null;
|
|
|
File file=new File(path);
|
|
|
if (file.exists()){
|
|
|
Integer size = 0;
|
|
|
InputStream inputStream = null;
|
|
|
File file = new File(path);
|
|
|
if (file.exists()) {
|
|
|
try {
|
|
|
inputStream = new FileInputStream(path);
|
|
|
size = inputStream.available();
|
|
@ -182,12 +180,13 @@ public class FileUtil {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
}
|
|
|
return size;
|
|
|
return size;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 读取文本文件内容
|
|
|
* @param file 文件路径
|
|
|
*
|
|
|
* @param file 文件路径
|
|
|
* @return
|
|
|
*/
|
|
|
public static String readFileText(File file) {
|
|
@ -215,4 +214,34 @@ public class FileUtil {
|
|
|
return stringBuilder.toString();
|
|
|
}
|
|
|
|
|
|
public static File writeFile(String filePathAndName, String fileContent) {
|
|
|
File f = new File(filePathAndName);
|
|
|
OutputStreamWriter write =null;
|
|
|
BufferedWriter writer=null;
|
|
|
try {
|
|
|
if (!f.exists()) {
|
|
|
f.createNewFile();
|
|
|
}
|
|
|
write = new OutputStreamWriter(new FileOutputStream(f), "UTF-8");
|
|
|
writer = new BufferedWriter(write);
|
|
|
writer.write(fileContent);
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
System.out.println("写文件内容操作出错");
|
|
|
e.printStackTrace();
|
|
|
}finally {
|
|
|
try {
|
|
|
if(writer!=null){
|
|
|
writer.close();
|
|
|
}
|
|
|
if(write!=null){
|
|
|
write.close();
|
|
|
}
|
|
|
}catch (Exception e){
|
|
|
e.printStackTrace();;
|
|
|
}
|
|
|
}
|
|
|
return f;
|
|
|
}
|
|
|
|
|
|
}
|