0

I have a text file with numbers, letters, special characters, and symbols. There are some lines where I want to insert an RLE unicode control character at the beginning/middle/end of the line.

First I needed to find out how to catch and represent RLE. I thought of streams. I found out that RLE takes up 3 bytes -30 -128 -85

InputStream input = new BufferedInputStream (new FileInputStream (file_name_here_with_path));
byte[] = input.read();

If what the app reads included an RLE character, then when printing the array you'll get those 3 signed numbers.

Next problem, CURRENT PROBLEM, is to find a suitable container for this information.

input.read(): this returns the byte the app read. I can save it in a byte array but I can't even create the array unless I knew its size. No, the file size isn't the size of the array because I need to insert those 3 bytes into the array more than once and at different locations depending on some conditions I set.

input.read(byte[] array): this returns an int representing the number of bytes that were read. The parameter will have all that info saved. Same problem as the above. Array of fixed size

input.read(byte[] array, offset, length): same as previous only I can make it read from any point I want and for as long as I want unlike the previous ones where it reads from the beginning to the end or until some exception is thrown

Use bufferedReader: same problem. I read a line, save it in a string, turn the string to byte array (stringname.getBytes()). fixed size. can't insert.

The solution to all 4 methods is to create a new byte array and move the bytes from the old array to the new one while inserting the control character. THE PROBLEM, maybe, is that according to a member here, Javier, the read method is slow. I haven't received confirmation yet as I wasn't sure if he meant one specific read or all 3. Also, even if I knew how many extra slots I needed in the new array, would it be good practice to create the new array of such a size? This reminds me, my txt file is 200KB tops. It's not much but I'm looking for the RIGHT practice. The generic solution!

Anyways, I looked for alternatives. I recall using vectors. Yes they're obsolete. I don't know why and since I'm not creating a big app or an app for a client then I can use vectors :P HOWEVER, i thought I should keep reading. Then I came across ArrayList and I read a post here about how it performs better.

So ... what will it be? possible slow performing read methods or bufferedReader or the obsolete vector or the fast performing ArrayList? :P

Community
  • 1
  • 1
user3340667
  • 23
  • 2
  • 6
  • Do you intend to process them as raw bytes (handling Unicode yourself), or as Unicode characters (let them be handled by the platform)? – rwong Feb 22 '14 at 23:03
  • I'm not sure what you mean. I don't get it. But I think it's the first one. I want to handle them myself. ill just write an example. if my sentence is a b c d. then when reading it i get 97 32 98 32 99 32 100. now i want to insert the RLE in the middle..between b and c. so it becomes 97 32 98 32 -30 -128 -85 99 32 100 hope this helps. – user3340667 Feb 23 '14 at 07:25

1 Answers1

0

Vectors were replaced by the faster ArrayLists with the caveat that that ArrayLists are not thread safe (but you could call a synchronize method for Collections to negate this) and has a different data growth method (every time a resize needs to be made, ArrayList increases size by 50% while vectors basically double). Apart from this, they are pretty much the same.

Given your options, I would go with an ArrayList that holds Objects (ArrayList) as in this manner, you could retain the original element type. Although, in such a case, you need to keep a track of what type of element is in each index position (if it is necessary).

ucsunil
  • 7,378
  • 1
  • 27
  • 32
  • I got the part about the resize. but the first two lines aren't clear especially the first one. what about reading the text? do i read a line or use inputstream to read? – user3340667 Feb 23 '14 at 13:32
  • In your case (considering you have special characters), I would say that the best way to go about reading the file would be through using FileReader - it preserves the characters. I am not certain how well InputStream handles special characters. – ucsunil Feb 24 '14 at 01:50
  • ya that's what I ended up doing. I used FileReader because it's easier to check but my output is bufferedOutputStream because I definitely need to write in the form of bytes. Thanx again. – user3340667 Feb 24 '14 at 22:31