// author: [email]huapingsmith@hotmail.com[/email]
// description: 程式先檢查檔案夾是否存在,如果不存在,則建立一個,存在,則繼續運行.
// 檔案夾處理完成後,下一步工作為往檔案夾中建立檔案.
// caution: 當前檔案夾中如果已經有名為abc.txt的檔案時,
// 在當前檔案夾建立一個名為abc.txt的檔案會失敗.
// 在這個程式中,如果E://test 下已經有名為1,2,3,4的檔案時,程式執行會失敗.
import java.io.*;
public class Demo
{
public static void main( String[] args)
{
File dirFile;
File tempFile;
boolean bFile;
String sFileName;
bFile = false;
try
{
dirFile = new File("E://test");
bFile = dirFile.exists();
if( bFile == true )
{
System.out.println("The folder exists.");
}
else
{
System.out.println("The folder do not exist,now trying to create a one...");
bFile = dirFile.mkdir();
if( bFile == true )
{
System.out.println("Create successfully!");
}
else
{
System.out.println("Disable to make the folder,please check the disk is full or not.");
System.exit(1);
}
}
System.out.println("Now we put files in to the folder...");
for(int i=0; i<5; i++)
{
sFileName = new String("E://test//");
sFileName += String.valueOf(i);
tempFile = new File(sFileName);
bFile = tempFile.createNewFile();
}
}catch(IOException e)
{
// Exception hadler
}
if( bFile == true )
System.out.println("Successfully put files in to the folder!");
else
System.out.println("Sorry sir,i don't finish the job!");
}
}