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()+"
");
		}
	}
	
	//Separator is applyed to different system. For example,windows,linux and so on.
	public static void ShowSeparator()
	{
		System.out.println("file:"+File.separator+"
"+"path:"+File.pathSeparator);
	}
}

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

  

原文地址:https://www.cnblogs.com/fang-beny/p/3289297.html