I have a software which outputs lines of numbers one by one. In another java program, I use BufferedReader as follows:
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream(),"US-ASCII"));
where pr is the process which runs the software. For each line in the output stream, I try to check whether the first number in the line is greater than a certain value and if it is, I write to another file.
The problem is, when I run the software repeatedly (in a linux cluster) like 10 simulations simultaneously, one of them have got @^@^.... characters followed by some huge numbers. So, I tried eliminating that line alone by using String.matches command but now, it seems even after that I get the error - so, I think it might mean that it is actually during the writing process that the problem occurs. I use BufferedWriter like the following to write it: (I do flush it after writing.)
I suggest to use UTF-8 instead of US-ASCII to make your program more robust. What if you ever reads in / writes out a file which contains "special" characters?
Further on agree with ejp: just use InputStream/OutputStream all the way, especially if you don't need to modify anything at character-level in Java.
Re: Java - InputStream - Weird Characters in the Output!
Oct 5, 2009 4:32 AM
(reply 5
of 14) (In reply to
#4 )
Hello! Thanks. It seems I still have the problem.
The simulator just outputs lines of numbers to the linux command line. To redirect the output, I use the >> operator as usual. But as I had to check for the first number in every line, I use the streamreader, read in line by line, split it, check and then write. So, there are no special characters actually. Its 100% numbers only!
Re: Java - InputStream - Weird Characters in the Output!
Oct 18, 2009 11:08 AM
(reply 7
of 14) (In reply to
#5 )
smuthiah wrote:
Hello! Thanks. It seems I still have the problem.
The simulator just outputs lines of numbers to the linux command line.
Which means at least the numbers are text.
To redirect the output, I use the >> operator as usual. But as I had to check for the first number in every line, I use the streamreader, read in line by line, split it, check and then write. So, there are no special characters actually. Its 100% numbers only!
Is there any other way I could do the same thing?
You need to first start by accurately identifying exactly what it is that the programs output. The fact that you see numbers doesn't mean that that is all that is being produced.
But presuming that all that is output is numbers, spaces and end of line characters then you problem is originating somewhere else in your code besides the readers themselves.
Re: Java - InputStream - Weird Characters in the Output!
Oct 18, 2009 2:41 PM
(reply 8
of 14) (In reply to
#7 )
Hi jschell! Thanks!
On your advice, I looked at the source code of the software; it was doing this: System.out.print(output), where output is a StringBuffer with an array of integers appended. Do you think this could have caused the problem?
I have now changed it into System.out.print(output.toString()).
//call the class file with the input xml file to simulate
public class RunSim {
public static void main(String[] args) {
String[] xmlFiles = new String[] { "/home/senthil/Simulator/Dessa1.4/" args[0]};
Scanner input=null;
FileWriter ofw=null;
PrintWriter out=null;
for(String file:xmlFiles) {
try {
String[] command = {"java","Test",file, "10000"};
Process pr = Runtime.getRuntime().exec(command); // run the command
input = new Scanner(new InputStreamReader(pr.getInputStream()));
String line=null;
ofw=new FileWriter(file"_out.txt");
out=new PrintWriter(ofw);
while(input.hasNextLine()) {
line=input.nextLine();
System.out.println(line);
if(line.matches("[0-9.\\s]*")) { // I included this as I didn't want the '@^' character - but still, it is being produced.
out.println(line);
out.flush();
String[] str = line.split(" ");
if(!line.equals("Badly formatted XML file")){
if((Float.parseFloat(str[0]) >= 196)) //this is the limit upto which the process has to be run
pr.destroy();
}
}
else
continue;
}
out.close();
int exitVal = pr.waitFor();
System.out.println("Exited with error code "+exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
}
}
}
Re: Java - InputStream - Weird Characters in the Output!
Oct 19, 2009 11:13 AM
(reply 11
of 14) (In reply to
#8 )
smuthiah wrote:
Hi jschell! Thanks!
On your advice, I looked at the source code of the software; it was doing this: System.out.print(output), where output is a StringBuffer with an array of integers appended. Do you think this could have caused the problem?
I have now changed it into System.out.print(output.toString()).
I doubt that changed the behaviour.
Note however that 'print' is not 'println' yet you are reading lines.
Re: Java - InputStream - Weird Characters in the Output!
Oct 21, 2009 7:11 PM
(reply 13
of 14) (In reply to
#12 )
That was awesome! Thanks a lot for the support, guys!
I guess, it resolved the problem - since yesterday morning, I haven't got those errors. What was the weirdest thing I was doing here? I thought once the process is destroyed, the while loop automatically exits and there was no need for a break.
But there was one more thing, I noticed. When I place the break command right after the pr.destroy(), for some unknown reasons, only the first line of the simulation was output to the file; the processes were running, though. [I recompiled everything and tried many times. ] Then, I introduced a flag value there and then before closing the while, I just check for the flag's reset value and exit accordingly - now its working perfectly. What could have been the problem?