File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes I/O and Streams and the fly likes Converting data file to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Converting data file to text file" Watch "Converting data file to text file" New topic

Converting data file to text file

Antonio Oroz

Joined: Sep 08, 2005
Posts: 1
Hi, I am trying to take data from the serial port and put it into a text file. I found something similar online that puts it into a data file but I need the text file. I heard that I have to use a lookup table but I am not sure how to go about doing this, does anyone know anything that can help me out. This is the code I found that puts it into a data file. I tried just changing the file to log.txt but it displays for example 25[]356[], not exactly but something like that, I dont want those square characters in there.

Thanks, Antonio

import java.util.*;
import javax.comm.*;
import java.awt.Toolkit;

public class Main{

public static final int BufferByteSize = 1440; //pretty big
static CommPortIdentifier portId;
static Enumeration portList;
public static void main(String[] args) {
portList = CommPortIdentifier.getPortIdentifiers();
//System.out.println("Before While");
while (portList.hasMoreElements()) {

portId = (CommPortIdentifier) portList.nextElement();
System.out.println("Found [" + portId.getName()+ "]");
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
if (portId.getName().equals("COM1")) {
//if (portId.getName().equals("/dev/term/a"))
Pinglog Pingloger = new Pinglog(portId,portList,BufferByteSize);
Thread readThread = new Thread(Pingloger);


class Pinglog implements Runnable, SerialPortEventListener {
//Buffer buffer;
DataOutputStream out;
CommPortIdentifier portId;
Enumeration portList;
byte[] readBuffer;
public int numBytes=0;
ByteArrayOutputStream buffer;
InputStream inputStream;
SerialPort serialPort;

Toolkit toolkit = Toolkit.getDefaultToolkit();
boolean firstBeepFlag=true;
int byteSize;

public Pinglog(CommPortIdentifier cpid, Enumeration plist, int byteSize) {
buffer = new ByteArrayOutputStream();
readBuffer = new byte[byteSize];
//buffer =b;
//set up data output stream
out = new DataOutputStream(
new FileOutputStream("log.dat"));
}catch (FileNotFoundException e) {}
//set up input stream
try {
serialPort = (SerialPort)"SimpleReadApp", 2000);
System.out.println("did return null?" + (serialPort == null));
System.out.println("Com Buffer size: " + serialPort.getInputBufferSize());
} catch (PortInUseException e) {
System.out.println(" threw exception " + e);
try {
inputStream = serialPort.getInputStream();
} catch (IOException e) {}
try {
} catch (TooManyListenersException e) {}
try {
} catch (UnsupportedCommOperationException e) {}


public void run() {
try {
} catch (InterruptedException e) {}

public void serialEvent(SerialPortEvent event) {
switch(event.getEventType()) {
case SerialPortEvent.BI:
case SerialPortEvent.OE:
case SerialPortEvent.FE:
System.out.println("Framing error");
case SerialPortEvent.PE:
case SerialPortEvent.CD:
case SerialPortEvent.CTS:
case SerialPortEvent.DSR:
case SerialPortEvent.RI:
case SerialPortEvent.OUTPUT_BUFFER_EMPTY:
case SerialPortEvent.DATA_AVAILABLE:
readBuffer = new byte[byteSize];
int i=0;
try {
while ( (i = inputStream.available()) > 0 ){, 0, i);
buffer.write(readBuffer, 0, i);
}catch (IOException e)

[ September 08, 2005: Message edited by: Antonio Oroz ]
Norm Radder
Ranch Hand

Joined: Aug 10, 2005
Posts: 685
To get characters from bytes, if you know the encoding, there are String Constructors that take a byte[] and an encoding.

Also there are output routines such as OutputStreamWriter that take encoding in they're constructors.

You'll probably need to wrap some classes to get the effect you want. Sorry, I don't have the combination of class handy right now.
I agree. Here's the link:
subject: Converting data file to text file
Similar Threads
Send SMS by using Serial Port
unable to read the data from serial port in rfid
Dial a modem from Java?
how can I send the output of a servlet to a jsp page?
Reading data through client COM1 port from server