java 写文件 工具类_java中IO写文件工具类

以下是一些依据经常使用java类进行组装的对文件进行操作的类,平时,我更喜欢使用Jodd.io中提供的一些对文件的操作类,里面的方法写的简单易懂。当中jodd中提供的JavaUtil类中提供的方法足够我们使用,里面的方法写的很简练,比如append,read等方法,封装更好,更符合面向对象,这里面我写的一些方法可多都是模仿jodd,从里面进行抽取出来的。/**

* 获取路径目录下的全部文件

* @param path

* @return

*/

public static File[] getKeywordFiles(String path) {

File dir = new File(path);

if (!dir.exists())

return null;

File[] fs = dir.listFiles();

return fs;

}

/**

* 删除目录 param folderPath 目录完整绝对路径

*/

private static void delFolder(String folderPath) {

try {

delAllFile(folderPath); // 删除完里面全部内容

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath = new java.io.File(filePath);

myFilePath.delete(); // 删除空目录

} catch (Exception e) {

log.error(e);

}

}

/**

* 读取一个文件

* @param filePathAndName

* @return

* @throws IOException

*/

public static List readFile(String filePathAndName)

throws IOException {

FileInputStream fis = new FileInputStream(filePathAndName);

InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

BufferedReader br = new BufferedReader(isr);

LineNumberReader lnr = new LineNumberReader(br);

List returnValue = new ArrayList();

int cnt = 0;

while (true) {

cnt++;

String tempStr = lnr.readLine();

if (tempStr == null)

break;

if (tempStr.length() < 2)

continue;

returnValue.add(tempStr);

}

lnr.close();

br.close();

isr.close();

fis.close();

return returnValue;

}

/**

* 读取一个文件,并排重后返回

*/

public static List readFileNoDup(String filePathAndName)

throws IOException {

FileInputStream fis = new FileInputStream(filePathAndName);

InputStreamReader isr = new InputStreamReader(fis, "UTF-8");

BufferedReader br = new BufferedReader(isr);

LineNumberReader lnr = new LineNumberReader(br);

Set set = new HashSet();

while (true) {

String tempStr = lnr.readLine();

if (tempStr == null)

break;

if (tempStr.length() < 2)

continue;

set.add(tempStr.trim());

}

lnr.close();

br.close();

isr.close();

fis.close();

List returnValue = new ArrayList(set.size());

returnValue.addAll(set);

return returnValue;

}

/**

* 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后

* flag为true,则向现有文件里加入内容,否则覆盖原有内容

*/

public static void writeFile(String filePathAndName, String fileContent,

boolean flag) throws IOException {

if (null == fileContent || fileContent.length() < 1)

return;

File file = new File(filePathAndName);

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream fos = new FileOutputStream(filePathAndName, flag);

OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

osw.write(fileContent + "\r\n");

osw.flush();

osw.close();

}

/**

* 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后

* flag为true,则向现有文件里加入内容,否则覆盖原有内容

*/

public static void writeFile(String filePathAndName,

List fileContent, boolean flag) throws IOException {

File file = new File(filePathAndName);

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream fos = new FileOutputStream(filePathAndName, flag);

OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

for (String temp : fileContent)

osw.write(temp + "\r\n");

osw.flush();

osw.close();

}

/**

* 加入内容到指定文件 假设该文件不存在,则创建并加入内容 假设该文件已存在,则加入内容到已有内容最后

* flag为true,则向现有文件里加入内容,否则覆盖原有内容

*/

public static void writeFile(String filePath,String filename,

List fileContent, boolean flag) throws IOException {

File file = new File(filePath);

if(!file.exists()){

boolean tempFlag = file.mkdirs();

if(!tempFlag){

log.error("目录"+filePath+"创建失败");

return;

}

}

file = new File(filePath,filename);

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream fos = new FileOutputStream(filePath+filename, flag);

OutputStreamWriter osw = new OutputStreamWriter(fos, "utf-8");

for (String temp : fileContent)

osw.write(temp + "\r\n");

osw.flush();

osw.close();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值