您的位置:首页 > 编程语言 > Java开发

Java文件创建删除复制读写以及查找盘符文件操作

2017-10-25 22:30 721 查看
package fourth;

import java.io.*;  //文件操作导入

/*@author ZZ
*@version 2017/10/24
* 任务:指定一个盘符下的文件,把该文件复制到指定文件啊下
*
* */
public class fileWork {//类名的创建不能和file重复
//生成文件路径:
private static String path = "E:\\file\\file1\\file2\\";

//文件路径+名称
private static String filenameTemp;

/*
* 创建文件
* @param fileName 文件名称
* @param filecontent 文件内容
* @return 是否创建成功,成功则返回true
* */
//创建文件方法;

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);
//行与行之间的分隔符相当于‘\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 {
//不要忘记关闭
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;
}
/**
* 复制文件
* @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
4000
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);

//查找指定目录下的.doc文件
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java