问题
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;
}
回答1:
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.
回答2:
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.
回答3:
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 ofbyte
s into a sequence ofchar
s (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 ofchar
s into a sequence ofbyte
s.
来源:https://stackoverflow.com/questions/22576430/method-read-from-file-using-java-language