`
tonyzzp
  • 浏览: 11926 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

java关于文件操作的基本知识Demo

    博客分类:
  • java
阅读更多
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>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics