Java: Using Piping or any other methods to use the output from one program as input in another.

J

julielaurek

Hi!

I have a huge problem finding adequate information online so if you
have a solution to these, please please help!!! ( and I would hugely
appreciate it if you could be as detailed as possible; I am still a
java novice.)

I have a serial device and a piece of code involving javax.comm
(serialDemo, one of the examples in the package) and it is totally
possible to get info out of the serial device that way. But I'd like
to use the information displayed on the serialDemo screen as input to
another piece of code which will be building an excel sheet out of
this (grammar?)

In my code, the output from serialDemo is called serialOutput. I just
set it to be a particular array to see if I could create the excel
sheet at all. Here's the excel sheet code.

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Double;
import java.lang.String;
import java.util.*;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SimpleSpreadsheetTest{

public static String[] serialOutput = {"1","M","43423",
"2","B","50608", "3","M","53478", "4","B","55853"};
public static Double myDoubleObject = null;
public static String myStringObject;

public static Double getTheString(Double someDouble, String
someString){
return someDouble.valueOf(someString);
}

public static double getTheDouble(int i){
if (serialOutput.length != 0){
ArrayList tempList = new ArrayList(Arrays.asList(serialOutput));
String myStringObject = (String) tempList.get(i);
myDoubleObject = getTheString(myDoubleObject, myStringObject);
//return myDoubleObject.doubleValue();
}
return myDoubleObject.doubleValue();
}
public static void main(String[] args) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheetAttemptOne...Work!");
HSSFRow row ;


if (serialOutput.length % 3 != 0){
System.out.println("Insufficient Data!");
}
else
{
row = sheet.createRow(0);
HSSFCell hssfCell = row.createCell((short)0);
hssfCell.setCellValue("Event Number");
hssfCell = row.createCell((short)1);
hssfCell.setCellValue("Event Type");
hssfCell = row.createCell((short)2);
hssfCell.setCellValue("Time");

for (int i=0; i<(serialOutput.length) ; i = i + 3){
row = sheet.createRow(1 + (i/3));

hssfCell = row.createCell((short)0);
hssfCell.setCellValue(getTheDouble(i));
hssfCell = row.createCell((short)1);
hssfCell.setCellValue(serialOutput[i+1]);
hssfCell = row.createCell((short)2);
hssfCell.setCellValue(getTheDouble(i+2));
}
FileOutputStream fileOut = new
FileOutputStream("workbookAttempt6.xls");
wb.write(fileOut);
fileOut.close();
}
}
}

Which works just fine. But where am I supposed to insert code that
actually imports this serialOutput once I run serialDemo? I am not
sure if I'm being very clear with my request but feel free to pump any
questions about the code or about what I want to do and, more
importantly, answer as well as you can :)

thanks;

julie
 
J

Jeff Higgins

julielaurek said:
Hi!
I have a serial device and a piece of code involving javax.comm
But I'd like
to use the information displayed on the serialDemo screen as input to
another piece of code which will be building an excel sheet out of
this (grammar?)
I'm not real familiar with javax.comm.
This is a question. I may have misunderstood your request.
You have a stream of bytes.
You would like to convert it to a stream of characters.
You would like to scan this character stream for records.
For each record you would like to scan for fields;
You would like to place the scanned records into an
Excel worksheet.
Have you seen this?
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>
 
J

JT

Jeff said:
I'm not real familiar with javax.comm.
This is a question. I may have misunderstood your request.
You have a stream of bytes.
You would like to convert it to a stream of characters.
You would like to scan this character stream for records.
For each record you would like to scan for fields;
You would like to place the scanned records into an
Excel worksheet.
Have you seen this?
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>
This is another one of those situations where I think the OP should
stop at the point where the text file is created. What if the person
running the program doesn't have Excel, rather they have (as I do) Open
Office. Perhaps rather than to assume Excel ( an excellent idea ) the
program in question should pop up some sort of selection list where the
user can choose which spreadsheet application they have.
 
J

Jeff Higgins

JT said:
This is another one of those situations where I think the OP should stop
at the point where the text file is created. What if the person running
the program doesn't have Excel, rather they have (as I do) Open Office.
Perhaps rather than to assume Excel ( an excellent idea ) the program in
question should pop up some sort of selection list where the user can
choose which spreadsheet application they have.

import org.apache.poi
Perhaps your suggestion would be better addressed to the original poster.
 
B

bencoe

Hi!

I have a huge problem finding adequate information online so if you
have a solution to these, please please help!!! ( and I would hugely
appreciate it if you could be as detailed as possible; I am still a
java novice.)

I have a serial device and a piece of code involving javax.comm
(serialDemo, one of the examples in the package) and it is totally
possible to get info out of the serial device that way. But I'd like
to use the information displayed on the serialDemo screen as input to
another piece of code which will be building an excel sheet out of
this (grammar?)

In my code, the output from serialDemo is called serialOutput. I just
set it to be a particular array to see if I could create the excel
sheet at all. Here's the excel sheet code.

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Double;
import java.lang.String;
import java.util.*;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SimpleSpreadsheetTest{

public static String[] serialOutput = {"1","M","43423",
"2","B","50608", "3","M","53478", "4","B","55853"};
public static Double myDoubleObject = null;
public static String myStringObject;

public static Double getTheString(Double someDouble, String
someString){
return someDouble.valueOf(someString);
}

public static double getTheDouble(int i){
if (serialOutput.length != 0){
ArrayList tempList = new ArrayList(Arrays.asList(serialOutput));
String myStringObject = (String) tempList.get(i);
myDoubleObject = getTheString(myDoubleObject, myStringObject);
//return myDoubleObject.doubleValue();
}
return myDoubleObject.doubleValue();
}
public static void main(String[] args) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheetAttemptOne...Work!");
HSSFRow row ;

if (serialOutput.length % 3 != 0){
System.out.println("Insufficient Data!");
}
else
{
row = sheet.createRow(0);
HSSFCell hssfCell = row.createCell((short)0);
hssfCell.setCellValue("Event Number");
hssfCell = row.createCell((short)1);
hssfCell.setCellValue("Event Type");
hssfCell = row.createCell((short)2);
hssfCell.setCellValue("Time");

for (int i=0; i<(serialOutput.length) ; i = i + 3){
row = sheet.createRow(1 + (i/3));

hssfCell = row.createCell((short)0);
hssfCell.setCellValue(getTheDouble(i));
hssfCell = row.createCell((short)1);
hssfCell.setCellValue(serialOutput[i+1]);
hssfCell = row.createCell((short)2);
hssfCell.setCellValue(getTheDouble(i+2));
}
FileOutputStream fileOut = new
FileOutputStream("workbookAttempt6.xls");
wb.write(fileOut);
fileOut.close();
}
}

}

Which works just fine. But where am I supposed to insert code that
actually imports this serialOutput once I run serialDemo? I am not
sure if I'm being very clear with my request but feel free to pump any
questions about the code or about what I want to do and, more
importantly, answer as well as you can :)

thanks;

julie

I'm not clear exactly what you might be trying to do, but you can
probably get a few programs patched together using,

Runtime runtime = Runtime.getRuntime();
Process proc = runtime.exec(
"ls" + " " + dir);

Depending on what the output is you could also try serializing objects
to disk, and then just loading them back into your program using
ObjectInput and ObjectOutput streams, just a couple suggestions to get
you going (perhaps not feasible ones depending on the exact nature of
your problem, I'm not too familiar with the library you are using.
 
J

JT

Hi!

I have a huge problem finding adequate information online so if you
have a solution to these, please please help!!! ( and I would hugely
appreciate it if you could be as detailed as possible; I am still a
java novice.)

I have a serial device and a piece of code involving javax.comm
(serialDemo, one of the examples in the package) and it is totally
possible to get info out of the serial device that way. But I'd like
to use the information displayed on the serialDemo screen as input to
another piece of code which will be building an excel sheet out of
this (grammar?)

In my code, the output from serialDemo is called serialOutput. I just
set it to be a particular array to see if I could create the excel
sheet at all. Here's the excel sheet code.

import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.Double;
import java.lang.String;
import java.util.*;


import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class SimpleSpreadsheetTest{

public static String[] serialOutput = {"1","M","43423",
"2","B","50608", "3","M","53478", "4","B","55853"};
public static Double myDoubleObject = null;
public static String myStringObject;

public static Double getTheString(Double someDouble, String
someString){
return someDouble.valueOf(someString);
}

public static double getTheDouble(int i){
if (serialOutput.length != 0){
ArrayList tempList = new ArrayList(Arrays.asList(serialOutput));
String myStringObject = (String) tempList.get(i);
myDoubleObject = getTheString(myDoubleObject, myStringObject);
//return myDoubleObject.doubleValue();
}
return myDoubleObject.doubleValue();
}
public static void main(String[] args) throws IOException{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheetAttemptOne...Work!");
HSSFRow row ;


if (serialOutput.length % 3 != 0){
System.out.println("Insufficient Data!");
}
else
{
row = sheet.createRow(0);
HSSFCell hssfCell = row.createCell((short)0);
hssfCell.setCellValue("Event Number");
hssfCell = row.createCell((short)1);
hssfCell.setCellValue("Event Type");
hssfCell = row.createCell((short)2);
hssfCell.setCellValue("Time");

for (int i=0; i<(serialOutput.length) ; i = i + 3){
row = sheet.createRow(1 + (i/3));

hssfCell = row.createCell((short)0);
hssfCell.setCellValue(getTheDouble(i));
hssfCell = row.createCell((short)1);
hssfCell.setCellValue(serialOutput[i+1]);
hssfCell = row.createCell((short)2);
hssfCell.setCellValue(getTheDouble(i+2));
}
FileOutputStream fileOut = new
FileOutputStream("workbookAttempt6.xls");
wb.write(fileOut);
fileOut.close();
}
}
}

Which works just fine. But where am I supposed to insert code that
actually imports this serialOutput once I run serialDemo? I am not
sure if I'm being very clear with my request but feel free to pump any
questions about the code or about what I want to do and, more
importantly, answer as well as you can :)

thanks;

julie

>
This is another one of those situations where I think the OP should
stop at the point where the text file is created. What if the person
running the program doesn't have Excel, rather they have (as I do) Open
Office. Perhaps rather than to assume Excel ( an excellent idea ) the
program in question should pop up some sort of selection list where the
user can choose which spreadsheet application they have.
 
J

julielaurek

I'm not real familiar with javax.comm.
This is a question. I may have misunderstood your request.
You have a stream of bytes.
You would like to convert it to a stream of characters.
You would like to scan this character stream for records.
For each record you would like to scan for fields;
You would like to place the scanned records into an
Excel worksheet.
Have you seen this?
<http://java.sun.com/docs/books/tutorial/essential/io/index.html>

I have seen it; but after reading it I couldn't figure out where in
each of serialDemo and SimpleSpreadsheetTest I have to include
pipedInputStream and pipedOutputStream, which had then seemed most
appropriate. Maybe I shouldn't be using them?

I have the same problem with Runtime, class which I have hardly used
before so I will read about that right away and see if it works. (with
regards to Ben's post)

with regards to javax.comm: basically it helps you have access to
serial devices. With the class that I'm using, serialDemo, it is
possible to choose a COMM port to communicate with. The output from
this port is what I'd like to be able to use as input for my excel
sheet creating class.

Thanks,

julie
 
M

Martin Gregorie

I have seen it; but after reading it I couldn't figure out where in
each of serialDemo and SimpleSpreadsheetTest I have to include
pipedInputStream and pipedOutputStream, which had then seemed most
appropriate. Maybe I shouldn't be using them?

I have the same problem with Runtime, class which I have hardly used
before so I will read about that right away and see if it works. (with
regards to Ben's post)

with regards to javax.comm: basically it helps you have access to
serial devices. With the class that I'm using, serialDemo, it is
possible to choose a COMM port to communicate with. The output from
this port is what I'd like to be able to use as input for my excel
sheet creating class.
Why not just set up a pipeline as a command line? Something like

java CSVGenerator | java SpreadsheetCreator

should work for DOS/Windows as well as Linux or UNIX - its just slow as
hell in DOS/Windows because it runs the programs one at a time and uses
temporary files to hold the intermediate results.
 
J

julielaurek

Why not just set up a pipeline as a command line? Something like

java CSVGenerator | java SpreadsheetCreator

should work for DOS/Windows as well as Linux or UNIX - its just slow as
hell in DOS/Windows because it runs the programs one at a time and uses
temporary files to hold the intermediate results.

would you know how to do that in eclipse? that's one of the problems
I've faced with my quest for info and help; I learned java using
eclipse and so i have difficulties calling anything directly from the
command line. I have tried to resort to it but my classes are all
stored in a really complex way with the help of eclipse so that the
prompt never knows where to locate anything...

thanks so much for your input guys; i'll be searching, still, but if
you come up with anything else, feel free to add it! :)

julie
 
J

Jeff Higgins

julielaurek said:
I have seen it; but after reading it I couldn't figure out where in
each of serialDemo and SimpleSpreadsheetTest I have to include
pipedInputStream and pipedOutputStream, which had then seemed most
appropriate. Maybe I shouldn't be using them?
Ok. You've made me curious enough to
download comm package to see exactly
what SerialDemo does. In the class
SerialConnection you'll see where the
InputStream and OutputStream are hooked up
to the awt TextArea(s). That is where
you want to create a stream to hook up
to your SimpleSpreadsheetTest. As far as
where in SimpleSpreadsheetTest, well it's
your class. Wherever you wish.
 
J

julielaurek

Ok. You've made me curious enough to
download comm package to see exactly
what SerialDemo does. In the class
SerialConnection you'll see where the
InputStream and OutputStream are hooked up
to the awt TextArea(s). That is where
you want to create a stream to hook up
to your SimpleSpreadsheetTest. As far as
where in SimpleSpreadsheetTest, well it's
your class. Wherever you wish.


thanks a lot, Jeff! it's about to work :) Once I fix my jar files,
which don't seem to want to be executable from my desktop anymore,
I'll let you guys know if it worked. Thanks a lot for all your
input!!!

Julie
 
J

Jeff Higgins

julielaurek said:
it's about to work :) Once I fix my jar files,
which don't seem to want to be executable from my desktop anymore,
I'll let you guys know if it worked. Thanks a lot for all your
input!!!
Good deal. It's always nice to hear the rest of the story. Thanks.
JH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top