2

Is it possible in java to create a file without extension

1
  • 5
    Its no different then creating one with an extension. Commented Mar 10, 2010 at 7:31

2 Answers 2

10

You can try

File f;
f=new File("myfile");
if(!f.exists()){
  f.createNewFile();
}
Sign up to request clarification or add additional context in comments.

4 Comments

will this create file without having extension.
@SureshS Yes it will. The file created will have exactly the name in quotes - if you add an extension, it will have an extension on disk, otherwise it won't. There is no default extension for the files you create in this manner.
@Suresh S - it won't if you don't try it! :-)
@Suresh probably worth noting that creating a file without an extension will do nothing different than one created with an exception.
1

Yes, it's easy. No different then doing it with an extension. So for example, if you have a string you want to write to a file, you can do

FileOutputStream out = new FileOutputStream("noExtension");
PrintStream printout = new PrintStream(out);
printout.println("Hello world!");
printout.close();

You could skip the PrintStream and do out.write("Hello world!".getBytes()); if you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.