Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers
Accepted Solution

Line Break while writing text file using OutputStream

This seems like a bit of a dumb question, but I'm stumped. I'm writing a simple eventLog for my program which at the moment just contains some hex strings. Since OutputStream doesn't have a writeLine or a writeln method, there's no innate line break, and they all show up on one line. Simple enough to fix... or so I thought. The line break character is being rejected. I've tried \n, \r, 0x0A, 0x0C, 0x0D, 0x1E, "\n".getBytes (), byte [] br = {/*insert host of line break notations here*/}... Each is replaced by a little black box. How can I programmatically insert a line break into  a .txt file using OutputStream.write (*)?

 

Currently testing this on the Bold 9000 Generic simulator.

 

 

public void log (String entry)
  {
    //Circular Buffer logic
    if (eventLog.size () == 100)
    {
      eventLog.removeElementAt (0);
    }
    
    eventLog.addElement (entry);
    
    //TODO: Remove this
    flushLog ();
  }
  
  //Empty log to external file
  public void flushLog ()
  {
    FileConnection logWriter;
    
    try
    {
      logWriter = (FileConnection) Connector.open
                  ("file:///SDCard/BlackBerry/documents/log.txt",
                   Connector.READ_WRITE);
    }
    catch (IOException e)
    {
      //Debug
      System.out.println ("ERROR: File connection could no be created.");
      System.out.println (e);
      
      return;
    }
    
    try
    {
      try
      {
        if (!logWriter.exists ())
        {
          logWriter.create ();
          
          //Debug
          System.out.println ("Log Successfully created");
        }
      }
      catch (Exception e)
      {
        //Debug
        System.out.println (e);
      }
      
      //Debug
      System.out.println (logWriter);
      System.out.println ("Creating output stream...");
      
      OutputStream logOut = logWriter.openOutputStream ();
      
      //Debug
      System.out.println ("OutputStream successfully created");
      
      try
      {
        for (int count = 0; count < 100; count++)
        {
          //Debug
          System.out.println ("Log Write Loop: " + count);
          
          logOut.write (((String) eventLog.elementAt (count)).getBytes ());
          
          //test
          //byte [] br = {0x0A, 0x0C, 0x0D, 0x1E};
          
          String br = "\n";
          logOut.write (br.getBytes ());
        }
      }
      catch (ArrayIndexOutOfBoundsException e)
      {
        //Debug
        System.out.println ("Buffer not full");
      }
      catch (Exception e)
      {
        //Debug
        System.out.println (e);
      }
      
      logOut.close ();
    }
    catch (IOException e)
    {
      //Debug
      System.out.println ("Error writing event log: May be incomplete.");
      System.out.println (e);
    }
    finally
    {
      try
      {
        logWriter.close ();
      }
      catch (IOException e)
      {
        //Debug
        System.out.println ("Error closing log Stream");
        System.out.println (e);
      }
    }
  }

 

 

Confused,

~Dom

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Line Break while writing text file using OutputStream

[ Edited ]

With what are you seeing the "little black box"? \n should work fine.

 

Ed. If you're viewing this file on a Windows machine, try "\r\n", because different operating systems expect different line ending conventions. "\n" should work fine for Blackberry internals strings and such.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Line Break while writing text file using OutputStream

Notepad. Now I know it's not exactly a full-featured suite, but ASCII encoding is definitely within the scope of its programming.

 

Hmm...

 

Is BlackBerry Unix-based? I remember I had a similar problem when porting files from Linux to my Windows computer...

 

br_fail.JPG

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
Developer
Posts: 562
Registered: ‎09-30-2009
My Device: Not Specified

Re: Line Break while writing text file using OutputStream

Windows, Notepad, whatever, expects CRLF for newlines. \n gives you LF, which works fine on 'nix systems; \r\n is what you want. Clever Windows editors will handle either convention, but Notepad won't.

 

Blackberry is probably not 'nix based; it's just a newline convention.

Developer
Posts: 587
Registered: ‎01-19-2010
My Device: BOLD 9700 OS5.0.0.x on Rogers Canada
My Carrier: Rogers

Re: Line Break while writing text file using OutputStream

That did the trick! Thank you, sir.

----------------------------------------------------------------------------
chown -R us ./base
~J!NX
New Contributor
Posts: 4
Registered: ‎08-10-2011
My Device: 8520
My Carrier: entel

Re: Line Break while writing text file using OutputStream

I use logOut.write (((String) eventLog.elementAt (count)+Characters.ENTER).getBytes ());