package cn.rojao.utils;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
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.Writer;
import java.net.HttpURLConnection;
import java.net.URL;
public class FileUtils {
/**
* 从输入流中读取string
* @param inputStream
* @return
*/
public static String getString(InputStream inputStream){
String returnStr = "";
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"));
StringBuffer sb = new StringBuffer();
String str = "";
while ((str = reader.readLine()) != null)
{
sb.append(str).append("\n");
}
returnStr = sb.toString();
}catch(Exception e){
}
return returnStr;
}
/**
* 新建一个文件并写入内容
*
* @param String path 文件全路径
* @param String fileName 文件名
* @param String content 内容
* @param int bufLen 设置缓冲区大小
* @param boolean isWrite 是否追加写入文件
* @return boolean
* @throws IOException
*/
public static boolean newStaticFile(String path, String fileName, String content,
int bufLen, boolean isWrite) {
if (path == null || path.equals("") || content == null
|| content.equals(""))
return false;
boolean flag = false;
File file = new File(path);
if (!file.exists()) {
file.mkdirs();
}
FileOutputStream fos = null;
Writer out = null;
try {
fos = new FileOutputStream(path + File.separator + fileName,
isWrite);
out = new BufferedWriter(new OutputStreamWriter(fos, "UTF-8"),
bufLen);
out.write(content);
out.flush();
flag = true;
//log.info(content);
} catch (IOException e) {
System.out.println("写入文件出错");
flag = false;
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return flag;
}
/**
* 复制一个目录及其子目录、文件到另外一个目录
* @param src
* @param dest
* @throws IOException
*/
public static void copyFolder(File src, File dest){
try{
if (src.isDirectory()) {
if (!dest.exists()) {
dest.mkdir();
}
String files[] = src.list();
for (String file : files) {
File srcFile = new File(src, file);
File destFile = new File(dest, file);
// 递归复制
copyFolder(srcFile, destFile);
}
} else {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dest);
byte[] buffer = new byte[1024];
int length;
while ((length = in.read(buffer)) > 0) {
out.write(buffer, 0, length);
}
in.close();
out.close();
}
}catch(Exception e){
e.printStackTrace();
}
}
/**
* 递归删除目录下的所有文件及子目录下所有文件
* @param dir 将要删除的文件目录
* @return boolean Returns "true" if all deletions were successful.
* If a deletion fails, the method stops attempting to
* delete and returns "false".
*/
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// 目录此时为空,可以删除
return dir.delete();
}
/**
* 复制单个文件
*
* @param oldPath
* String 原文件路径 如:c:/fqf.txt
* @param newPath
* String 复制后路径 如:f:/
* @return boolean
*/
public static boolean copyFile(String oldPath, String newPath,
String newFileName) throws IOException {
boolean flag = false;
if (oldPath == null || newPath == null || newPath.equals("")
|| oldPath.equals("")) {
return flag;
}
InputStream inStream = null;
FileOutputStream fs = null;
try {
int bytesum = 0;
int byteread = 0;
File file = null;
file = new File(newPath);
if (!file.exists()) {
file.mkdirs();
}
file = new File(oldPath);
if (file.exists()) { // 文件存在时
inStream = new FileInputStream(oldPath); // 读入原文件
if (newFileName == null || newFileName.equals("")) {
newFileName = file.getName();
}
fs = new FileOutputStream(newPath + File.separator
+ newFileName);
byte[] buffer = new byte[1024 * 8];
while ((byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; // 字节数 文件大小
// System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
flag = true;
}
} catch (IOException e) {
throw e;
} finally {
try {
if (fs != null) {
fs.close();
}
if (inStream != null) {
inStream.close();
}
} catch (IOException e) {
throw e;
}
}
return flag;
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName)
throws IOException {
return readFileContent(filePathAndName, null, null, 1024);
}
/**
* 读取文本文件内容,以行的形式读取
*
* @param String
* filePathAndName 带有完整绝对路径的文件名
* @param String
* encoding 文本文件打开的编码方式 例如 GBK,UTF-8
* @param String
* sep 分隔符 例如:#,默认为/n;
* @param int
* bufLen 设置缓冲区大小
* @return String 返回文本文件的内容
*/
public static String readFileContent(String filePathAndName,
String encoding, String sep, int bufLen) throws IOException {
if (filePathAndName == null || filePathAndName.equals("")) {
return "";
}
if (sep == null || sep.equals("")) {
sep = System.getProperty("line.separator");
}
if (!new File(filePathAndName).exists()) {
return "";
}
StringBuffer str = new StringBuffer("");
FileInputStream fs = null;
InputStreamReader isr = null;
BufferedReader br = null;
try {
fs = new FileInputStream(filePathAndName);
if (encoding == null || encoding.trim().equals("")) {
isr = new InputStreamReader(fs);
} else {
isr = new InputStreamReader(fs, encoding.trim());
}
br = new BufferedReader(isr, bufLen);
String data = "";
while ((data = br.readLine()) != null) {
if(!str.toString().isEmpty()){
str.append(sep);
}
str.append(data);
}
} catch (IOException e) {
java File文件处理工具类
需积分: 48 97 浏览量
2017-12-01
10:29:06
上传
评论 1
收藏 3KB RAR 举报
CNZYYH
- 粉丝: 39
- 资源: 12
最新资源
- 基于JavaWeb的足球网平台设计(毕业设计源码)
- 基于matlab实现不同老化天数的玉米籽粒的THz光谱,用移动窗口相关系数法找出特征波段.rar
- 基于matlab实现单纯型算法和最速下降法的综合算法,能很快的搜索到最小值 .rar
- 基于matlab实现该算法可以用于新安江水文模型的参数优选,从而进行降雨径流过程模拟 .rar
- 基于matlab实现混沌单纯形法算子的布谷鸟搜索算法,算法引入混沌算子及单纯形法,提高了布谷鸟搜索算法的.rar
- 基于matlab实现能完成基本二维条形码的识别,包含gui界面编程,可对较为清晰的拍摄二维条形码图像进行识别.rar
- 基于matlab实现石墨烯在太赫兹频率的非线性光学特性的数值模拟.rar
- 基于matlab实现水文频率处理.rar
- 基于matlab实现太赫兹和中红外频率下,石墨烯表面电导率的公式.rar
- 土壤湿度数据文件AMSR-E Aqua L2B Surface Soil Moisture, Ancillary.pdf
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈