-3

I need to check if a file exists without allocating memory for it. I just want to check that it exists, I am not interested in getting the content. Is there a simple way to achieve that?

Jesus Paradinas
  • 189
  • 2
  • 12

1 Answers1

2

The java.io.File.exists() method allows you to do just that:

boolean fileExists = new File("path").exists();

Creating a File instance does not read its content on the disk.

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • I forgot to mention that the file is on a remote server. How would look the code in that case? Can I put the remote host in the path string? – Jesus Paradinas May 21 '18 at 18:46
  • 1
    @JesusParadinas Unless the remote server is mapped to a local drive, the File API is pretty much not going to help. What protocol are you using to connect to the remote server? – ernest_k May 21 '18 at 18:52
  • I have been told that the servers are mapped. I am not sure how to connect do you have any suggestion? – Jesus Paradinas May 21 '18 at 19:48
  • 1
    @JesusParadinas Then I suppose the code should work. Try it. – ernest_k May 22 '18 at 04:49