import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class SiteMap {
private static String path = "";
private static String filenameTemp;
public static void main(String[] args) throws IOException {
creatTxtFile("src/main/resources/siteMap");
writeTxtFile("我要輸入內(nèi)容!");
}
/**
* 創(chuàng)建文件
*
* @throws IOException
*/
public static boolean creatTxtFile(String name) throws IOException {
boolean flag = false;
filenameTemp = path + name + ".txt";
File filename = new File(filenameTemp);
if (!filename.exists()) {
filename.createNewFile();
flag = true;
} else {
filename.delete();
filename.createNewFile();
flag = true;
}
return flag;
}
/**
* 寫文件
*
* @param newStr
*? ? ? ? ? ? 新內(nèi)容
* @throws IOException
*/
public static boolean writeTxtFile(String newStr) throws IOException {
// 先讀取原有文件內(nèi)容脏款,然后進(jìn)行寫入操作
boolean flag = false;
String filein = newStr + "\r\n";
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();
// 保存該文件原有的內(nèi)容
for (int j = 1; (temp = br.readLine()) != null; j++) {
buf = buf.append(temp);
// System.getProperty("line.separator")
// 行與行之間的分隔符 相當(dāng)于“\n”
buf = buf.append(System.getProperty("line.separator"));
}
buf.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
flag = true;
} catch (IOException e1) {
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 flag;
}
}