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: 224
Registered: ‎07-26-2010
My Device: Z10
Accepted Solution

How to remove CR+LF?

I use this method for reading text files

	public static String readTextFile(String fName) {
		String result = null;
		FileConnection fconn = null;
		DataInputStream is = null;
		try {
			fconn = (FileConnection) Connector.open(fName, Connector.READ_WRITE);
			is = fconn.openDataInputStream();
			byte[] data = IOUtilities.streamToBytes(is);
			result = new String(data);
			
		} 
		catch (IOException e) {
			System.out.println(e.getMessage());
		} 
		finally {
			try {
				if (is != null) is.close();
				if (fconn != null) fconn.close();
			} 
			catch (IOException e) {
				System.out.println(e.getMessage());
			}
		}
		
		return result;
	}

 There's a small catch: %0D%0A (CR+LF) will be appended to the result.

So, for example, if the content of test.txt is Helloworld, then applying that method to test.txt will yield Helloworld%0D%0A.

 

How to remove this CR+LF?

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to remove CR+LF?

If you are seeing the CRLF it is because the data is actually there.  Presumably you created your file using Windows and so will get this appended to each line.  Create this on Linux and you typically only see LF..

 

Most parsers just process this byte at a time and ignore CR and LF.  Presumably you can do the same.

 

Alternatively you could try using another Stream processor like the LineNumberReader and it will probably just use these markers to indicate end of line and give you each line separately.  But I have never tested that, so you will need to confirm. 

Developer
Posts: 64
Registered: ‎02-12-2011
My Device: 9300

Re: How to remove CR+LF?

Hi.

If you want to remove characters from a String you can use the StringUtilities method "removeChars"

http://www.blackberry.com/developers/docs/5.0.0api/index.html?net/rim/device/api/util/StringUtilitie...

That being said, be aware that the CR+LF is not added by your program. It is already there in your file like peter_strange says.
Developer
Posts: 224
Registered: ‎07-26-2010
My Device: Z10

Re: How to remove CR+LF?

I solved this problem by using the method, found at this page:

http://stackoverflow.com/questions/593671/remove-end-of-line-characters-from-java-string

 

static byte[] discardWhitespace(byte[] data) {
    byte groomedData[] = new byte[data.length];
    int bytesCopied = 0;

    for (int i = 0; i < data.length; i++) {
        switch (data[i]) {
            case (byte) '\n' :
            case (byte) '\r' :
                break;
            default:
                groomedData[bytesCopied++] = data[i];
        }
    }

    byte packedData[] = new byte[bytesCopied];

    System.arraycopy(groomedData, 0, packedData, 0, bytesCopied);

    return packedData;
}

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to remove CR+LF?

Great.

 

Can you mark ths as solved then?  Instructions in Help  link near the top of your screen if you need them.  Thanks