---
title: "java关于文件操作的基本知识Demo"
author: "tonyzzp"
date: "2009-12-06T16:10:00Z"
tags: []
slug: "java-demo"
draft: false
lastmod: "2009-12-06T16:10:00Z"
id: 978052
url: "https://streamcave.iteye.com/blog/978052"
---

```
import java.io.*;
import java.util.*;
import java.io.File;
public class file {
	public static void main (String[] args) {
		String path="C:\\Users\\Administrator\\Desktop";
		File desktop=new File(path);
		System.out.println(desktop.getName()+"是目录："+desktop.isDirectory());
		File test=new File(desktop,"test");
		System.out.println(test.getName()+"是目录："+test.isDirectory());
		System.out.println(test.getName()+"是文件："+test.isFile());
		System.out.println("test.path:"+test.getAbsolutePath());
		try{test.mkdir();}catch(Exception e){System.out.println(e.toString());}
		System.out.println(test.getName()+"是目录："+test.isDirectory());
		System.out.println(test.getName()+"是文件："+test.isFile());
		System.out.println("test.path:"+test.getAbsolutePath());
		for(int i=0;i<desktop.list><pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">			System.out.println(desktop.list()[i]);
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		}
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		File text=<span style="color: #0000ff">new</span> File(test,"<span style="color: #8b0000">text.txt</span>");
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		<span style="color: #0000ff">try</span>{text.createNewFile();}<span style="color: #0000ff">catch</span>(Exception e){System.out.println(e.toString());}
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		System.out.println(text.getAbsolutePath());
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		System.out.println(text.getName()+"<span style="color: #8b0000">是目录：</span>"+text.isDirectory());
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">		System.out.println(text.getName()+"<span style="color: #8b0000">是文件：</span>"+text.isFile());
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">	}
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,'Courier New',courier,monospace; font-size: 12px">}</pre></desktop.list>
```