Java中关于File类的一些简单应用!

来源:互联网 发布:福建网络干部学院 编辑:程序博客网 时间:2024/05/04 20:47

File(文件)类   (FileOperate

File类直接继承OBject类,实现Comparable接口,Comparable接口的作用是强行对实现它的类的每一个对象进行整体排序,把这种排序成为自然排序,排序的方法comparaTo成为它的自然排序方法。

在整个IO包中只有Flie类与文件的本身有关,即对文件的“新建”、“删除”、“重命名”、“得到路径”、“创建时间”等(在编写IO流的时候所有的分隔符都要用separator进行指定)。

1:创建文件和文件夹

package org .iodemo.filedemo; 

import java.io.File;  

import java.io.IOException; 

public class FileDemo01 { 

  public static void main(String[] args) { 

    File file = new  File("d: \\test.txt"); //windows下用”\”,linux下用”/”

    try  { 

      file.createNewFile(); //  创建文件 

    } catch (IOException e) { 

      e.printStackTrace(); 

    } 

  } 

}

public void newFolder(String folderPath) {

try {

String filePath = folderPath;

filePath = filePath.toString();

File myFilePath = new File(filePath);

if (!myFilePath.exists()) {//判断文件夹是否已经存在

myFilePath.mkdir();//新建文件夹

System.out.println("新建目录操作 成功执行");

} else {

System.out.println(filePath + "已存在,无需再建");

}

} catch (Exception e) {

System.out.println("新建目录操作出错");

e.printStackTrace();

}

}

2:删除文件和文件夹

//删除文件可以在拿到文件的名字之后直接delete()删除,但是这种删除方法会造成延迟,所以在delete()之前应该利用exists()进行一个判断

package org .iodemo.filedemo; 

import java.io.File;  

public class FileDemo04 { 

  public static void main(String[] args) { 

    File file = new  File("d:" + File.separator + "test.txt"); 

    if (file.exists()) {  //  文件是否存在 

      file.delete();//  删除文件 

    } 

  } 

}

public void delFolder(String folderPath) {

try {

String filePath = folderPath;

filePath = filePath.toString();

File myFilePath = new File(filePath);

if (myFilePath.delete()) { // 删除文件夹,在删除文件夹之前可先将文件夹里面的所有文件先行进行删除

System.out.println("删除文件夹" + folderPath + "操作 成功执行");

} else {

System.out.println("删除文件夹" + folderPath + "操作 执行失败");

}

} catch (Exception e) {

System.out.println("删除文件夹操作出错");

e.printStackTrace();

}

}

3:判断File对象是文件还是文件夹,并输出它的长度和名字

package org .iodemo.filedemo; 

import java.io.File;  

public class FileDemo06 { 

  public static void main(String[] args) { 

    File file1 =  new  File("d:" + File.separator + "test.txt");  //  文件路径 

File file2 =  new  File("d:");  //  文件夹路径 

if(file1.isFile()){//判断对象是不是文件

 System.out .println(“大家好,我是文件”); 

}else if(file2.isDirectory()){//判断对象是不是文件夹

    System.out .println(“大家好,我是文件夹”); 

}

    System.out .println("文件大小:" + file1.length());  

    System.out .println("文件路径:" + file1.getPath()); 

System.out .println("文件路径:" + file1); 

String str[] = file2.list();  //  列出目录内容 

for  (int  x = 0; x < str.length; x++) {  

  System.out .println(str[x]); 

  } 

}  

4:删除文件夹里的所有内容

public void delAllFile(String path) {

File file = new File(path);

if (!file.exists()) {

return;

}

if (!file.isDirectory()) {

return;

}

String[] tempList = file.list();

File temp = null;

for (int i = 0; i < tempList.length; i++) {

if (path.endsWith(File.separator)) {//判断path是否是以\结束的

temp = new File(path + tempList[i]);

} else {

temp = new File(path + File.separator + tempList[i]);

}

if (temp.isFile()) {

temp.delete();

}

if (temp.isDirectory()) {

delAllFile(path+"/"+ tempList[i]);//先删除文件夹里面的文件

                String filepath = path;

File file1 = new File(path+File.separatorChar+tempList[i]);

file1.delete();// 再删除空文件夹

}

}

}

5:复制文件

public void copyFile(String oldPath, String newPath) {

try {

int bytesum = 0;

int byteread = 0;

File oldfile = new File(oldPath);

if (oldfile.exists()) {

// 文件存在时

InputString inStream = new FileInputStream(oldPath); // 读入原文件

FileOutputStream fs = new FileOutputStream(newPath);

byte[] buffer = new byte[1444];

while ((byteread = inStream.read(buffer)) != -1) {

bytesum += byteread; // 字节数 文件大小

System.out.println(bytesum);

fs.write(buffer, 0, byteread);

fs.close();

}

} else {

System.out.println(oldPath + " 文件不存在");

}

} catch (Exception e) {

System.out.println("复制单个文件操作出错");

e.printStackTrace();

} finally {

try {

inStream.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

6:复制文件夹里的所有内容

public void copyFolder(String oldPath, String newPath) {

try {

(new File(newPath)).mkdirs(); // 如果文件夹不存在 则建立新文件夹

File a = new File(oldPath);

String[] file = a.list();

File temp = null;

for (int i = 0; i < file.length; i++) {

if (oldPath.endsWith(File.separator)) {

temp = new File(oldPath + file[i]);

} else {

temp = new File(oldPath + File.separator + file[i]);

}

if (temp.isFile()) {

FileInputString input = new FileInputStream(temp);

FileOutputString output = new FileOutputStream(newPath + "/"+ (temp.getName()).toString());

byte[] b = new byte[1024 * 5];

int len;

while ((len = input.read(b)) != -1) {

output.write(b, 0, len);

}

output.flush();

}

if (temp.isDirectory()) {

// 如果是子文件夹

copyFolder(oldPath + "/" + file[i], newPath + "/" + file[i]);

}

}

System.out.println("复制文件夹操作 成功执行");

} catch (Exception e) {

System.out.println("复制整个文件夹内容操作出错");

e.printStackTrace();

} finally {

try {

output.close();

input.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

原创粉丝点击