• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 159
  • Last Modified:

FileReader

How can I do smth like......try to open a file, and if the file does not exist, create one with the following line:

admin:admin

?

Apparently, I can only try to open the file but it reports exception if the file doesnt exist.
0
jtcy
Asked:
jtcy
  • 2
1 Solution
 
sudhakar_koundinyaCommented:
File  createFile(String str)
{
           File file=new File(str);
            if(file.exists() )
           {
                      return file;
             }
            else
           {
                      FileOutputStream fout=new FileOutputStream(file);
                      fout.write("admin:admin".getBytes());
                       fout.close();
                       return new File(str);
            }
           
}
0
 
sudhakar_koundinyaCommented:
you can call like this

createFile("admin.txt");


if admin.txt dows not exists it creates and dumps "admin:admin" into the file
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now