文件

一.文件的建立
File file1 = new File ("C:\\temp\\myNote.txt"); // in Windows 这是windows文件系统下的方法
File file2 = new File ("/tmp/myNote.txt"); // in Linux/Unix    unix文件系统的方法
二.文件的统计
1.根据路径统计
  public int countFile1(String path){
int count=0;
File file=new File(path);
if(file.isFile()){
count++;
}else if(file.isDirectory()){
File[] listfile=file.listFiles();
if(null!=listfile){
for(int i=0;i<listfile.length;i++){
if(listfile[i].isDirectory()){
count+=countFile1(listfile[i].getAbsolutePath());
}else{
count++;
}
}

}
}
return count;
}
2.根据文件名统计
  public int countFile2(String filename){
int count=0;
//通过传入的名字构造File对象
File file=new File(filename);
if(file.exists()){
//System.out.println("这是一个目录");
    if(file.isFile()){
    System.out.println("这是一个目录");
count++;
//System.out.println("这是一个目录");
    }  else if(file.isDirectory()){
File [] listfile=file.listFiles();
if(null!=listfile){
for(int i=0;i<listfile.length;i++){
if(listfile[i].isDirectory()){
System.out.println("这是一个目录"+listfile[i].getAbsolutePath());
count+=countFile2(listfile[i].getName());
} else {
count++;
}
}

}
}
}


return count;
}
三.文件的删除
public void testCreateAndDelete(){

//通过传入的名字构造File对象
File temFile=new File(fileName);
//判断文件是否存在
if(temFile.exists()){
//如果是一个目录
if(temFile.isDirectory()){
System.out.println("这是一个目录"+temFile.getAbsolutePath());
}
//如果是文件
if(temFile.isFile()){
//打印出文件的长度
prinFileAttr(temFile);
//删除文件
temFile.delete();
String theName=temFile.getName();
String absPath=temFile.getAbsolutePath();
System.out.println("文件已删除,名字是"+theName+"绝对路径是"+absPath);

}else {
try{
temFile.createNewFile();
System.out.println("文件已创建"+temFile.getAbsolutePath());
//打印文件长度
prinFileAttr(temFile);

}  catch(Exception ef){
System.out.println("创建文件失败");
}
}
}

}
四.练习
  文件搜索器
核心代码
public void actionPerformed(ActionEvent e) {
   content = tf.getText();
root = (String) cb.getSelectedItem();
// System.out.println(cb.getSelectedItem());

if (e.getActionCommand().equals("Search")) {
// System.out.println("I have press the button");
// 选择目录时
if (cb.getSelectedItem().equals("选择目录")) {
// 绘制对话框
JFileChooser chooser = new JFileChooser();
int option = chooser.showDialog(null, "选择");
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
//int option = JFileChooser.showOpenDialog(cb);
//NC:要添加点击取消按钮的动作事项
if(option == JFileChooser.CANCEL_OPTION){
return;
}
//int state = chooser.s
chooser.setControlButtonsAreShown(true);
// 获取输入
File file = chooser.getCurrentDirectory();
//System.out.println(file.getName());
// 路径,获取当前路径(磁盘)
String path = file.getAbsolutePath();
    //System.out.println("I am path " + path);
File file1 = new File(path); 
listFile = file1.listFiles();
//获取对话框中输入的搜索关键字,进行搜索
File file2 = chooser.getSelectedFile();
content = file2.getName();
//启动线程,开始搜索
item = "chooseDirectory";
s = "";
fl = new FileListener(tf,cb,ta);
//每一次重新搜索时都清空JTextArea
ta.setText("");
t = new Thread(fl);
t.start();

} else {
// 指定盘
if (root == null || root.equals("")) {
ta.append("该路径不存在!");
File file = new File("");
listFile = file.listRoots();
//for (int i = 0; i < listFile.length; i++) {
// ta.append(listFile[i].getName()+"\n");
// ta.append(listFile[i].getAbsolutePath()+"\n");
//System.out.println(listFile[i].getName() + "\n");
//System.out.println(listFile[i].getAbsolutePath() + "\n");
//}
} else {
if (content == null || content.length() == 0) {
//System.out.println("===================>");
File file = new File(root);
listFile = file.listFiles();
//启动线程
item = "search";
s = "";
fl = new FileListener(tf,cb,ta);
ta.setText("");
t = new Thread(fl);
t.start();

} else {
System.out.println("-------------------->"+root);
File file = new File(root);
listFile = file.listFiles();
//启动线程
item = "searchCount";
s = "";//每次线程启动前都要将s清空
fl = new FileListener(tf,cb,ta);
ta.setText("");
t = new Thread(fl);
t.start();
}
}

}
}

if (e.getActionCommand().equals("Stop")) {
s = "stop";//判断是否继续搜索
item = "";
if(t.isAlive()){
t.interrupt();
ta.append("you press Stop!"+"\n");
}
}
}

private int search(File[] file) {
//System.out.println("search is running!");
//每次使用之前都将count清0,否则会叠加
int count = 0;
if (file == null || file.length == 0) {
return 0;
}
for (int i = 0; i < file.length; i++) {
File filenew = file[i];
if (filenew.isDirectory()) {
File[] tempFile = filenew.listFiles();
count += search(tempFile);
this.count = count;
}
if(s.equals("stop")){
//s = "";清空s
break;
}else{
if (filenew.isFile()) {
count++;
ta.append("文件:" + filenew.getAbsolutePath() + "\n");

}
  }
}

return count;
}

private int searchContent(File[] file) {
//System.out.println("new count=======================");
int count = 0;
if (file == null || file.length == 0) {
return 0;
}
for (int i = 0; i < file.length; i++) {
File filenew = file[i];
// 包含字符
if (filenew.isDirectory()) {
File[] tempFile = filenew.listFiles();
count += searchContent(tempFile);
this.count = count;
}

if (filenew.isFile()) {
String path = filenew.getAbsolutePath();
//找到与内容相符的路径

if(s.equals("stop")){
//s = "";
break;
}else{
if (path.indexOf(content) != -1) {
count++;
ta.append("文件:" + filenew.getAbsolutePath() + "\n");
}
}
  }
}
return count;
}


//重写run方法
@Override
public void run() {
//System.out.println("I am item:"+item);
if(item.equals("chooseDirectory")){
   searchContent(listFile);
   ta.append("共找到文件" + count + "个" + "\n" + "\n" + "\n");
ta.append("\n" + "\n" + "\n");
}

if(item.equals("search")){
   //找寻root路径下的所有file
   search(listFile);
   ta.append("共找到文件" + count + "个" + "\n" + "\n" + "\n");
ta.append("\n" + "\n" + "\n");
}

if(item.equals("searchCount")){
   searchContent(listFile);
   ta.append("共找到文件" + count + "个" + "\n" + "\n" + "\n");
ta.append("\n" + "\n" + "\n");
}
}

}

你可能感兴趣的:(文件)