package file;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
/**
* File Operation Class
*
*
@author liangyongbiao
*
*/
public class FileOperate {
/**
* Read Form File
*
@throws IOException
*/
public static List ReadFromFile(String strfile) throws IOException {
CheckFileExit(strfile);
File file = new File(strfile);
List list=new ArrayList();
try {
InputStreamReader read = new InputStreamReader(new FileInputStream(
file), "GBK");
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
list.add(lineTxt);
}
read.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public static void main(String[] args) throws IOException {
WriteToFileWithOneLine("D:/file/fileoper.txt", "附近萨芬的克里斯");
// System.out.println(CheckFileNotNull("D:/file/fileoper.txt"));
}
/**
* Write To File
*
@throws IOException
*/
public static void WriteToFile(String strfile,String str) throws IOException {
File file=new File(strfile);
CheckFileExit(strfile);
RandomAccessFile randomAccessFile=null;
try{
randomAccessFile=new RandomAccessFile(file, "rw");
long fileLength=randomAccessFile.length();
randomAccessFile.seek(fileLength);
randomAccessFile.writeBytes(str);
//也可以使用下面的方式追加
// FileWriter writer = new FileWriter(file, true);
// writer.write(str);
// writer.close();
}catch(IOException e){
e.printStackTrace();
}
finally{
if(randomAccessFile!=null){
try{
randomAccessFile.close();
}catch(IOException e){
e.printStackTrace();
}
}
}
}
/**
* Check File is exit or not
*/
public static void CheckFileExit(String strfile) {
File file = new File(strfile);
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Clean File
*/
public static void CleanFile(String strfile) {
CheckFileExit(strfile);
try {
FileWriter fileWriter = new FileWriter(strfile);
fileWriter.write("");
fileWriter.flush();
fileWriter.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
*
@author liangyongbiao
* Copy A File Content
*/
public File CopyFileToFile(File sourceFile, File ReturnFile) {
return ReturnFile;
}
/**
*
@author liangyongbiao * Append To File end */ public File AppendToFileEnd(String strfile) { CheckFileExit(strfile); File file = new File(strfile); return file; } /** * Check File Content is null or not * @throws IOException */ @SuppressWarnings("resource") public static Boolean CheckFileNotNull(String strfile) throws IOException { Boolean flag = false; File file=new File(strfile); CheckFileExit(strfile); FileInputStream fileInputStream; try { fileInputStream = new FileInputStream(file); int size=fileInputStream.available(); if(size==0){ System.out.println("文件为空!!"); }else{ flag=true; } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } return flag; } /** * Radrom Write To File */ public File randomWriteToFile(String strfile, String str) { File file = new File(strfile); return file; } /** * One Line One Read */ public static File WriteToFileWithOneLine(String strfile,String str){ File file=new File(strfile); CheckFileExit(strfile); FileWriter writer; try { writer = new FileWriter(file, true); str=str+"\n"; writer.write(str); writer.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return file; } }