import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.FileWriter;
/**
* 文件操作类
*
* @author 9995857
* @version v1.0
*/
public class FileIo {
/**
* 写文本文件内容
*
* @param filePathAndName
* 文本文件完整绝对路径及文件名
* @param fileContent
* 文本文件内容
* @param encoding
* 编码方式 例如 GBK 或者 UTF-8
* @return 文本文件完整绝对路径及文件名
*/
public static String writeText(String filePathAndName, String fileContent,
String encoding) {
if (encoding.equals(""))
encoding = "UTF-8";
filePathAndName = filePathAndName.replace(File.separator, "/");
PrintWriter myFile = null;
try {
File myFilePath = new File(filePathAndName);
if (!myFilePath.exists()) {
createFolders(filePathAndName.substring(0, filePathAndName
.lastIndexOf("/")));
myFilePath.createNewFile();
}
myFile = new PrintWriter(myFilePath, encoding);
myFile.print(fileContent);
myFile.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (myFile != null)
myFile.close();
}
return filePathAndName;
}
/**
* 追加写文本文件内容
*
* @param filePathAndName
* 文本文件完整绝对路径及文件名
* @param fileContent
* 文本文件内容
* @param encoding
* 编码方式 例如 GBK 或者 UTF-8 默认UTF-8
* @return 文本文件完整绝对路径及文件名
*/
public static String appendWriteText(String filePathAndName,
String fileContent, String encoding) {
if (encoding.equals(""))
encoding = "UTF-8";
filePathAndName = filePathAndName.replace(File.separator, "/");
FileWriter myFile = null;
try {
File myFilePath = new File(filePathAndName);
if (!myFilePath.exists()) {
return writeText(filePathAndName, fileContent, encoding);
}
myFile = new FileWriter(filePathAndName, true);
myFile.write(fileContent);
myFile.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (myFile != null)
myFile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return filePathAndName;
}
/**
* 读取文本文件内容
*
* @param filePathAndName
* 带有完整绝对路径的文件名
* @param encoding
* 文本文件打开的编码方式 GBK UTF-8
* @return 返回文本文件的内容
*/
public static String readText(String filePathAndName, String encoding) {
if (encoding == null)
encoding = "UTF-8";
encoding = encoding.trim();
StringBuffer str = new StringBuffer("");
String st = "";
FileInputStream fs = null;
InputStreamReader isr = null;
try {
fs = new FileInputStream(filePathAndName);
isr = new InputStreamReader(fs, encoding);
BufferedReader br = new BufferedReader(isr);
try {
String data = "";
while ((data = br.readLine()) != null) {
str.append(data + " ");
}
} catch (Exception e) {
str.append(e.toString());
} finally {
if (fs != null)
fs.close();
if (isr != null)
isr.close();
}
st = str.toString();
} catch (IOException e) {
e.printStackTrace();
}
return st;
}
/**
* 写二进制文件
*
* @param inputStream
* 二进制流
* @param newpath
* 保存路径
* @param newfilename
* 保存的文件名
* @return 保存的文件名
*/
public static String writeFile(InputStream inputStream, String newpath,
String newfilename) {
FileOutputStream outStream = null;
try {
newpath = newpath.replace(File.separator, "/");
if (newpath.endsWith("/")) {
newpath = newpath.substring(0, newpath.length() - 1);
}
writeText(newpath + "/" + newfilename, "", "");
outStream = new FileOutputStream(newpath + "/" + newfilename);
byte[] buffer = new byte[1024 * 5];
int byteread = 0;
while ((byteread = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, byteread);
outStream.flush();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outStream != null)
outStream.close();
if (inputStream != null)
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return newfilename;
}
/**
* 写二进制文件
*
* @param byteA
* 二进数组
* @param newpath
* 保存路径
* @param newfilename
* 保存的文件名
* @return 保存的文件名
*/
public static String writeFile(byte[] byteA, String newpath,
String newfilename) {
FileOutputStream outStream = null;
try {
newpath = newpath.replace(File.separator, "/");
if (newpath.endsWith("/")) {
newpath = newpath.substring(0, newpath.length() - 1);
}
writeText(newpath + "/" + newfilename, "", "");
outStream = new FileOutputStream(newpath + "/" + newfilename);
outStream.write(byteA);
outStream.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (outStream != null)
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return newfilename;
}
/**
* 读取二进制文件
*
* @param filePathAndName
* 带有完整绝对路径的文件名
* @return 二进制流
*/
public static byte[] readFile(String filePathAndName) {
byte[] byteA = null;
try {
File file = new File(filePathAndName);
if (file.exists()) {
InputStream inputStream = new FileInputStream(file);
inputStream.read(byteA);
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return byteA;
}
/**
* 多级目录创建
*
* @param 要创建的目录路径
* @return 创建好的目录路径
*/
public static String createFolders(String multiFolderPath) {
String cuttentFolder = "";
multiFolderPath = multiFolderPath.replace(File.separator, "/");
String[] multi = multiFolderPath.split("/");
if (multi.length == 0)
return "";
for (int i = 0; i < multi.length; i++) {
cuttentFolder += multi[i] + "/";
createFolder(cuttentFolder);
}
return cuttentFolder;
}
/**
* 新建目录
*
* @param folderPath
* 目录
* @return 返回目录创建后的路径
*/
private static String createFolder(String folderPath) {
File myFilePath = new File(folderPath);
if (!myFilePath.exists())
myFilePath.mkdir();
return folderPath;
}
/**
* 删除文件
*
* @param filePathAndName
* 文本文件完整绝对路径及文件名
* @return Boolean 成功删除返回true 遭遇异常返回false
*/
public static boolean deleteFile(String filePathAndName) {
boolean ret = false;
try {
File myDelFile = new File(filePathAndName);
if (myDelFile.isFile() && myDelFile.exists()) {
myDelFile.delete();
ret = true;
} else {
ret = false;
}
} catch (Exception e) {
ret = false;
}
return ret;
}
/**
* 删除指定文件夹下所有文件
*
* @param filePath
* 文件夹完整绝对路径
* @return 成功删除返回true 遭遇异常返回false
*/
public static boolean deleteAllFile(String filePath) {
boolean ret = false;
filePath = filePath.replace(File.separator, "/");
File file = new File(filePath);
if (!file.exists()) {
return ret;
}
if (!file.isDirectory()) {
return ret;
}
String[] tempList = file.list();
File temp = null;
for (int i = 0; i < tempList.length; i++) {
if (filePath.endsWith("/")) {
filePath = filePath.substring(0, filePath.length() - 1);
}
temp = new File(filePath + File.separator + tempList[i]);
if (temp.isFile()) {
temp.delete();
ret = true;
} els
Flex 组件Flex 组件Flex 组件
5星 · 超过95%的资源 需积分: 9 140 浏览量
2009-07-03
16:26:14
上传
评论 2
收藏 1.92MB RAR 举报
lc9995857
- 粉丝: 5
- 资源: 22
最新资源
- PHP端通过modbus协议跟第三方设备进行数据通信
- navicat安装包亲测可用
- 算法部署-使用OpenVINO部署MobileStyleGAN轻量化高保真图像合成算法-项目源码-优质项目实战.zip
- 基于java实现远程采集华为逆变器使用modbus tcp协议进行通讯的设备数据
- Unity画面共享Spout插件
- 基于C++用modbus实现的工业设备的数据采集程序,支持Tcp、串口
- 完结12章AI Agent智能应用从0到1定制开发
- 15白落梅:你是锦瑟我为流年:三毛的万水千山-3491776.mobi
- Federated Learning-Aided Prognostics in the Shipping 4.0: Princi
- OFDM 的鲁棒频率和定时同步文献部分阅读笔记
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈