tags:

views:

134

answers:

2

Is it possible in java to create a file without extension

+10  A: 

You can try

File f;
f=new File("myfile");
if(!f.exists()){
  f.createNewFile();
}
astander
will this create file without having extension.
Suresh S
@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.
a_m0d
@Suresh S - it won't if you don't try it! :-)
Stephen C
@astander thanks i will try @a_mod,stephen C thank u too.
Suresh S
@Suresh probably worth noting that creating a file without an extension will do nothing different than one created with an exception.
AndyC
+1  A: 

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.

Chad Okere
thanks chad for the code
Suresh S