/*
* Created on 2009-6-3
* Copyright 2009 by www.xfok.net All Rights Reserved
*
*/
package net.xfok.common;
import java.io.BufferedReader;
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.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author YangHua
* 转载请注明出处:http://www.xfok.net/2009/10/134499.html
*/
public class MyFileOper {
/**
* 写文件
*
* @param content
* 内容
* @param name
* 文件名
* @param path
* 保存路径
* @throws IOException
*/
public void writeFile(String content, String name, String path)
throws IOException {
if (!new File(path).isDirectory())
new File(path).mkdirs();
FileWriter fw = new FileWriter(path + name);
fw.write(content);
fw.close();
}
public void writeFile(String content, String name, String path,
String charset) throws IOException {
if (!new File(path).isDirectory())
new File(path).mkdirs();
try {
OutputStreamWriter out = new OutputStreamWriter(
new FileOutputStream(path + name), charset);
out.write(content);
out.flush();
out.close();
System.out.println("success...");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void writeFile(byte[] encrypt, String name, String path)
throws IOException {
if (!new File(path).isDirectory())
new File(path).mkdirs();
RandomAccessFile randomFile = new RandomAccessFile(path + name, "rw");
long fileLength = randomFile.length();
randomFile.seek(fileLength);
randomFile.write(encrypt);
randomFile.close();
}
/**
* 读文件
*
* @param name
* 文件名
* @param path
* 存储路径
* @return
* @throws IOException
*/
public String readFile(String name, String path) throws IOException {
StringBuffer sb = new StringBuffer();
FileReader fr = new FileReader(path + name);
int i = 0;
while ((i = fr.read()) != -1) {
sb.append((char) i);
}
fr.close();
return sb.toString();
}
public String readFile(String name, String path, String charset)
throws IOException {
StringBuffer content = new StringBuffer();
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
path + name), charset);
BufferedReader reader = new BufferedReader(read);
String line;
while ((line = reader.readLine()) != null) {
content.append(line);
}
} catch (Exception e) {
e.printStackTrace();
}
return content.toString();
}
/**
* 删除文件
*
* @param path
* 文件路径
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public boolean delFile(String path) throws FileNotFoundException,
IOException {
try {
File file = new File(path);
if (!file.isDirectory()) {
System.out.println("1");
file.delete();
} else if (file.isDirectory()) {
System.out.println("2");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File delfile = new File(path + "//" + filelist[i]);
if (!delfile.isDirectory()) {
System.out.println("path=" + delfile.getPath());
System.out.println("absolutepath="
+ delfile.getAbsolutePath());
System.out.println("name=" + delfile.getName());
delfile.delete();
System.out.println("删除文件成功");
} else if (delfile.isDirectory()) {
delFile(path + "//" + filelist[i]);
}
}
file.delete();
}
} catch (FileNotFoundException e) {
System.out.println("delFile() Exception:" + e.getMessage());
}
return true;
}
/**
* 读取某个文件夹下的所有文件夹和文件
*
* @param path
* 文件夹
* @throws FileNotFoundException
* @throws IOException
* @return boolean
*/
public boolean readFile(String path) throws FileNotFoundException,
IOException {
try {
File file = new File(path);
if (!file.isDirectory()) {
System.out.println("文件");
System.out.println("path=" + file.getPath());
System.out.println("absolutepath=" + file.getAbsolutePath());
System.out.println("name=" + file.getName());
} else if (file.isDirectory()) {
System.out.println("文件夹");
String[] filelist = file.list();
for (int i = 0; i < filelist.length; i++) {
File readfile = new File(path + "//" + filelist[i]);
if (!readfile.isDirectory()) {
System.out.println("path=" + readfile.getPath());
System.out.println("absolutepath="
+ readfile.getAbsolutePath());
System.out.println("name=" + readfile.getName());
} else if (readfile.isDirectory()) {
readFile(path + "//" + filelist[i]);
}
}
}
} catch (FileNotFoundException e) {
System.out.println("readFile() Exception:" + e.getMessage());
}
return true;
}
/**
* 得到文件夹中的文件列表
*
* @param path
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public List<String> readFiles(String path) throws FileNotFoundException,
IOException {
List<String> list = new ArrayList();
try {
File file = new File(path);
list = Arrays.asList(file.list());
} catch (Exception e) {
System.out.println("readFile() Exception:" + e.getMessage());
}
return list;
}
}
JAVA文件操作集合
最新推荐文章于 2022-08-12 00:02:25 发布