Java 从二进制文件中转换字符

标签 java file methods

我查看了一些以前关于二进制文件的线程,并且我正在像它所说的那样执行数据流,但我不确定为什么我的不工作,因为我认为我正在做与线程所说的相同的事情。我的目标是创建一种方法,该方法接受带有移位整数的 .bin 格式的文件名。我将创建一个 .bin 类型的新文件,并移动字符。不过,只有大写或小写字母会被移动。我不知道正在读入的二进制文件的长度,并且需要遍历所有字符。但该文件只有 1 行。我有一个方法可以提供该行上的字符数,还有一个方法可以创建文件。我知道的程序确实正确创建了文件。无论如何,发生的事情是它创建文件,然后给我一个关于该行的 EOF 异常: char currentChar=data.readChar();

这是我的代码:

private static void cipherShifter(String file, int shift) {
        String newFile=file+"_cipher";
        createFile(newFile);
        int numChar;
        try {
            FileInputStream stream=new FileInputStream(file);
            DataInputStream data=new DataInputStream(stream);

            FileOutputStream streamOut=new FileOutputStream(newFile);
            DataOutputStream dataOut=new DataOutputStream(streamOut);
            numChar=readAllInts(data);
            for (int i=0;i<numChar;++i) {
                char currentChar=data.readChar();
                if (((currentChar>='A')&&(currentChar<='Z'))||((currentChar>='a')&&(currentChar<='z'))) {
                    currentChar=currentChar+=shift;
                    dataOut.writeChar(currentChar);
                }
                else {
                    dataOut.writeChar(currentChar);
                }

            }
            data.close();
            dataOut.flush();
            dataOut.close();
        } catch(IOException error) {
            error.printStackTrace();
        }
    }

    private static void createFile(String fileName) {
        File file=new File(fileName);
        if (file.exists()) {
            //Do nothing
        }

        else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                //Do nothing
            }
        }
    }

    private static int readAllInts(DataInputStream din) throws IOException {
        int count = 0; 
        while (true) { 
            try { 
                din.readInt(); ++count; 
            } catch (EOFException e) { 
                return count; 
            } 
        }
    }

所以我认为不应该发生这个错误,因为我确实有正确的数据类型,并且我告诉它只读取一个字符。任何帮助都会很棒。提前致谢。

最佳答案

根据上面的描述,您的错误是在 data.readChar() 方法调用时报告的,而不是在 readAllInts 方法内报告的。我模拟了您的错误附近的代码,并在同一位置的文本文件上得到了相同的异常。

我使用 readByte 方法一次读取一个字节,因为您主要对 ASCII 字节感兴趣。我还将 readAllInts 更改为 readAllBytes,以便我使用总字节数。

private static void cipherShifter(String file, int shift) {
        String newFile=file+"_cipher";
        createFile(newFile);
        int numChar;
        try {
            FileInputStream stream=new FileInputStream(file);
            DataInputStream data=new DataInputStream(stream);

            FileOutputStream streamOut=new FileOutputStream(newFile);
            DataOutputStream dataOut=new DataOutputStream(streamOut);
            numBytes=readAllBytes(data);
            stream.close();
            data.close();
            stream=new FileInputStream(file);
            data=new DataInputStream(stream);
            for (int i=0;i<numBytes;++i) {
                byte currentByte=data.readByte();
                if (((currentByte>=65)&&(currentByte<=90))||((currentByte>=97)&&(currentByte<=122))) {
                    currentByte=currentByte+=shift; //need to ensure no overflow beyond a byte
                    dataOut.writeByte(currentByte);
                }
                else {
                    dataOut.writeByte(currentByte);
                }

            }
            data.close();
            dataOut.flush();
            dataOut.close();
        } catch(IOException error) {
            error.printStackTrace();
        }
    }

    private static void createFile(String fileName) {
        File file=new File(fileName);
        if (file.exists()) {
            //Do nothing
        }

        else {
            try {
                file.createNewFile();
            } catch (IOException e) {
                //Do nothing
            }
        }
    }

    private static int readAllBytes(DataInputStream din) throws IOException {
        int count = 0; 
        while (true) { 
            try { 
                din.readByte(); ++count; 
            } catch (EOFException e) { 
                return count; 
            } 
        }
    }

关于Java 从二进制文件中转换字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49042247/

相关文章:

java - 如何在具有其他 Java 版本的计算机上安装 Java 8

java - 构建类来创建 JFreeChart,如何将其添加到主界面中的 JPanel 中?

python - 从 Python 的输入流加载图像

c# - 使用泛型方法时如何正确约束相关类类型?

java - 为什么我传递给 Arrays.sort 的方法引用需要是静态的?

java - Eclipse 替换两行相同的行

java - 正则表达式删除空格

file - Qt:下载文件不起作用

bash:对于文件是否存在?

android - 如何读取类(class)的返回 - android