File

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? 





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值