將文件讀取并寫入一個(gè)文件夾里面的文件中
①生成一個(gè)文件路徑(選擇D盤)
private static String path = "D:\\file\\";
②創(chuàng)建文件的路徑和名稱
private static String filenameTemp;
③創(chuàng)建一個(gè)“寫入文件”的方法:
/**
* 向文件中寫入內(nèi)容
* @param filepath 文件路徑與名稱
* @param new_str 寫入的內(nèi)容
* @return
* @throws IOException
*/
public static boolean writeFileContent(String filepath,String new_str) throws IOException{
Boolean bool = false;
String filein = new_str+"\r\n";//新寫入的行,換行
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filepath);//文件路徑(包括文件名稱)
//將文件讀入輸入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
//文件原有內(nèi)容
for(int i=0;(temp =br.readLine())!=null;i++){
buffer.append(temp);
// 行與行之間的分隔符 相當(dāng)于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//不要忘記關(guān)閉
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 bool;
}
④創(chuàng)建一個(gè)“創(chuàng)建文件的方法”,并且引用“寫入文件”的方法。
public static boolean createFile(String fileName,String filecontent){
Boolean bool = false;
filenameTemp = path+fileName+".txt";//文件路徑+名稱+文件類型
File file = new File(filenameTemp);//new一個(gè)文件對(duì)象
try {
//如果文件不存在,則創(chuàng)建新的文件
if(!file.exists()){
file.createNewFile();//創(chuàng)建一個(gè)新的文件目錄。
bool = true;
System.out.println("文件創(chuàng)建成功,文件名是:"+filenameTemp);
//調(diào)用writeFileContent()方法,創(chuàng)建文件成功后,寫入內(nèi)容到文件里
writeFileContent(filenameTemp, filecontent);
}else {
bool = true;//開關(guān)打開。
System.out.println("文件已存在!");
//調(diào)用writeFileContent()方法,繼續(xù)寫入內(nèi)容到文件里
writeFileContent(filenameTemp, filecontent);
}
} catch (Exception e) {
bool = false;
e.printStackTrace();
}
return bool;
}
⑤引用“createFile”方法就可以寫入文件!
以下是總結(jié):
public class Test2 {
//生成文件路徑
private static String path = "D:\\file\\";
//文件路徑+名稱
private static String filenameTemp;
/**
* 創(chuàng)建文件
* @param fileName 文件名稱
* @param filecontent 文件內(nèi)容
* @return 是否創(chuàng)建成功,成功則返回true
*/
public static boolean createFile(String fileName,String filecontent){
Boolean bool = false;
filenameTemp = path+fileName+".txt";//文件路徑+名稱+文件類型
File file = new File(filenameTemp);//new一個(gè)文件對(duì)象
try {
//如果文件不存在,則創(chuàng)建新的文件
if(!file.exists()){
file.createNewFile();//創(chuàng)建一個(gè)新的文件目錄。
bool = true;
System.out.println("文件創(chuàng)建成功,文件名是:"+filenameTemp);
//調(diào)用writeFileContent()方法,創(chuàng)建文件成功后,寫入內(nèi)容到文件里
writeFileContent(filenameTemp, filecontent);
}else {
bool = true;//開關(guān)打開。
System.out.println("文件已存在!");
//調(diào)用writeFileContent()方法,繼續(xù)寫入內(nèi)容到文件里
writeFileContent(filenameTemp, filecontent);
}
} catch (Exception e) {
bool = false;
e.printStackTrace();
}
return bool;
}
/**
* 向文件中寫入內(nèi)容
* @param filepath 文件路徑與名稱
* @param new_str 寫入的內(nèi)容
* @return
* @throws IOException
*/
public static boolean writeFileContent(String filepath,String new_str) throws IOException{
Boolean bool = false;
String filein = new_str+"\r\n";//新寫入的行,換行
String temp = "";
FileInputStream fis = null;
InputStreamReader isr = null;
BufferedReader br = null;
FileOutputStream fos = null;
PrintWriter pw = null;
try {
File file = new File(filepath);//文件路徑(包括文件名稱)
//將文件讀入輸入流
fis = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
//文件原有內(nèi)容
for(int i=0;(temp =br.readLine())!=null;i++){
buffer.append(temp);
// 行與行之間的分隔符 相當(dāng)于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(filein);
fos = new FileOutputStream(file);
pw = new PrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool = true;
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
//不要忘記關(guān)閉
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 bool;
}
/**
* 刪除文件
* @param fileName 文件名稱
* @return
*/
public static boolean delFile(String fileName){
Boolean bool = false;
filenameTemp = path+fileName+".txt";
File file = new File(filenameTemp);
try {
if(file.exists()){
file.delete();
bool = true;
}
} catch (Exception e) {
// TODO: handle exception
}
return bool;
}
public static void main(String[] args) {
UUID uuid = UUID.randomUUID();
//分開用uuID把數(shù)據(jù)分開
createFile(uuid+"myfile", "我的夢(mèng)說別停留等待,就讓光芒折射淚濕的瞳孔,映出心中最想擁有的彩虹,帶我奔向那片有你的天空,因?yàn)槟闶俏业膲?mèng) 我的夢(mèng)");
//總共的數(shù)據(jù)
createFile("myfile", "");
}
}