0

How to distinguish between a named pipe and an ordinary file (for example a text file) in Java (under Windows as well as under Linux)?

In my case the user gives an input parameter (input filename/pipename from which I intend to read) and I should be able to distinguish whether the input parameter is a file or a named pipe.

xenteros
  • 15,586
  • 12
  • 56
  • 91
Sunil
  • 1
  • 1
  • **if (something instanceof someClass)** – Rabbit Guy Jun 29 '16 at 14:25
  • 1
    Could you please elaborate? – Sunil Jun 29 '16 at 14:32
  • Please see here: http://www.java2s.com/Tutorial/Java/0060__Operators/TheinstanceofKeyword.htm – Rabbit Guy Jun 29 '16 at 14:33
  • Thank you, but I dont see how this helps my case. – Sunil Jun 29 '16 at 14:38
  • @blahfunk A "named pipe" is not a "special" class in Java: https://en.wikipedia.org/wiki/Named_pipe, so your suggestion is not usable here. – Tom Jun 29 '16 at 14:47
  • *"I should be able to distinguish whether the input parameter is a file or a named pipe"* And why? One of the features of the named pipe is, that you don't have to care, that this isn't a "real" file. – Tom Jun 29 '16 at 14:48
  • 1
    If I am able to tell whether the input parameter is a file, then I read the entire contents from the file in one go. If its a pipe, I will be in a loop reading whatever is written from the other end until an exit criteria is sent to me from the other end of the pipe. – Sunil Jun 29 '16 at 14:54
  • Then let the user tell you (using a second input parameter), if it is a file or a pipe. – Tom Jun 29 '16 at 14:58
  • That solution doesn't work for me. I just want to know whether it can be done at all in JAVA?? (Distinguish between a named pipe and an ordinary file). If yes how, if no I want to be sure it can't be done at all. – Sunil Jun 29 '16 at 15:02
  • There is no available API to achieve this directly. But 'File' has an api to check whether that file exists of not. File f = new File(filePathString); f.exists(); which will return true if file exists. I think you can use this logic to check whether the input given is a file or not. Hope this helps. – Prasad Jun 29 '16 at 15:07
  • Thank you, but f.exists() returns true in both cases, in case of a valid filename and in case of a valid pipename. – Sunil Jun 29 '16 at 15:21
  • In Windows the pipe name [has the format](https://msdn.microsoft.com/en-us/library/aa365783(v=vs.85).aspx) `\\ServerName\pipe\PipeName` (f.ex. `\\.\pipe\MyPipe`) so this unequivocally identifies a named pipe. – SantiBailors Sep 08 '16 at 19:01

0 Answers0