Is it possible in java to create a file without extension
2 Answers
You can try
File f;
f=new File("myfile");
if(!f.exists()){
f.createNewFile();
}
4 Comments
Suresh S
will this create file without having extension.
a_m0d
@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.
Stephen C
@Suresh S - it won't if you don't try it! :-)
AndrewC
@Suresh probably worth noting that creating a file without an extension will do nothing different than one created with an exception.
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.