0

I will enhance an old algorithm which use java language and i want it to read the text from file to encrypt it SO I will make a method that reads the text line by line from file then store them in array. I made this method and it works BUT the variable "line2" reads the first line correctly but once the next line come it will erase the first line and put the second line so what can i do please??

// The CODES

Private byte[] line2;

public byte[] readFromFile (){ 
    try (BufferedReader br = new BufferedReader(new FileReader    ("C:\\Users\\just\\Desktop\\message.txt")))
    {

        String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
            line2 = sCurrentLine.getBytes();
        }

    } catch (IOException e) {
        e.printStackTrace();
    }     
    return line2; 
}
SimonC
  • 6,590
  • 1
  • 23
  • 40
Yusra
  • 1
  • 2

3 Answers3

0

You said you will read the file line by line and store them in an array, but you haven't stored them in an array. Below the line line2 = sCurrentLine.getBytes();, store line2 in an array, and then read the next line.

sCurrentLine.getBytes(); returns the content of sCurrentLine as a byte array, so every time this statement is executed, it will return the bytes of the current line and so the previous line's contents is lost. So you have to store the contents line2 in another array, before reading the next line's byte.

You could use System.arraycopy() to copy the contents of line2 and append it to the contents of the previous line using this method. You can look at System class docs to find out how to use the System.arraycopy() method. Also have a look at Appending a byte[] to the end of another byte[] to append the contents of array to another array.

Community
  • 1
  • 1
anirudh
  • 4,116
  • 2
  • 20
  • 35
  • it says that u are not allow to store byte in byte[] – Yusra Mar 29 '14 at 06:07
  • I have edited the answer according to your comment. Just create a new `byte[]` array and use it to store the contents as explained in the answer. – anirudh Mar 29 '14 at 09:07
  • thank you it works BUT can you tell me which method is better and faster (( mine or fge answer )) because his method is more simple than me and i think if any method wrote in simple way will execute faster. – Yusra Mar 29 '14 at 12:34
  • The method you have used is the same as what the `System.arraycopy()` will do, but we could trust the java API to be efficient if we use `System.arraycopy()`. However, fge's method of reading bytes from file will I think be the most efficient, especially since you are not decoding bytes into characters and then encoding them, and for the reasons of avoiding corruption as explained by fge. – anirudh Mar 29 '14 at 13:16
0

i want it to read the text from file to encrypt it

Reading as text is a surefire way of getting corrupted data. Read as bytes. More on this below.

With Java 7, it is as simple as:

final Path file = Paths.get("C:\\Users\\just\\Desktop\\message.txt");
final byte[] content = Files.readAllBytes(file);

Why corruption?

  • first of all, a BufferedReader's .readLine() strips newlines; the content you will encrypt will therefore not be the same;
  • second, you don't specify an encoding with which to read the file, and you don't specify an encoding to encode to bytes; and the JVM can choose to use a different default encoding and file encoding. Imagine what would happen if you read the file in windows-1252 and decoded them using UTF-8.

More generally:

  • when you "read a string" from a file, what is read is not characters; those are bytes. And a CharsetDecoder will then decode this sequence of bytes into a sequence of chars (possibly with information loss);
  • when you "write a string" to a file, what is written is not characters; again, those are bytes. And a CharsetEncoder will encode this sequence of chars into a sequence of bytes.
fge
  • 119,121
  • 33
  • 254
  • 329
  • Thank you so much for helping but this does not work till i changed instead of path i wrote file BUT how can i guarantee the result please. final byte[] content = Files.readAllBytes(file); – Yusra Mar 29 '14 at 06:04
  • Sorry, I don't understand what you mean at all? – fge Mar 29 '14 at 09:34
  • you put "path" as an argument in readAllBytes, but it does not work. SO when i change it to "file" it works. do u understand now. – Yusra Mar 29 '14 at 12:25
  • Could you check my answer above, because i insert my code it works and i want to you check which one better mine or yours or no different because i think if the code written in simple way will execute better. – Yusra Mar 29 '14 at 12:39
  • You do the same fundamental mistake again! Do NOT use a `BufferedReader`. Read my answer carefully again. – fge Mar 29 '14 at 13:29
0

I do it like this :

public byte[] readFromFile ()throws IOException { BufferedReader br = new BufferedReader(new FileReader("C:\Users\just\Desktop\message.txt")); int k = 0; int f=0; byte[] line2; // -1 means END, I made this loop to count the length of the file while (br.read() != -1) { f++; } byte[] array2 = new byte[f]; String sCurrentLine;

        while ((sCurrentLine = br.readLine()) != null) {
                       line2 = sCurrentLine.getBytes();
                    for(byte s : line2){
                    array2[k]=s;

                   k++;}
                    }
            return array2;
          }

this work with me BUT please can any one tell me which one better this one or the other one OR the other one which provided by "fge" Because i hope to take the best and thank you for all.

Yusra
  • 1
  • 2