jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes need help with IO Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "need help with IO" Watch "need help with IO" New topic
Author

need help with IO

Mich Orou
Greenhorn

Joined: Jul 17, 2003
Posts: 6
Hi, I created a little java program under windows 2000 that's supposed to run a dir command and return the output to standard output. Unfortunately, the actual output doesn't look right, can someone help me out? Here's the code:
import java.io.*;
public class SysTest {
public static void main (String args[]) {
String cmd = "command.com /c dir /w";
try {
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
String thestring = null;
while ( (thestring = in.readLine()) != null ) {
System.out.println(thestring);
}
}
catch (Exception e) {
System.out.println("caught exception");
}
}
------
here's how the output should look like:
[.] [..] appletviewer.exe dt_shmem.dll
dt_socket.dll extcheck.exe HtmlConverter.bat idlj.exe
jar.exe jarsigner.exe java.exe javac.exe
javadoc.exe javah.exe javap.exe javaw.exe
jdb.exe jdwp.dll keytool.exe ListC.class
ListC.java native2ascii.exe oldjava.exe oldjavac.exe
oldjavaw.exe oldjdb.exe policytool.exe rmic.exe
rmid.exe rmiregistry.exe serialver.exe SysTest.class
SysTest.java SysTest.java~ tnameserv.exe unregbean.exe
here's the actual output:
[.] ☺☺☺☺☺☺☺☺☺☺ [..] ☺☺☺☺☺☺☺☺☺ APPLET~1.EXE DT_SHMEM.DLL DT_SOC~1.DLL
EXTCHECK.EXE HTMLCO~1.BAT IDLJ.EXE ☺☺☺ JAR.EXE ☺☺☺☺ JARSIG~1.EXE
JAVA.EXE ☺☺☺ JAVAC.EXE ☺☺ JAVADOC.EXE JAVAH.EXE ☺☺ JAVAP.EXE ☺☺
JAVAW.EXE ☺☺ JDB.EXE ☺☺☺☺ JDWP.DLL ☺☺☺ KEYTOOL.EXE LISTC~1.CLA
LISTC~1.JAV NATIVE~1.EXE OLDJAVA.EXE OLDJAVAC.EXE OLDJAVAW.EXE
OLDJDB.EXE ☺ POLICY~1.EXE RMIC.EXE ☺☺☺ RMID.EXE ☺☺☺ RMIREG~1.EXE
SERIAL~1.EXE SYSTES~1.CLA SYSTES~1.JAV SYSTES~2.JAV TNAMES~1.EXE
UNREGB~1.EXE
Mich Orou
Greenhorn

Joined: Jul 17, 2003
Posts: 6
sorry for the bad formatting.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

Welcome to JavaRanch, Mich!
I can't speak for Win2k, but on my XP machine, command.com is not recognized. If I change command.com to cmd.exe, I get the following result:

BTW, if you press the "CODE" button on the message edit screen, a begin-code and an end-code tag will be placed in your message; just put you code or whatever you want to formate between the tags. (Or you can type the tags by hand, like I do... [ CODE ] and [ /CODE ], without the spaces...)


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Mich Orou
Greenhorn

Joined: Jul 17, 2003
Posts: 6
haha, thanks. that was easy. i'm just glad the i/o part worked after all.
Mich Orou
Greenhorn

Joined: Jul 17, 2003
Posts: 6
oh, one more thing. i also added a printwriter to the code to get the output to a file, but the file comes up blank! can someone explain?
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

You're not flughing the stream. After you're all done writing, do
pw.flush();

Or, alternatively, create your PrintWriter as:

That will auto-flush the stream (and gets rid of the BufferedWriter...).
Mich Orou
Greenhorn

Joined: Jul 17, 2003
Posts: 6
thanks. i don't even know what the BufferedWriter is there for. I just cut and pasted from somewhere. Honestly, all these different writers and readers and streams are confusing. it's so much simpler in c++, although i'm sure that once i get the hang of it, this will prove to be more flexible.
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1817

I agree that I/O is simpler in C++. When I first started in Java, I learned I/O and wrote a CFile class -- one that mimics a FILE structure from C. This is instantiated via calls like:
new CFile(fileName, "r");
new CFile(fileName, "w");
new CFile(fileName, "a");
It does all the other work so I don't have to remember it all (the .io package is one I still have to look up anytime I'm not doing file-based I/O).
Of course, it could be more flexible, and that class is slated for revision as soon as I can find the time....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need help with IO
 
Similar Threads
how to decode .class files to .java files?
java 1.4 and java 1.5
Running code
javac problem
java on command