Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java从二进制文件中移动字符_Java_File_Methods - Fatal编程技术网

Java从二进制文件中移动字符

Java从二进制文件中移动字符,java,file,methods,Java,File,Methods,我看了一些以前关于二进制文件的线程,我正在做它所说的数据流,但我不确定为什么我的线程不能工作,因为我想我正在做的事情和线程所说的一样。我的目标是创建一个方法,该方法接受带有移位整数的.bin格式的文件名。我将创建一个.bin类型的新文件,并将字符移位。不过,将只移动大写或小写字母。我不知道正在读取的二进制文件的长度,需要遍历所有字符。但该文件只有一行。我有一个方法可以告诉我那一行的字符数,还有一个方法可以创建一个文件。我知道的程序确实正确地创建了文件。无论如何,它创建了文件,然后给了我一个关于行

我看了一些以前关于二进制文件的线程,我正在做它所说的数据流,但我不确定为什么我的线程不能工作,因为我想我正在做的事情和线程所说的一样。我的目标是创建一个方法,该方法接受带有移位整数的.bin格式的文件名。我将创建一个.bin类型的新文件,并将字符移位。不过,将只移动大写或小写字母。我不知道正在读取的二进制文件的长度,需要遍历所有字符。但该文件只有一行。我有一个方法可以告诉我那一行的字符数,还有一个方法可以创建一个文件。我知道的程序确实正确地创建了文件。无论如何,它创建了文件,然后给了我一个关于行的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; 
            } 
        }
    }
private静态无效密码移位器(字符串文件,int移位){
字符串newFile=file+“\u cipher”;
创建文件(newFile);
内努姆查尔;
试一试{
FileInputStream=新的FileInputStream(文件);
DataInputStream数据=新的DataInputStream(流);
FileOutputStream streamOut=新FileOutputStream(新文件);
DataOutputStream dataOut=新的DataOutputStream(streamOut);
numChar=读取内容(数据);

对于(inti=0;i='A')&&(currentChar='A'))&&(currentChar看起来您得到的是EOFEException,因为您正在将DataInputStream对象传递给ReadAllings方法,读取流,然后在for循环中再次尝试从中读取。问题是跟踪流中位置的指针已经接近流的末尾(或在它的末尾)当readAllInts返回时。我怀疑它已经接近尾声了,而不是在尾声,因为readChar()方法正在立即抛出EOFEException,当它只读取它期望在命中EOF之前能够读取的两个字节中的一个时,它就会这样做

为了解决这个问题,您可以在将读取器传递给ReadAllings方法之前调用data.mark(),然后在该方法返回后调用data.reset();这将把指针重新指向流的开头。(假设data.markSupported()为true。)

您还有上面提到的问题,您的计数器一次读取四个字节,而您的字符读取器一次读取两个字节。您建议的将readAllInts返回值加倍的方法会有所帮助(您也可以使用readChar()而不是readInt()


您仍然需要考虑如何处理奇数字节长的二进制文件。有多种方法可以处理这种情况。今晚我太累了,无法编写代码示例,但是如果您明天仍然无法完成,请添加一条注释,我将看看我能做些什么来帮助您。

看起来您得到了EOFEException,因为您正在将DataInputStream对象传递给ReadAllings方法,读取流,然后在for循环中再次尝试从流中读取。问题是跟踪流中位置的指针已经接近流的末尾(或在流的末尾)当readAllInts返回时。我怀疑它接近尾声,而不是在尾声,因为readChar()方法正在立即抛出EOFEException,当它只读取它期望在命中EOF之前能够读取的两个字节中的一个时,它就会立即抛出EOFEException

为了解决这个问题,您可以在将读取器传递给ReadAllings方法之前调用data.mark(),然后在该方法返回后调用data.reset();这将把指针重新指向流的开头。(假设data.markSupported()为true。)

您还有上面提到的问题,您的计数器一次读取四个字节,而您的字符读取器一次读取两个字节。您建议的将readAllInts返回值加倍的方法会有所帮助(您也可以使用readChar()而不是readInt()


您仍然需要考虑如何处理奇数字节长的二进制文件。有多种方法可以处理这种情况。今晚我太累了,无法编写代码示例,但如果您明天仍然无法完成,请添加一条注释,我将看看我能做些什么来帮助您。

基于上述描述,您可以r错误是在data.readChar()方法调用时报告的,而不是在ReadAllings方法中报告的。我模拟了错误附近的代码,在同一位置的文本文件上得到了相同的异常

我使用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; 
            } 
        }
    }
private静态无效密码移位器(字符串文件,int移位){
字符串newFile=file+“\u cipher”;
创建文件(newFile);
内努姆查尔;
试一试{
FileInputStream=新的FileInputStream(文件);
DataInputStream数据=新的DataInputStream(流);
FileOutputStream streamOut=新FileOutputStream(新文件);
DataOutputStream dataOut=新的DataOutputStream(streamOut);
numBytes=readAllBytes(数据);
stream.close();
data.close();
流=新文件输入流(文件);
数据=新数据输入流(流);

对于(int i=0;i=65)&&&(currentByte=97)&&&(currentByte,基于上述描述,您的错误在data.readChar()方法调用中报告,而不是在readAllings方法中报告。我模拟了错误附近的代码,并在同一位置的文本文件上得到了相同的异常

我使用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; 
            } 
        }
    }