com.datastax.util.io.FileUtil Maven / Gradle / Ivy
The newest version!
package com.datastax.util.io;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
public class FileUtil {
public static String readFile(File file){
String read;
FileReader fileread;
String readStr="";
try {
fileread = new FileReader(file);
BufferedReader bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
readStr = readStr + read+ "\r\n";
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufread!=null){
bufread.close();
}
if(fileread!=null){
fileread.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return readStr;
}
public static String readDir(File file,boolean printLog){
StringBuffer sb=new StringBuffer();
if(file.isDirectory()){
File[] subFiles=file.listFiles();
for(File subFile : subFiles){
if(subFile.isFile()){
if(printLog){
System.out.println("正在处理文件:"+subFile.getName());
}
sb.append(readFile(subFile));
}
}
}else {
sb.append(readFile(file));
}
return sb.toString();
}
public static List readFileText(File file){
String read;
FileReader fileread;
List texts=new ArrayList();
try {
fileread = new FileReader(file);
BufferedReader bufread = new BufferedReader(fileread);
try {
while ((read = bufread.readLine()) != null) {
texts.add(read);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufread!=null){
bufread.close();
}
if(fileread!=null){
fileread.close();
}
}
} catch (IOException e) {
e.printStackTrace();
}
return texts;
}
public static List readFileText(String path){
String read;
BufferedReader bufread=null;
List texts=new ArrayList();
try {
File file=new File(path);
if(file.exists()) {
//FileReader fileread = new FileReader(file);
InputStreamReader isr=new InputStreamReader(new FileInputStream(path),"UTF-8");
bufread = new BufferedReader(isr);
}else {
InputStream in = FileUtil.class.getResourceAsStream(path);
bufread=new BufferedReader(new InputStreamReader(in, "UTF-8"));
}
try {
while ((read = bufread.readLine()) != null) {
texts.add(read);
}
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufread!=null){
bufread.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return texts;
}
public static String readFileString(String path){
StringBuffer sb=new StringBuffer();
for(String line : readFileText(path)){
sb.append(line+"\r\n");
}
return sb.toString();
}
public static void writeFile(File file,String newStr,boolean append){
BufferedWriter writer = null;
try {
writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file, append)));
writer.write(newStr);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (writer != null)
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void writeFile(File file,List words,boolean append){
for(String word : words){
writeFile(file,word+"\r\n",append);
}
}
public static void writeFileBatch(File file,List words,boolean append){
writeFileBatch(file,words,append,"\r\n");
}
public static void writeFileBatch(File file,List words,boolean append,String sep){
StringBuffer sb=new StringBuffer();
int index=0;
for(String word : words){
if(index==words.size()-1){
sb.append(word);
}else {
sb.append(word + sep);
}
}
writeFile(file,sb.toString(),append);
}
//java serialize a object to file
public static void writeObject(String path,Object map) throws IOException{
File f=new File(path);
FileOutputStream out=new FileOutputStream(f);
ObjectOutputStream objwrite=new ObjectOutputStream(out);
objwrite.writeObject(map);
objwrite.flush();
objwrite.close();
}
// read the object from the file
public static Object readObject(String path) throws IOException, ClassNotFoundException{
FileInputStream in=new FileInputStream(path);
ObjectInputStream objread=new ObjectInputStream(in);
Object map=objread.readObject();
objread.close();
return map;
}
public static List subDirList(File file){
List dirs=new ArrayList();
File[] subFiles=file.listFiles();
for(File subFile : subFiles){
if(subFile.isDirectory()){
dirs.add(subFile.getName());
}
}
return dirs;
}
public static String[] subDirs(File file){
List dirList=subDirList(file);
if(dirList==null || dirList.size()==0) return null;
String[] dirs=new String[dirList.size()];
for(int i=0;i