import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.Map;
import org.jdom.Document;
import org.jdom.Element;
public class FileUtils {
//创建文件
public static boolean creatTxtFile(String filenameTemp) {
boolean flag = false;
File filename = new File(filenameTemp);
if (!filename.exists()) {
try {
filename.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
flag = true;
}
return flag;
}
/**
* 写文件
*
* @param newStr
* 新内容
* @throws IOException
*/
public static int writeTxtFile(String newStr, String filenameTemp) throws IOException {
// 先读取原有文件内容,然后进行写入操作
int bytelength = 0;
String filein = newStr;
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
// 文件路径
File file = new File(filenameTemp);
// 将文件读入输入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
// 保存该文件原有的内容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行与行之间的分隔符 相当于“n”
// buf = buf.append(System.getProperty("line.separator"));
buf = buf.append((char) 10);
}
buf.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
bytelength = buf.toString().getBytes().length;
} catch (IOException e1) {
// TODO 自动生成 catch 块
throw e1;
} finally {
if (pw != null) {
pw.close();
}
if (fos != null) {
fos.close();
}
if (br != null) {
br.close();
}
if (isr != null) {
isr.close();
}
if (fis != null) {
fis.close();
}
}
return bytelength;
}
/**
* 将文件转换成byte数组
* @param filePath
* @return
*/
public static byte[] File2byte(File tradeFile){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
//获取txt中的总行数
public static int getTextLines(String path) throws IOException {
FileReader fr = new FileReader(path); //这里定义一个字符流的输入流的节点流,用于读取文件(一个字符一个字符的读取)
BufferedReader br = new BufferedReader(fr); // 在定义好的流基础上套接一个处理流,用于更加效率的读取文件(一行一行的读取)
int x = 0; // 用于统计行数,从0开始
while(br.readLine() != null) { // readLine()方法是按行读的,返回值是这行的内容
x++; // 每读一行,则变量x累加1
}
fr.close();
br.close();
return x; //返回总的行数
}
//文件重命名
public static void updateFileNames(String url,String oldChar,String newChar) {
//获取文件所在目录
File file = new File(url);
// 获取文件夹绝对路径
// 判断文件目录是否存在,且是文件目录,非文件
if (file.exists() && file.isDirectory()) {
// 遍历文件夹下的所有文件
File[] childFiles = file.listFiles();
for (int i = 0; i < childFiles.length; i++) {
File file2 = childFiles[i];
if (file2.isFile()) {
//下面是对文件重命名的操作,可根据需要修改。利用String里面的方法
String oldName = file2.getName();
String newName = oldName.replace(oldChar, newChar);
if(oldName.split("\\.")[oldName.split("\\.").length-1].toUpperCase().equals("TXT")){
file2.renameTo(new File(url + "/" + newName));
}
//如果是文件夹,继续递归
}
}
}
}
/**
* 生成xml方法
*/
public static Document createXml(String BMJC,Map map){
Element handler = new Element(BMJC);
Document document = new Document(handler);
Element channel = null;
try {
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
channel = new Element(key.toString());
channel.setText(val.toString());
handler.addContent(channel);
}
// 4、创建XMLOutputter的对象
System.out.println("生成xml成功");
return document;
} catch (Exception e) {
System.out.println("生成xml失败");
return null;
}
}
//删除文件
public static void deleteFile(String url,String[] leixingArr) {
//获取文件所在目录
File file = new File(url);
// 获取文件夹绝对路径
// 判断文件目录是否存在,且是文件目录,非文件
if (file.exists() && file.isDirectory()) {
// 遍历文件夹下的所有文件
File[] childFiles = file.listFiles();
for (int i = 0; i < childFiles.length; i++) {
File file2 = childFiles[i];
if (file2.isFile()) {
//下面是对文件重命名的操作,可根据需要修改。利用String里面的方法
String oldName = file2.getName();
for(int j = 0 ; j<leixingArr.length;j++){
if(oldName.split("\\.")[oldName.split("\\.").length-1].toUpperCase().equals(leixingArr[j])){
File file3 = new File(url + "/" + oldName);
if (file3.exists()) {
if(file3.isFile()){
file3.delete();
}
}
}
}
}
}
}
}
}
java 文件操作,创建,删除,重命名,生成xml,写入文件,文件转byte,获取txt中的行数
于 2021-10-29 16:48:56 首次发布