Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.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 读卡器返回值63?_Java_File_Binary_Writer_Reader - Fatal编程技术网

Java 读卡器返回值63?

Java 读卡器返回值63?,java,file,binary,writer,reader,Java,File,Binary,Writer,Reader,我试着用写程序在二进制文件中写随机数,然后用读卡器读出。到目前为止,一切顺利。但有时读卡器会显示63,而不是正确的数字。为什么程序向我显示值63 例如: 38 193 74 115 84 203 123 6 190 76 151 11 148 122 240 241 162 232 224 92 164 43 247 81 226 163 117 116 202 90 66 16 14 63 174 78 182 92 195 77 196 112 194 153 204 ===========

我试着用写程序在二进制文件中写随机数,然后用读卡器读出。到目前为止,一切顺利。但有时读卡器会显示63,而不是正确的数字。为什么程序向我显示值63

例如:
38 193 74 115 84 203 123 6 190 76 151 11 148 122 240 241 162 232 224 92 164 43 247 81 226 163 117 116 202 90 66 16 14 63 174 78 182 92 195 77 196 112 194 153 204
===============================
38 193 74 115 84 203 123 6 190 76 63 11 63 122 240 241 162 232 224 92 164 43 247 81 31 226 163 117 116 202 90 66 16 16 63 174 78 182 92 195 77 196 112 194 63 204

Random rand=new Random();
智力麻木;
File File=新文件(“randNumbers.txt”);
Writer fw=null;
尝试
{
fw=新文件编写器(文件);
对于(int i=0;i<100;i++)
{
numb=rand.nextInt(256);
系统输出打印(numb+“”);
fw.写(麻木);
}
System.out.println();
System.out.println(“=============写入========”);
}
捕获(IOE异常)
{
e、 printStackTrace();
}
捕获(例外e)
{
e、 printStackTrace();
}
最后
{
如果(fw!=null)
{
尝试
{
fw.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}
//从文件中读取随机数
读卡器fr=null;
尝试
{
fr=新文件读取器(文件);
对于(int i=0;i<100;i++)
{
System.out.print(fr.read()+);
}
}
捕获(IOE异常)
{
e、 printStackTrace();
}
最后
{
如果(fr!=null)
{
尝试
{
fr.close();
}
捕获(IOE异常)
{
e、 printStackTrace();
}
}
}

您将随机数视为系统默认字符集中的所有有效字符。事实并非如此

63是问号的数值,用于替换字符集中不表示任何有效字符的值

看起来您实际上并不想在文本文件中写入字符,而是想在二进制文件中写入随机字节。因此,您应该使用FileOutputStream进行写入,使用FileInputStream进行读取

    Random rand = new Random();
    int numb;
    File file = new File("randNumbers.txt");
    Writer fw = null;

    try
    {
        fw = new FileWriter(file);

        for(int i = 0; i < 100; i++)
        {
            numb = rand.nextInt(256);
            System.out.print(numb + " ");
            fw.write(numb);
        }
        System.out.println();
        System.out.println("======== Wrote ========");
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(fw != null)
        {
            try
            {
                fw.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }

    // read random numbers from file
    Reader fr = null;

    try
    {
        fr = new FileReader(file);

        for(int i = 0; i < 100; i++)
        {
            System.out.print(fr.read() + " ");
        }
    }
    catch(IOException e)
    {
        e.printStackTrace();
    }
    finally
    {
        if(fr != null)
        {
            try
            {
                fr.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        }
    }