package fourth;
import java.io.*;
public class fileWork {
private static String path = "E:\\file\\file1\\file2\\";
private static String filenameTemp;
public static boolean creatFile(String fileName,String fileContent){
boolean bool = false;
filenameTemp=path+fileName+".txt";
File file = new File(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.fillInStackTrace();
}
return bool;
}
public static boolean writeFileContent(String filepath,String newstr)throws IOException{
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 = new FileInputStream(file);
isr = new InputStreamReader(fis);
br = new BufferedReader(isr);
StringBuffer buffer = new StringBuffer();
for(int i = 0;(temp = br.readLine())!=null;i++){
buffer.append(temp);
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) {
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();
}
}
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) {
}
return bool;
}
/**
* 复制文件
* @param fileName 文件名称
* @return
*/
private static void copyFile(File soure,File dest)throws IOException{
InputStream input = null;
OutputStream output = null;
try{
input = new FileInputStream(soure);
output = new FileOutputStream(dest);
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf))>0){
output.write(buf, 0, bytesRead);
}
}finally{
input.close();
output.close();
}
}
/**
* 查找文件
* @param fileName 文件名称
* @return
*/
public static void findFile(File file, String extName){
if(file == null){
return;
}
if(file.isDirectory()){
File [] fs = file.listFiles();
if(fs!=null){
for(File s : fs){
findFile(s,extName);
}
}
}else{
String path = file.getPath().toLowerCase();
if(path.endsWith(extName)){
System.out.println(file.getPath());
}
}
}
public static void main(String args[]) throws InterruptedException,IOException{
fileWork a = new fileWork();
a.creatFile("file1","创建了一个文件,写入的内容");
File source = new File(filenameTemp);
File dest = new File("C:\\Users\\54541\\Desktop\\file3.txt");
copyFile(source,dest);
File file = new File("E:\\file\\");
String extName = ".doc";
findFile(file,extName);
}
}
程序结果:
success create file,the file isE:\file\file1\file2\file1.txt
E:\file\file1\file2\文件查找.doc