package com.common.utils;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
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;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
/**
* 文件常用工具包
* @since 1.0
*/
public class FileUtils {
/**
* 读取绝对路径的文件内容
* @param filename
* String 绝对路径+文件名+文件类型
* @return String
*/
public String readTruePathFile(String filename) {
StringBuffer strBuffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filename), "UTF-8"));
String line = null;
while ((line = br.readLine()) != null) {
strBuffer.append(line).append("\r\n");
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
return strBuffer.toString();
}
/**
* 写文件
* @param strBuffer
* 写入的字符串
* @param filename
* 绝对路径+文件名+文件类型
*/
public void write(String strBuffer, String filename) {
try {
filename = filename.replaceAll("\\\\", "/");
File file = new File(filename);
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(out, "GB2312");
osw.write(strBuffer);
osw.close();
file.exists();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 文件结尾处追加数据
* @param filePath
* @param content
*/
public void appendFile(String filePath, String content) {
try {
//打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
FileWriter writer = new FileWriter(filePath.replace("\\", "/"), true);
writer.write(content);
writer.close();
} catch (IOException e) {
createFile(filePath);
appendFile(filePath, content);
}
}
/**
* 创建文件及文件夹
* @param filePath
*/
public void createFile(String filePath) {
mkdirs(filePath.replaceAll("[\\w-]+\\.\\w+$", ""));
try {
new File(filePath.replace("\\", "/")).createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 创建文件夹
* @param filename
*/
public void mkdirs(String filename) {
String[] dirname = filename.replace("\\", "/").replaceAll("[A-Za-z0-9.]+$", "").split("/");
StringBuffer filepath = new StringBuffer();
for (int i = 0; i < dirname.length; i++) {
File file = new File(filepath.append(dirname[i]).append("/").toString());
file.mkdir();
}
}
/**
* 利用传入的InputStream写文件到filename
* @param filename
* String 绝对路径+文件名+文件类型
* @param in
* InputStream
*/
public void write(String filename, InputStream in) {
byte[] b = new byte[2048];
try {
filename = filename.replaceAll("//", "/");
File file = new File(filename);
if (file.isFile()) {
file.delete();
}
file.createNewFile();
FileOutputStream out = new FileOutputStream(file);
int len = 0;
do {
len = in.read(b);
if (len == -1) break;
out.write(b, 0, len);
} while (len != -1);
file.exists();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 获取从classpath根目录开始读取文件注意转化成中文
* @param path
* String
* @return String
*/
public String getCPFile(String path) {
URL url = FileUtils.class.getClassLoader().getResource(path);
String filepath = url.getFile();
File file = new File(filepath);
byte[] retBuffer = new byte[(int) file.length()];
try {
FileInputStream fis = new FileInputStream(filepath);
fis.read(retBuffer);
fis.close();
return new String(retBuffer, "UTF-8");
} catch (IOException e) {
return null;
}
}
/**
* 利用java本地拷贝文件及文件夹,实现文件夹对文件夹的拷贝
* @param objDir
* 目标文件夹
* @param srcDir
* 源的文件夹
* @throws IOException
*/
public void copyDirectiory(String objDir, String srcDir) throws IOException {
(new File(objDir)).mkdirs();
File[] file = (new File(srcDir)).listFiles();
for (int i = 0; i < file.length; i++) {
if (file[i].isFile()) {
FileInputStream input = new FileInputStream(file[i]);
FileOutputStream output = new FileOutputStream(objDir + "/" + file[i].getName());
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
if (file[i].isDirectory()) {
copyDirectiory(objDir + "/" + file[i].getName(), srcDir + "/" + file[i].getName());
}
}
}
/**
* 将一个文件inName拷贝到另外一个文件outName中
* @param inName
* 源文件路径
* @param outName
* 目标文件路径
* @throws FileNotFoundException
* @throws IOException
*/
public void copyFile(String inName, String outName) throws FileNotFoundException, IOException {
BufferedInputStream is = new BufferedInputStream(new FileInputStream(inName));
BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(outName));
copyFile(is, os, true);
}
public void copyFile(InputStream is, OutputStream os, boolean close) throws IOException {
int b;
while ((b = is.read()) != -1) {
os.write(b);
}
is.close();
if (close) os.close();
}
/**
* 拷贝文件
* @param inName
* String
* @param pw
* PrintWriter
* @param close
* boolean 执行完毕后Writer是否要关闭
* @throws FileNotFoundException
* @throws IOException
*/
public void copyFile(String inName, PrintWriter pw, boolean close) throws FileNotFoundException, IOException {
BufferedReader is = new BufferedReader(new FileReader(inName));
copyFile(is, pw, close);
}
private void copyFile(Reader is, Writer os, boolean close) throws IOException {
int b;
while ((b = is.read()) != -1) {
os.write(b);
}
is.close();
if (close) os.close();
}
/**
* 从文件inName中读取第一行的内容
* @param inName
* 源文件路径
* @return 第一行的内容
* @throws FileNotFoundException
* @throws IOException
*/
public String readLine(String inName) throws FileNotFoundException, IOException {
BufferedReader is = new BufferedReader(new FileReader(inName));
String line = null;
line = is.readLine();
is.close();
return line;
}
/**
* default buffer size
*/
private final int BLKSIZ = 8192;
/**
* 拷贝文件
* @param inName
* String 输入文件名
* @param outName
* String 输出文件名
* @throws FileNotFoundException
* @throws IOException
*/
public void copyFileBuffered(String inName, String outName) throws FileNotFoundException, IOException {
InputStream is = new FileInputStream(inName);
OutputStream os = new FileOutputStream(outName);
int count = 0;
byte b[] = new byte[BLKSIZ];
while ((count = is.read(b)) != -1) {
os.write(b, 0, count);
}
is.close();
os.close();
}
/**
* 将String变成文本文件
* @param t
java实现cad(dxf格式)转svg
2星 需积分: 38 166 浏览量
2018-11-19
17:01:18
上传
评论 1
收藏 6.55MB RAR 举报
hege05141
- 粉丝: 0
- 资源: 5
最新资源
- base.apk
- Vue 结合 D3.js 进行数据可视化开发的练手案例.zip
- 语音模块使用说明_jr6001调试工具.html
- 毕业设计-reat18货物运输后台管理系统
- Qt tcp通信(exe)
- tensorflow-gpu-2.7.3-cp39-cp39-manylinux2010-x86-64.whl
- tensorflow-2.8.0-cp37-cp37m-manylinux2010-x86-64.whl
- tensorflow-2.7.4-cp39-cp39-manylinux2010-x86-64.whl
- HTML后台管理系统源49套合集 H5模板源码下载 总有一款适合你 静态页面
- DDColor-PDF文献
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈