File

来源:互联网 发布:java messagedigest 编辑:程序博客网 时间:2024/06/10 19:27

Now, I will tell you how to create a file or delete a file. Mabe it very easy for someone, but  it indeed difficult for most students who learn java not too long.

import java.io.File;import java.io.IOException;public class FileClass {/** * @author lushun * @param args * @throws IOException */public static void main(String[] args) throws IOException {// The following is to tell you how to create a file.String path = "e:/lushun/weidan/xiaoli";File f = new File(path, "c.txt");if (!f.exists()) {if (!f.getParentFile().exists()) {f.getParentFile().mkdirs();f.createNewFile();} else {f.createNewFile();}}// Calls the function to delete file 'lushun' and other files it// include.File f1 = new File("e:/lushun");// deleteFile(f1);// Returns an array of strings naming the files and directories in the// directory denoted by this abstract pathname.String[] s = f.getParentFile().list();for (int i = 0; i < s.length; i++)System.err.println(s[i]);}public static void deleteFile(File file) {// Delete the whole file including// all directory and normal// file.if (file.isFile()) {file.delete();} else if (file.isDirectory()) {File[] f = file.listFiles();for (int i = 0; i < f.length; i++) {deleteFile(f[i]);}}file.delete();}}

There have a result in the following picture. Do you know which sentence lead to this reslut? 





0 0
原创粉丝点击