//創建文件
public static void createFile(File filename) {
try {
if(!filename.exists()) {
filename.createNewFile();
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
//寫入txt 內容不被覆蓋 追加寫入
public static boolean filechaseWrite(String Content,String filepath) {
boolean flag=false;
try {
FileWriter fw=new FileWriter(filepath,true);
fw.write(Content);
fw.flush();
fw.close();
flag=true;
}catch (Exception e) {
//
e.printStackTrace();
}
return flag;
}
//寫入txt內容 覆蓋原內容
public static boolean writetxtfile(String Content,String filepath) {
boolean flag=false;
try {
//寫入的txt文檔的路徑
PrintWriter pw=new PrintWriter(filepath);
//寫入的內容
pw.write(Content);
pw.flush();
pw.close();
flag=true;
}catch (Exception e) {
e.printStackTrace();
}
return flag;
}
//讀取txt內容
public static String readtxtFile(File file) {
String sResult="";
try {
InputStreamReader reader=new InputStreamReader(new FileInputStream(file),"gbk");
BufferedReader br=new BufferedReader(reader);
String s=null;
while((s=br.readLine())!=null) {
sResult+=s;
System.out.println(s);
}
}catch (Exception e) {
e.printStackTrace();
}
return sResult;
}