Create folders and files in Java

Source: Internet
Author: User
// Author: [email] huapingsmith@hotmail.com [/Email]
// Description: Check whether the folder exists. If the folder does not exist, create one. If yes, continue running.
// After the folder is processed, the next step is to create a file in the folder.
// Caution: When a file named abc.txt already exists in the prefix folder,
// Creating a file named abc.txt in front of the folder will fail.
// In this program, if the file E: // test is named 1, 2, 3, and 4, the program will fail to be executed.

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! ");
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.