This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes How to read from DOS pipe? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "How to read from DOS pipe?" Watch "How to read from DOS pipe?" New topic
Author

How to read from DOS pipe?

Roy Hellinga
Greenhorn

Joined: Jul 20, 2001
Posts: 1
I have a need to pipe the output from a DOS utility into my Java program.
I have tried a few combinations without success. I can't seem to find the right combination of DOS syntax and Java I/O that works.
Ideally, I would like to use the DOS pipe command (e.g. dir | more), but substitute my own Java application for 'more' ( dir | java Test1 :confused .
Where will the input show up (I thought it would be System.in, but that doesn't seem to be the case -- it only seems to read keyboard input)?
Thanks in advance,
Roy.
ben coleamn
Greenhorn

Joined: Apr 08, 2003
Posts: 1
this is how to do it in C or C++
its just reading charters from stdin i think 1 at a time
while ((c = getchar()) != EOF) {
/* read single char until EOF */
//if (c == fromch) c = toch;
/* convert matching characters */
putchar (c);
/* copy character to stdout */
}
hears one for qbasic i found PS ther called "filter" not pipe's it touc me ages to find this out
'===========================================================================
' Subject: MD-DOS STANDARD OUTPUT FILTER Date: 01-08-97 (15:27)
' Author: Tyler Barnes Code: QB, PDS
' Origin: comp.lang.basic.misc Packet: DOS.ABC
'===========================================================================
'Hey everyone. Here's one last version of the filter program I made. (I
'hope I didn't post this version already)
'You will notice that I have two different filter "methods" ... The
'"EASY" method worked in my old version, but the "COMPLICATED" version
'did not because I didn't have any of the file handling SUBs created. I
'expected you could just use BASIC's file handling routines.
'Unfortunatly, BASIC's routines mess with the MSDOS cursor so I had to
'make my own custom file-handling routines to handle the second filter
'method. Here it is:
DECLARE FUNCTION REPLACE$ (T$, S$, R$)
DECLARE SUB StandardOutput (ST$)
DECLARE FUNCTION ReadFile$ (Bytes%)
DECLARE FUNCTION FileExist% (T$)
DECLARE FUNCTION StandardInput$ ()
DECLARE SUB OpenFile (File$, Mode$)
DECLARE SUB WriteFile (DTW$)
DECLARE SUB CloseFile ()
'$INCLUDE: '\basic\bc\qb.bi'
DIM SHARED InregX AS RegTypeX, OutregX AS RegTypeX
' __________________________________________________________________________
'| MS-DOS Standard Output Filter |
'| By D. Tyler Barnes |
'| |
'| This program allows you to create a standard output filter such |
'| as MORE.COM and SORT.EXE, two programs that come with MS-DOS. It allows |
'| you to pipe information to your program from the command line and modify |
'| the information before sending it to standard output. |
'| |
'| Note: This version of FILTER.BAS has custom file handling routines |
'| because BASIC's inbuilt file handling routines mess with the DOS |
'| cursor. That was messing things up pretty badly in the previous |
'| version. |
'| |
'| Example Usage: DIR /w |Filter |
'| Example #2: Filter <FILENAME.EXT |
'| |
'| Send comments to: Tyler.Barnes@access.cn.camriv.bc.ca |
'| |
'| TWO WANNA-BE-HACKER FILTERS PROVIDED! |
' ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CONST RunMethod = "complicated"

SELECT CASE UCASE$(RunMethod)
CASE "COMPLICATED"
'----------------------------------------------------------------------------
'****************************************************************************
'Complicated method. Study this if you're wanting to make a filter that has
'to recieve data more than one byte at a time
'****************************************************************************
TempName% = 0
NewName:
TempName$ = LTRIM$(STR$(TempName%))
IF FileExist(TempName$) THEN TempName% = TempName% + 1: GOTO NewName
OpenFile TempName$, "O"
DO
in$ = StandardInput$
WriteFile in$
LOOP UNTIL in$ = CHR$(0)
CloseFile
OpenFile TempName$, "I"
DO
in$ = ""
DO
A$ = ReadFile$(1)
in$ = in$ + A$
LOOP UNTIL A$ = CHR$(0) OR A$ = CHR$(13)
'~~~~~~~~~~~~ Data is processed here ~~~~~~~~~~~~
in$ = REPLACE$(in$, "MORON", "LAMER")
in$ = REPLACE$(in$, "IDIOT", "LAMER")
in$ = REPLACE$(in$, "GEEK", "LAMER")
in$ = REPLACE$(in$, "JERK", "LAMER")
in$ = REPLACE$(in$, "NERD", "LAMER")
in$ = REPLACE$(in$, "LOSER", "LAMER")
in$ = REPLACE$(in$, "THAT", "DAT")
in$ = REPLACE$(in$, "THIS", "DIS")
in$ = REPLACE$(in$, "THING", "THANG")
in$ = REPLACE$(in$, "LOVE", "LUV")
in$ = REPLACE$(in$, "BITES", "BYTES")
in$ = REPLACE$(in$, "YOUR", "YER")
in$ = REPLACE$(in$, "YOU'RE", "YER")
in$ = REPLACE$(in$, "GREETING", "GREET")
in$ = REPLACE$(in$, "CONGRATULATIONS", "CONGRATZ")
in$ = REPLACE$(in$, "CKS", "X")
in$ = REPLACE$(in$, " C", " K")
in$ = REPLACE$(in$, " F", " PH")
in$ = REPLACE$(in$, "S ", "Z ")
in$ = REPLACE$(in$, "S" + CHR$(13), "Z" + CHR$(13))
in$ = REPLACE$(in$, "AU", "AW")
in$ = UCASE$(in$)
in$ = REPLACE$(in$, "A", "a")
in$ = REPLACE$(in$, "E", "e")
in$ = REPLACE$(in$, "I", "i")
in$ = REPLACE$(in$, "O", "o")
in$ = REPLACE$(in$, "O", "0")
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StandardOutput in$
LOOP UNTIL A$ = CHR$(0)
CloseFile
KILL TempName$
'----------------------------------------------------------------------------
CASE "EASY"
'****************************************************************************
'Easy method. Use this if recieving data 1 byte at a time is A-ok!
'****************************************************************************
DO
in$ = StandardInput$
'~~~~~~~~~~~~ Data is processed here ~~~~~~~~~~~~
in$ = UCASE$(in$)
SELECT CASE in$
CASE "A", "E", "I", "U": in$ = LCASE$(in$)
CASE "S": in$ = "Z"
CASE "O": in$ = "0"
CASE "F": in$ = "PH"
END SELECT
'~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
StandardOutput in$
LOOP UNTIL in$ = CHR$(0)
'----------------------------------------------------------------------------
END SELECT
SUB CloseFile
SHARED FileNum%
CLOSE FileNum%
END SUB
FUNCTION FileExist% (T$)
'Just for the record, I didn't write this function. Not to say I COULDN'T.
DEFINT A-Z
DIM F AS STRING * 64
DIM Inx AS RegTypeX
DIM Outx AS RegTypeX
Inx.ax = &H2F00
CALL INTERRUPTX(&H21, Inx, Outx)
DTAAddr = Outx.bx
F$ = LTRIM$(RTRIM$(UCASE$(T$))) + CHR$(0)
Inx.ds = VARSEG(F$)
Inx.dx = VARPTR(F$)
Inx.ax = &H4E00
Inx.cx = -1
CALL INTERRUPTX(&H21, Inx, Outx)
IF Outx.flags AND 1 THEN
FileExist% = 0
ELSE
FileExist% = 1
END IF
END FUNCTION
DEFSNG A-Z
SUB OpenFile (File$, Mode$)
SHARED FileNum%, DOSFileNum%
Mode$ = UCASE$(Mode$)
FileNum% = FREEFILE
SELECT CASE Mode$
CASE "I": OPEN File$ FOR INPUT AS FileNum%
CASE "O": OPEN File$ FOR OUTPUT AS FileNum%
CASE ELSE: ERROR (5)
END SELECT
DOSFileNum% = FILEATTR(1, 2)
END SUB
FUNCTION ReadFile$ (Bytes%)
SHARED DOSFileNum%
RecieveBuffer$ = STRING$(Bytes%, 0)
InregX.ax = &H3F00 'Read from device/file subfunction
InregX.bx = DOSFileNum% 'File handle
InregX.cx = Bytes% 'Characters to read
InregX.dx = SADD(RecieveBuffer$) 'Point DX to offset of recieve buffer
InregX.ds = VARSEG(RecieveBuffer$) 'Point DS to segment of recieve buffer
CALL INTERRUPTX(&H21, InregX, OutregX) 'Read in to recieve buffer
IF OutregX.ax = 0 THEN RecieveBuffer$ = CHR$(0)'If EOF return a null!
ReadFile$ = RecieveBuffer$
END FUNCTION
FUNCTION REPLACE$ (T$, S$, R$)
DEFINT A-Z
W = 1
DO UNTIL INSTR(W, UCASE$(T$), UCASE$(S$)) = 0
Q = INSTR(W, UCASE$(T$), UCASE$(S$))
T$ = LEFT$(T$, Q - 1) + R$ + RIGHT$(T$, LEN(T$) - (Q + LEN(S$) - 1))
W = Q + LEN(R$)
LOOP
REPLACE$ = T$
END FUNCTION
DEFSNG A-Z
FUNCTION StandardInput$
SHARED DOSFileNum%
Temp% = DOSFileNum%
DOSFileNum% = 0
StandardInput$ = ReadFile$(1)
DOSFileNum% = Temp%
END FUNCTION
SUB StandardOutput (ST$)
SHARED DOSFileNum%
Temp% = DOSFileNum%
DOSFileNum% = 1
WriteFile ST$
DOSFileNum% = Temp%
END SUB
SUB WriteFile (DTW$)
SHARED DOSFileNum%
InregX.ax = &H4000 'Read from device/file subfunction
InregX.bx = DOSFileNum% 'File handle
InregX.cx = LEN(DTW$) 'Characters to write
InregX.dx = SADD(DTW$) 'Point DX to offset of string
InregX.ds = VARSEG(DTW$) 'Point DS to segment of string
CALL INTERRUPTX(&H21, InregX, InregX) 'Write chars
END SUB
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16093
    
  21

Trust me. System.in can be used to read from DOS stdin to a java app. I've been doing it forever.

Simple System.in.readLine calls can retrieve lines output by the DIR program this way.
Of course, this [I]is[/B] assuming you're using an application and not an applet (where the stdout is usually a debugging console and you may not even have a stdin).


Customer surveys are for companies who didn't pay proper attention to begin with.
Ravi Anamalay
Ranch Hand

Joined: Apr 15, 2003
Posts: 38
Hi Roy,
I'm still new to Java but I've been able to pipe output from a dos program to a java program (in the console). Here's a snippet of the code I used when learning to do this:
// Example of pipe in and output
import java.io.*;
class Pipe {
public static void main (String args []) throws IOException {
BufferedReader S= new BufferedReader(new InputStreamReader(System.in));
String s;
do {
s = S.readLine();
if ( s != null)
System.out.println(s);
} while ( s != null );

}

}

From the dos prompt, you use :
dir | java Pipe


with kind regards,<br />Ravi
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to read from DOS pipe?