简体   繁体   中英

problems reading 2d byte array from a text file

I'm having problems reading a byte array back from a text file that I had already written the byte array to.

Here is the code for writing the byte array to the file

    FileOutputStream fout = new FileOutputStream(new File("C:\\Users\\lvannini\\Desktop\\FileSystem\\diskFiles\\", fileName));
    for(int i = 0; i < 64; ++i){
        for(int j = 0; j < 64; ++j)
            fout.write(String.valueOf(ldisk[i][j]).getBytes());
        fout.write(System.lineSeparator().getBytes()); //write line break;

As far as I'm aware, this part is working correctly, because my output text file contains the appropriate contents of the 2d array I am using. An example of the first 9 lines of the text file are printed as follows:

00-1-16000000000000000000000000000000000000000000000000000000000000
00024000700000000000000090000000000071000100001100000000000800000000
-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000
-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000
-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000
-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000
-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000-1-1-1-1000000000000
10010010000001979899000021021111110000900000000000000010000000000000000800000000
0000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000

However, using this following code to read back from the file into the 2D array:

FileInputStream fin = new FileInputStream("C:\\Users\\lvannini\\Desktop\\FileSystem\\diskFiles\\" + fileName);
    for(int i = 0; i < 64; ++i)
        fin.read(ldisk[i]);

    fin.close();

gives me the actual ASCII values of each byte being read in. For instance, after reading in the first line of the text file, my 2d array, ldisk[0], contains:

48 48 45 49 45 49 54 48 48 48 . . .

Where am I losing the translation from the byte values to the ascii values? I'm suspecting I'm saving the byte values to the text file incorrectly. Any advice/responses are very much appreciated! Thanks again.

Where am I losing the translation from the byte values to the ASCII values?

FileInputStream.read() is giving you the ASCII code for the byte. See ASCII table , which reveals you're not losing the translation.

Just cast that ASCII code to a character code:

char c = (char)fin.read(ldisk[i]);

OR subtract 48 from it to get the integer value:

-48 + fin.read(ldisk[i])

You still have to deal with the minus signs taking up bytes, so your

for(int i = 0; i < 64; ++i)

is inadequate with 64 as limit when there are minus signs in the file.

Your problem is with FileInputStream. There are many ways get ASCII values of each byte being read but the easiest is probably BufferedReader with defined charset.

Charset charset = Charset.forName("US-ASCII");
BufferedReader in = Files.newBufferedReader(C:\\Users\\lvannini\\Desktop\\FileSystem\\diskFiles\\" + fileName, charset);

String line;
while((line = in.readLine()) != null)
{
   //do something with your input
}
in.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM