java对IO的操作

import java.io.*;

public class HelloWorld {
	
	//Main method.
	public static void main(String[] args) {
		CreateFile();
		ListFileName();
		ShowSeparator();
	}

	//Create a file.
	//Judge that the file exists.
	public static void CreateFile() {
		File f = new File("d:\\hello.xt");
		try {
			if (!f.exists()) {
				f.createNewFile();
				System.out.println("File created successfully.");
			} else {
				f.delete();
				f.createNewFile();
				System.out.println("File exists! File is created again.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//Get file name of all for loop.
	public static void ListFileName()
	{
		String _fileName="D:"+File.separator;
		File _fileObj=new File(_fileName);
		File[] _fileList=_fileObj.listFiles();
		for(File _file : _fileList)
		{
		  System.out.println(_file.getName()+"\n");
		}
	}
	
	//Separator is applyed to different system. For example,windows,linux and so on.
	public static void ShowSeparator()
	{
		System.out.println("file:"+File.separator+"\n"+"path:"+File.pathSeparator);
	}
}

本文由fang_beny原创,转载请注明出处。

  

posted @ 2013-08-29 15:47  fang_beny  阅读(172)  评论(0编辑  收藏  举报