1

このプログラムでは、配列インデックスが範囲外の例外を取得しており、それがどこで発生しているかを知ることができませんでした. ご協力ありがとうございました

import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.*;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import java.math.BigInteger;


public class Decrypt extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    public static void main(String[] args) throws Exception {
    new Decrypt();
    }

    public Decrypt() throws Excenter code hereeption {
    setTitle("Color");
    setSize(300, 300);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(3);

    add(new PaintPanel(ImageIO.read(new File("src/outputImages/s2.bmp"))));
    setVisible(true);
    }

    class PaintPanel extends JPanel {

    /**
         * 
         */
        private static final long serialVersionUID = 1L;
    private BufferedImage image;

    PaintPanel(BufferedImage image) {
        this.image = image;
    }

    public void paintComponent(Graphics g) {
        File file = new File("src/input.txt");
        int length=0, insertCounter;
        String strFileContent="";
        try {
        //create FileInputStream object
        FileInputStream fin = new FileInputStream(file);

        /*
         * Create byte array large enough to hold the content of the file.
         * Use File.length to determine size of the file in bytes.
         */


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

        /*
         * To read content of the file in byte array, use
         * int read(byte[] byteArray) method of java FileInputStream class.
         *
         */
        fin.read(fileContent);

        //create string from byte array
            strFileContent = new String(fileContent);

        System.out.println("File content : "+ strFileContent );

        BigInteger bi = new BigInteger(fileContent);
        // Format to binary
        strFileContent = bi.toString(2);      // 100100000111111110000
        length=strFileContent.length();      //thia variable contains the length of binary string
        //System.out.println(length+" File content in binary: "+ strFileContent );
        }
        catch(Exception e){
        System.out.println("File not found" + e);
        }

        File file1 = new File("input.txt");
        byte outputBytes[] = new byte[(int)file1.length()];
        insertCounter=0;
        int localCounter=0;
        byte tempbyte=0x00;
        for (int h = 0; h <=image.getHeight()-3; h=h+3) {
        for (int w = 0; w <=image.getWidth()-3; w=w+3) {
            int whiteParity=0;
            for(int i=0;i<3;i++){
            for(int j=0;j<3;j++){
                int rgb = image.getRGB(w+j,h+ i);     
                if (rgb == -1)
                whiteParity++;
            }
            }
            /* if(WhitePArity==1 ||WhitePArity==3 ||WhitePArity==5 ||WhitePArity==7 ||WhitePArity==9){

               }*/

            if( image.getRGB(w+1,h+1)==-1){
            if(localCounter==0)
                tempbyte=(byte)(tempbyte|0x80);
            if(localCounter==1)
                tempbyte=(byte)(tempbyte|0x40);
            if(localCounter==2)
                tempbyte=(byte)(tempbyte|0X20);
            if(localCounter==3)
                tempbyte=(byte)(tempbyte|0x10);
            if(localCounter==4)
                tempbyte=(byte)(tempbyte|0x08);
            if(localCounter==5)
                tempbyte=(byte)(tempbyte|0x04);
            if(localCounter==6)
                tempbyte=(byte)(tempbyte|0x02);
            if(localCounter==7)
                tempbyte=(byte)(tempbyte|0x01);
            }
            localCounter++;

            if(localCounter==8){
            outputBytes[insertCounter]=tempbyte;
            insertCounter++;
            localCounter=0;
            tempbyte=(byte)(tempbyte & 0x00);
            }


        }

        String outputString=new String(outputBytes);
        String strFilePath = "output.txt";

        try{
            FileWriter fstream = new FileWriter(strFilePath);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(outputString);
            //Close the output stream
            out.close();
        }
        catch(Exception e){
            System.out.println("File not found" + e);
        }

        }

        g.drawImage(image, 0, 0, this);
    }
    }
}
4

1 に答える 1

3

コードに 2 つの配列が表示されます。

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

と:

byte outputBytes[] = new byte[(int)file1.length()];

両方の配列は、への呼び出しに基づいてサイズ設定されますFile.length()

Fileの JavaDoc から:

この抽象パス名が示すファイルの長さ (バイト単位)、またはファイルが存在しない場合は 0L。一部のオペレーティング システムは、デバイスやパイプなどのシステム依存エンティティを示すパス名に対して 0L を返す場合があります。

したがって、これらのファイルの 1 つが空であるか、存在しないと推測されます。

(例外メッセージだけでなく、完全なスタック トレースを投稿すると、問題をより明確に診断するのに役立ちます)。

于 2012-03-22T04:54:10.333 に答える