0

.xtt我正在执行一个加密算法,我需要您的帮助,以便在 Java 中写入和读取文件。作为加密的一部分,我基本上需要将 Base64 编码的字节写入.txt文件并读取这些确切的字节,对其进行解码并使用它们来执行解密过程。

与我写入.txt文件的内容相比,我似乎正在阅读不同的内容。基本上,当我检查我正在写入文件的字节数组时,它被读取为[B@56e5b723但是当我从文件中读取它时,它会生成[B@35a8767.

这是我的 Java 控制台中打印的结果:

***Numbs converted to ByteArray is as follows: [B@56e5b723
Size of NumbsByteArray is: 10

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x

6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): [B@56e5b723

6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: /Users/anmonari/Desktop/textfiletwo.txt

bs.write(encoded); HAS BEEN CALLED

-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

-x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x

fileData read as bytes is: [B@35a8767

Size of fileData is: 10

fileDataString when converted to a string using String object is����������

fileDataString when converted to a string using fileDataStringTwo.toString()[B@35a8767

fileDataString.getBytes(); is: [B@2c6f7ce9

-x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x***

下面是我的代码:

package com.writeandreadfromfile;

import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class WriteAndRead {

    public static void main(String j[]) {

        String Numbs = "6,7,8,9,10";

        byte[] NumbsByteArray = Numbs.getBytes();

        System.out.println("Numbs converted to ByteArray is as follows: " + NumbsByteArray);
        System.out.println("Size of NumbsByteArray is: " + NumbsByteArray.length);

        System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

        WriteByteArrayToFile(NumbsByteArray);

        System.out.println("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");

        try {
            ReadByteArrayFromFile();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    // Write ByteArray To File
    public static void WriteByteArrayToFile(byte[] NumbsByteArray) {
        System.out.println("\n-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS STARTED -x-x-x-x-x");

        System.out.println("6,7,8,9,10 has been received as a byte array in WriteByteArrayToFile(byte[] encoded): " + NumbsByteArray);

        String fileName = "/Users/anmonari/Desktop/textfiletwo.txt";

        System.out.println("6,7,8,9,10 IS TO BE WRITTEN TO THE FILE: " + fileName);

        BufferedOutputStream bs = null;

        try {

            FileOutputStream fs = new FileOutputStream(new File(fileName));
            bs = new BufferedOutputStream(fs);
            bs.write(NumbsByteArray);
            System.out.println("bs.write(encoded); HAS BEEN CALLED");
            bs.close();
            bs = null;

        } catch (Exception e) {
            e.printStackTrace();
        }

        if (bs != null) try { bs.close(); } catch (Exception e) {}

        System.out.println("-x-x-x-x-x WriteByteArrayToFile(byte[] encoded) HAS ENDED -x-x-x-x-x");
    }

    // Read ByteArray To File
    public static void ReadByteArrayFromFile() throws IOException {
        // Create FileInputStream and feed it the file name
        System.out.println("-x-x-x-x-x ReadByteArray() HAS STARTED -x-x-x-x-x");

        File file;

        try {
            file = new File("/Users/anmonari/Desktop/textfiletwo.txt");

            // Create the object of DataInputStream
            DataInputStream in = new DataInputStream((new FileInputStream(file)));

            byte[] fileData = new byte[(int)file.length()];

            System.out.println("fileData read as bytes is from file: "  + fileData);

            System.out.println("Size of fileData is: " + fileData.length);

            //String fileDataString = in.readLine();

            String fileDataString = new String(fileData);
            System.out.println("fileDataString when converted to a string using String object is"  + fileDataString);

            String fileDataStringTwo = fileData.toString();
            System.out.println("fileDataString when converted to a string using fileDataStringTwo.toString()" + fileDataStringTwo);

            fileDataString.getBytes();
            System.out.println("fileDataString.getBytes(); is: "  + fileDataString.getBytes());

            //Close the input stream
            in.close();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.out.println("-x-x-x-x-x ReadByteArray() HAS ENDED -x-x-x-x-x");

    }
}

对于如何从文件中读取您写入文件的确切字节数组的任何帮助,我们将不胜感激!

4

3 回答 3

1

您没有打印字节数组的内容。您正在输出它们的类型和 hashCode(toString()数组方法的结果)。

要输出字节数组的内容,请使用

System.out.println(java.util.Arrays.toString(array));
于 2013-03-11T21:12:42.683 回答
0

除了 Sotirios 指出的(你不是从 读取in)之外,你正在将字节数组写为文本,“6,7,8,9,10”,但是你正在调用fileDataString.getBytes();. 这将获取字符串的字节,其中(假设 UTF-8)将为0x36 0x2c 0x37 0x2c 0x38 0x2c 0x39 0x2c 0x31 0x30.

如果您的计划是文件将是文本,则需要将字符串解析回字节数组。也许像

String[] numbers = fileDataString.split();
byte[] bytes = new byte[numbers.length];
for (int i = 0; i < numbers.length; i++) {
    bytes[i] = Byte.parseByte(numbers[i]);
}

另一方面,如果您只需要保存和恢复字节数组,则只需使用ObjectOutputStreamObjectInputStream就可以更轻松。

于 2013-03-11T21:27:59.577 回答
0

您可以尝试 bs(fs).flush() 缓冲所有写出的内容

于 2013-03-11T21:14:27.950 回答