• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

need help with IO

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the bad formatting.
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...)
 
Mich Orou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
haha, thanks. that was easy. i'm just glad the i/o part worked after all.
 
Mich Orou
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!