How to read/dump unwanted information java

I am using netbean. I have serial data coming in in Hex followed by \r\n
So data would be 000845AB\r\n
In my java program, i want to read 000845AB, then convert it to decimal and perform some calculation.
I can read this but then if i read again the conversion is distorted because its trying to convert \r\n to decimal which doesn't work and comes up a NumberFormatException. If i don't read or dump it then it remains in the serial buffer. So either i stop sending \r\n serially and just read 8bytes, 8 bytes. But how do i ignore \r\n that comes in the buffer.
Here's a quick function of my receive serial
int byteLength = 8;
byte[] readInputBuffer = new byte[byteLength];
String stringInput = null;
int stringBytes = 0;
int numBytes = 0; // return value
if (is.available() > 0) {
stringBytes = is.read(readInputBuffer);
stringInput = new String(readInputBuffer); // store hex number into stringInput
if (stringInput != null) {
for (int i = 0; i < (stringBytes-2); i++) {
String hex = stringInput;
numBytes = Integer.parseInt(hex, 16);
else {
return 0;
return numBytes;

Construct a BufferedReader on top of your input string and use its readLine() method.
public void doSomething(InputStream bytesIn){
    BufferedReader in = new BufferedReader(new InputStreamReader(bytesIn));
    String line = null;
    while((line = in.readLine()) != null){
        // the readLine method strips the line endings so you can process you string without
        // without worrying about them. Additionally, using a Buffered reader you don't need to worry
        // about buffering the data or checking for the end of the stream yourself. makes your life
        // easier
}

Similar Messages

  • How to read system eventlog using java program in windows?

    How to read system eventlog using java program in windows?
    is there any java class available to do this ? or any one having sample code for this?
    Your friend Zoe

    Hi,
    There is no java class for reading event log in windows, so we can do one thing we can use windows system 32 VBS script to read the system log .
    The output of this command can be read using java program....
    we can use java exec for executing this system32 vbs script.
    use the below program and pass the command "eventquery"
    plz refer cscript,wscript
    import java.io.*;
    public class CmdExec {
    public static void main(String argv[]) {
    try {
    String line;
    Process p = Runtime.getRuntime().exec("Command");
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(p.getInputStream()));
    while ((line = input.readLine()) != null) {
    System.out.println(line);
    input.close();
    catch (Exception err) {
    err.printStackTrace();
    This sample program will list all the system log information....
    Zoe

  • How To Read RTF file in JAVA?  Using  iText?

    How To Read RTF file in JAVA?  Using  iText?.....
    import java.io.*;
    import com.lowagie.text.*;
    import com.lowagie.text.rtf.*;
    public class RTF3 {
    public static void main(String[] args) {
    // System.out.println("This example generate a RTF file name Sample.rtf");
    // Create Document object
    Document myDoc = new Document();
    try {
    // Create writer to listen document object
    // and directs RTF Stream to the file Sample.rtf
    RtfWriter2.getInstance(myDoc, new FileOutputStream("Sample.rtf"));
    // open the document object
    myDoc.open();
    // Create a paragraph
         Paragraph p = new Paragraph();
         p.add("Helloworld in Rtf file..amazing isn't");
         // Add the paragraph to document object
    myDoc.add(p);
    catch(Exception e) {
    System.out.println(e);
    //close the document
    myDoc.close();
    Exception in thread "main" java.lang.NoSuchMethodError: com.lowagie.text.Rectangle.width()F
         at com.lowagie.text.rtf.document.RtfPageSetting.rectEquals(RtfPageSetting.java:433)
         at com.lowagie.text.rtf.document.RtfPageSetting.guessFormat(RtfPageSetting.java:362)
         at com.lowagie.text.rtf.document.RtfPageSetting.setPageSize(RtfPageSetting.java:341)
         at com.lowagie.text.rtf.RtfWriter2.setPageSize(RtfWriter2.java:248)
         at com.lowagie.text.Document.open(Unknown Source)
         at view.RTF3.main(RTF3.java:23)
    CAN you HELP me?

    import com.lowagie.text.Document;
    import com.lowagie.text.rtf.parser.RtfParser;
    import java.io.FileInputStream;
    String inputFile = "sample.rtf";
    Document document = new Document();
    document.open();
    RtfParser parser = new RtfParser(null);
    parser.convertRtfDocument(new FileInputStream(inputFile), document);

  • How to read system evenlog using java program in windows

    How to read system evenlog using java program in windows???
    is there any java class available to do this ? or any one having sample code for this?
    Your friend Zoe

    Welcome to the Sun forums.
    >
    How to read system evenlog using java program in windows???>
    JNI. (No.)
    >
    is there any java class available to do this ? or any one having sample code for this?>You will generally get better help around here if you read the documentation, try some sample code and come back with a specific question (hopefully with an SSCCE included).
    >
    Your friend Zoe>(raised eyebrow) Thank you for sharing that with us.
    Note also that one '?' denotes a question, while 2 or more generally denotes a dweeb.

  • How to read pdf files using java.io package classes

    Dear All,
    I have a certain requirement that i should read and write PDF files at runtime. With normal java file IO reading is not working. Can any one suggest me how to proceed probably with sample code block
    Thanks in advance.

    hi I also have the pbm. to read pdf file using JAVA
    can any body help meWhy is it so difficult to read the thread you posted in? They say: java.io is pointless, use iText. So why don't you?
    or also I want to read a binary encoded data into
    ascii,
    can anybody give me a hint how to do it.Depends on what you mean with "binary encoding". ASCII's binary encoding, too, basically.

  • How to read File Header in JAVA

    I wanna upload a(doc/pdf) file from client site, where I hv to check whether the the file is originally a doc/pdf file or not, I mean not just the extension, but the actual header information of the file. Please some body help me to do this, I dont know how to read the file header. Thnx in advnc.

    You can always try looking at the [Header Signatures|http://www.techpathways.com/uploads/headersig.txt]. But it would probably be easier to adjust your application so it doesn't have to guess.
    ~

  • How to read XML files from java

    i need a sugession that how to read a xml file using java code
    and i need to parse using some parsers and display attributes and entity seperately
    as a string.......

    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.io.SAXReader;
    import java.io.File;
    import java.text.AttributedCharacterIterator.Attribute;
    import java.util.Iterator;
    import java.util.StringTokenizer;
    public class XmlParser
    private String Result="";
    private String Final="";
    private String Delim="";
    public void bar1(Document document) throws DocumentException
    org.dom4j.Element root = document.getRootElement();
    // System.out.println(root.getName());
    bar2(root);
    System.out.println(this.Result);
    process();
    public void bar2(org.dom4j.Element e)
    for(Iterator i = e.elementIterator();i.hasNext();)
    org.dom4j.Element Element = (org.dom4j.Element) i.next();
    Result += Element.getName()+"\t"+Element.getText()+"\n";
    bar2(Element);
    public void process()
    StringTokenizer Tokenizer = new StringTokenizer(this.Result,"\n");
    String element;
    while(Tokenizer.hasMoreTokens())
    element = Tokenizer.nextToken();
    StringTokenizer Tokenizer2 = new StringTokenizer(element,"\t");
    // Do what ever String Process here Example
    this.Final += element.getName();
    this.Final += this.Delim;
    System.out.println(this.Final);
    public static void main(String s[])throws Exception
    Document document = null;
    SAXReader reader = new SAXReader();
    File f1= new File("D:/Rajesh/EDI to XML/EDI.xml");
    document = reader.read(f1);
    Demo obj = new Demo();
    obj.bar1(document);
    i think this will hep full.......

  • How to Read An Attachment Using Java Mail

    Hi
    I Am Able To Read The Mail Using Java Mail ,but Unable To Read The Attachment Which Comes Along With The Mail.
    Please Help Me , In Reading The Attachment.

    Hi
    I Am Able To Read The Mail Using Java Mail ,but
    t Unable To Read The Attachment Which Comes Along
    With The Mail.
    Please Help Me , In Reading The Attachment.Do you mean:
    - I only recieve .txt or .doc attachments and I want to see the contents? Or could you get a .jpg as well?
    or
    - Do you need to seperate the attachment from the e-mail and then view it?

  • How to read outlook mail using java

    Hi,
    I am new to javamail ,
    I want to read outlook mail using java
    thanks
    Edited by: My_Problems on Jun 20, 2008 11:13 AM

    Hi!
    See Java Api Msgparser in GNU General Public License...
    A parser for .msg Files in Java :
    [http://auxilii.com/msgparser/|http://auxilii.com/msgparser/]
    Use POI... Apache POI - Java API To Access Microsoft Format Files...
    regards,

  • How to read Korean characters using Java program?

    In Oracle table, i am having Korean characters, how to read those characters using JDBC and insert into SQL Server?
    NLS_NCHAR_CHARACTERSET is UTF8

    What data type is the column in the Oracle table? The NLS_NCHAR_CHARACTERSET would control the encoding of NCHAR and NVARCHAR2 columns. The NLS_CHARACTERSET would control the encoding of CHAR and VARCHAR2 columns.
    Justin

  • How to read SGML files using Java

    I've got a text categorisation test collection called Reuters-21578 for my Information Retrieval project. It is distributed in 22 files. Each of the first 21 files (reut2-000.sgm through reut2-020.sgm) contains 1000 documents, while the last (reut2-021.sgm) contains 578 documents. The files are in SGML format. Each of the 22 files begins with a document type declaration line:
    <!DOCTYPE lewis SYSTEM "lewis.dtd"> The DTD file lewis.dtd is included in the distribution. Following the document type declaration line are individual Reuters articles marked up with SGML tags.
    My questions is how to write a java program to read those 21578 documents or transform them into 21578 seperated text files.

    I guess I missed something. What is Renes link?. The
    parser stuff isn't really what I'm looking for. I'm
    a new at and just learning java and I just want to
    know the easiest way to read a SGML file. Should I
    use a buffered Reader with a Pushback Input Stream?Hang on.....you want to just read the file without intelligently extracting the SGML data contained within and so have no need of a parser?
    Well, in that case, its just text.....so just use BufferedReader or whatever to read the text data. If I understand you correctly, all you really wanted to ask was "how do I read a text file?"

  • How to read Excel file in java

    Respected sir/madam
    I want to read the values from Excel file in Java program. How can I do that. When I searched over the net I came to know that you can treat Excel file as a Database and write the code as u write for making DB connections .
    I did that but i am getting the following error ..can anybody please help..
    This is the code what i have written
    import java.io.*;
    import java.sql.*;
    public class ReadExcelFile {
    Connection c;
    Statement stmnt;
    public void checkABA_Number()
    try
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
    c = DriverManager.getConnection("jdbc:odbc:ExcelTest","",""); // ExcelTest is the DSN name
    stmnt = c.createStatement();
    ResultSet rs = stmnt.executeQuery("select * from abadata"); // abadata is my Excel file name
    while(rs.next())
    System.out.println(rs.getString(1)+" "+rs.getString(2));
    }catch(Exception e)
    System.out.println(""+e.toString());
    finally
    try
    stmnt.close();
    c.close();
    catch( Exception e )
    System.err.println( e );
    public static void main(String[] args)
    new ReadExcelFile().checkABA_Number();
    My Excel file starts from the first row and first column and also the first row contains the names of the column.
    It give me the following error..
    java.sql.SQLException: [Microsoft][ODBC Excel Driver] The Microsoft Jet database engine could not find the object 'abadata'. Make sure the object exists and that you spell its name and the path name correctly.
    How can I deal with this.?I have properly selected the worksheet while giving the DSN . Is there any versionig problem with Excel or some drivers are in appropriate..and yes i chose Microsoft Excel Driver (.xsl) from ODBC .
    I created System DSN.
    Can anybody please help me with this ? I will be very gratefull for replies
    Thanks in advance

    here is the code to read excel file
    public void readexel(String filename)
    Connection c = null;
    Statement stmnt = null;
    try
    Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" );
    c = DriverManager.getConnection("jdbc:odbc:Driver={Microsoft Excel Driver (*.xls)};DBQ=" + filename);
    stmnt = c.createStatement();
    String query = "Select * from [Sheet1$]" ;
    ResultSet rs = stmnt.executeQuery( query );
    while( rs.next() )
    System.out.println( rs.getString(1) );
    catch( Exception e )
    System.err.println( e );
    }

  • How to read XML Values using JAVA

    Hi FRNDS,
    I uploaded one XML File in imported Archieves, i have to read the value form XML uisng JAVA code,
    using XSL able to read  like  xsl variable name='TimeZone' select =$linkDoc//...........*
    But using java i have to read the value from XML,is it possible to read the value??
    please give me some sample code.
    regards,
    Raja Sekhar

    But using java i have to read the value from XML,is it possible to read the value??
    Check the below JAVA code from help:
    http://help.sap.com/saphelp_nwpi71/helpdata/en/55/7ef3003fc411d6b1f700508b5d5211/content.htm
    you can ignore the XSLT part and configure only the JAVA code...check if this suits your needs.
    Regards,
    Abhishek.

  • How to read or view information (ldf) File And find out operations update or drop or insert for tables ?

    I'm want read Ldf log and find out all event for database ???

    I'm want read Ldf log and find out all event for database ???
    hello,
    You can use below undocumented commands( you cannot completely read it but below commands will surely be helpful).Any specific requirement you have from log files
    select * from fn_dblog()
    dbcc loginfo(db_name)
    dbcc log
    simiilar discussion
    http://social.msdn.microsoft.com/Forums/sqlserver/en-US/5aea4dab-8d84-4515-b686-202e329ad2b6/how-to-open-or-read-sql-server-log-file-ldf?forum=sqlgetstarted
    http://stackoverflow.com/questions/9767054/read-the-log-file-ldf-in-sql-server-2008
    Please mark this reply as the answer or vote as helpful, as appropriate, to make it useful for other readers

  • How to read e-mail via java programming...

    String host= "myhostname";
    String user="myusername";
    String pass= "mypassword";
    Properties props =System.getProperties();
    Session session =Session.getInstance(props,null);
    Store store= session.getStore("imap");
    System.out.println(store);
    store.connect(host,user,pass)
    this is my java program. when i run this i am getting the following error... but i can able to send the mail.
    Exception in thread "main" javax.mail.MessagingException: Connection refused: connect;
    nested exception is:
         java.net.ConnectException: Connection refused: connect
         at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:571)
         at javax.mail.Service.connect(Service.java:288)
         at javax.mail.Service.connect(Service.java:169)
         at com.ebay.trinity.tps.EmailClient.main(EmailClient.java:25)
    Caused by: java.net.ConnectException: Connection refused: connect
         at java.net.PlainSocketImpl.socketConnect(Native Method)
         at java.net.PlainSocketImpl.doConnect(Unknown Source)
         at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
         at java.net.PlainSocketImpl.connect(Unknown Source)
         at java.net.SocksSocketImpl.connect(Unknown Source)
         at java.net.Socket.connect(Unknown Source)
         at java.net.Socket.connect(Unknown Source)
         at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:233)
         at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189)
         at com.sun.mail.iap.Protocol.<init>(Protocol.java:107)
         at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:104)
         at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:538)
         ... 3 more
    what could be the reason.. am i missing anything here. please help me..
    Thanks in advance
    Marimuthu.sp

    this is my java program. when i run this i am getting the following error... but i can able to send the mail.See this thread, its just about 2 days old:
    [http://forums.sun.com/thread.jspa?forumID=54&threadID=5349815]
    Specially see Reply#4
    Thanks!
    P.S.: Its a good practice to search the forum for the problem you are facing before posting, because there a 50% chances that others might also have faced similar problems before.

Maybe you are looking for