packagecom.file.run;importjava.io.BufferedReader;importjava.io.File;importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.InputStreamReader;importjava.io.PrintWriter;importjava.util.UUID;/***@author夕橘子-O
*@version2016年7月8日 上午10:38:49*/
public classForFile {//生成文件路径
private static String path = "D:\\file\\";//文件路径+名称
private staticString filenameTemp;/*** 创建文件
*@paramfileName 文件名称
*@paramfilecontent 文件内容
*@return是否创建成功,成功则返回true*/
public static booleancreateFile(String fileName,String filecontent){
Boolean bool= false;
filenameTemp= path+fileName+".txt";//文件路径+名称+文件类型
File file = newFile(filenameTemp);try{//如果文件不存在,则创建新的文件
if(!file.exists()){
file.createNewFile();
bool= true;
System.out.println("success create file,the file is "+filenameTemp);//创建文件成功后,写入内容到文件里
writeFileContent(filenameTemp, filecontent);
}
}catch(Exception e) {
e.printStackTrace();
}returnbool;
}/*** 向文件中写入内容
*@paramfilepath 文件路径与名称
*@paramnewstr 写入的内容
*@return*@throwsIOException*/
public static boolean writeFileContent(String filepath,String newstr) throwsIOException{
Boolean bool= 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(filepath);//文件路径(包括文件名称)//将文件读入输入流
fis = newFileInputStream(file);
isr= newInputStreamReader(fis);
br= newBufferedReader(isr);
StringBuffer buffer= newStringBuffer();//文件原有内容
for(int i=0;(temp =br.readLine())!=null;i++){
buffer.append(temp);//行与行之间的分隔符 相当于“\n”
buffer = buffer.append(System.getProperty("line.separator"));
}
buffer.append(filein);
fos= newFileOutputStream(file);
pw= newPrintWriter(fos);
pw.write(buffer.toString().toCharArray());
pw.flush();
bool= true;
}catch(Exception e) {//TODO: handle exception
e.printStackTrace();
}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();
}
}returnbool;
}/*** 删除文件
*@paramfileName 文件名称
*@return
*/
public static booleandelFile(String fileName){
Boolean bool= false;
filenameTemp= path+fileName+".txt";
File file= newFile(filenameTemp);try{if(file.exists()){
file.delete();
bool= true;
}
}catch(Exception e) {//TODO: handle exception
}returnbool;
}public static voidmain(String[] args) {
UUID uuid=UUID.randomUUID();
createFile(uuid+"myfile", "我的梦说别停留等待,就让光芒折射泪湿的瞳孔,映出心中最想拥有的彩虹,带我奔向那片有你的天空,因为你是我的梦 我的梦");
}
}