1

I tried creating a file with empty title or just a space and it worked but I cannot find the file in the directory. I could not even delete or rename file.

File dir1 = new File("dir1");
dir1.mkdir();
File file1 = new File(dir1, "");
file1.createNewFile();

System.out.println(file1.exists()); always returns true even if I execute file1.delete() or file1.renameTo(...)

Mercenary
  • 2,106
  • 6
  • 34
  • 43
  • 4
    *"I tried creating a file with empty title"* Why?!? – Andrew Thompson Jul 22 '13 at 10:40
  • You need to give absolute path here new File("C:\\dir\\file.txt"); or new file("/usr/loc/file.txt"); – RaceBase Jul 22 '13 at 10:40
  • A file must have a non-empty name. I'm assuming that in your case `file1` is referring to your directory `dir1`. – David Pärsson Jul 22 '13 at 10:41
  • I've found some previous questions on Stack Overflow: Show [this][1] [1]: http://stackoverflow.com/questions/3024002/how-to-create-a-folder-in-java – Joe Taras Jul 22 '13 at 10:44
  • 3
    @JoeTaras That question is irrelevant to this one. – Nima Jul 22 '13 at 10:58
  • It is impossible to create a file without name, even space is forbidden for that, you must use any character, you can not use / or < in the name of the file. File1 appears because you search if exists anyfile without name and extension. Really it dosent created. – Distopic Jul 22 '13 at 11:02
  • @Nima: I linked that question, about creation of folder (first part of this question) – Joe Taras Jul 22 '13 at 11:02
  • @AndrewThompson I tried this out just to see if it works (might be helpful for interviews/exams too :-)). – Mercenary Jul 22 '13 at 11:16
  • @Deckard27 I actually don't see any compilation error while running this piece of code. It says file exists. Just need to know if that file is actually present and if you can access it or not? – Mercenary Jul 22 '13 at 11:17
  • *"might be helpful for interviews/exams too"* I very much doubt it. – Andrew Thompson Jul 22 '13 at 11:21
  • 1
    The "dired" mode in Emacs is very handy for deleting files with impossible names. Note that much of the discussion above is incorrect: certainly most Unix systems do, indeed, let you create files with names like this. – Ernest Friedman-Hill Jul 22 '13 at 11:21
  • Thanks @ErnestFriedman-Hill. I'm trying this on Windows and it lets me create files with empty string names. – Mercenary Jul 22 '13 at 11:23
  • Enerst but in window yo can not to create a file without name – Distopic Jul 22 '13 at 11:33

1 Answers1

3

Since you are passing an empty string for the child, Then the file1 will be the same as dir1, anything you're doing to file1 is actually happening to the directory you just created.

Here is a sample:

public static void main(String[] args) {

File directory = new File("/home/test");
directory.mkdir();

File file = new File(directory, "");

System.out.println(file.exists());
System.out.println(file.getAbsolutePath());
System.out.println(directory.getAbsolutePath());

file.delete();
System.out.println(directory.exists());
System.out.println(file.exists());  
}

output:

true
/home/test
/home/test
false
false
Nima
  • 6,383
  • 7
  • 46
  • 68
  • Thanks for the pointer for AbsolutePath. But, when you do a `file.delete()`, it still does not delete the file or the directory. `..exists()` still gives true. Please check – Mercenary Jul 22 '13 at 11:21
  • Strange, on my machine with jdk 7 it does delete the folder and return false afterwards.. – Nima Jul 22 '13 at 11:25
  • 1
    Depends perhaps of the Os that you are using, i think that in unix is possible, but not in window. – Distopic Jul 22 '13 at 11:34
  • Hmmm. I was using just jre. I installed jdk and this supposedly worked. Any ideas? – Mercenary Jul 22 '13 at 11:48
  • I think it used eclipse's own compiler which would not have supported this. – Mercenary Jul 22 '13 at 11:50