Results 1 to 4 of 4
  1. #1
    Keesjuh is offline Member
    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Default Logic error in file creation

    Hello all,

    Iīm trying to make a program that opens a file which contains multiple .jpg images in a row (so the file canīt be opened) and I have to seperate those files and save them into individual files. Iīm (I think) almost finished but there is a logic error in the file creation bit. I does not create a new file but it does execute the 2 System.out.println(list[0]) which I used to see if that part of the loop was being executed.Can someone help me out?

    Java Code:
    public static void main(String[] args) {
            int cntr = 0;
            int[] list = new int[10];
            int arraycntr = 0;
    
            try {
                DataInputStream is = new DataInputStream(new FileInputStream("D:\\Documents\\NetBeansProjects\\ByteCheck\\build\\classes\\bytecheck\\potato.jpg"));
    
                while (is.available() > 0) {
                    try {
                        if (check(is)) {
                            list[arraycntr] = cntr;
                            arraycntr++;
                            cntr++;
                            is.readByte();
                        } else {
                            cntr++;
                            is.readByte();
                        }
                    } catch (IOException e) {
    
                        int i;
                        int j;
                        File f;
                        byte ch;
    
                        for (i = 0; i == list.length - 1; i++) {
                            f = new File("D:\\Documents\\NetBeansProjects\\ByteCheck\\build\\classes\\bytecheck\\afbeelding" + i + ".jpg");
                            f.mkdirs();
                            f.createNewFile();
                            DataOutputStream out = new DataOutputStream(new FileOutputStream("D:\\Documents\\NetBeansProjects\\ByteCheck\\build\\classes\\bytecheck\\afbeelding" + i + ".jpg"));
                            System.out.println("File created: afbeelding" + i);
                            for (j = 0; j == list[i]; j++) {
                                try {
                                    ch = is.readByte();
                                    out.writeByte(ch);
                                } catch (SecurityException d) {
                                    System.err.println("Caught SecurityException: A file already exists: afbeelding" + i);
                                }
                            }
    
                        }
                        System.out.println(list[0]);
                        System.out.println(list[1]);
                    }
                }
            } catch (IOException e) {
                System.err.println("Caught IOException: " + e.getMessage());
            }
        }

  2. #2
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Logic error in file creation

    What's the purpose of this for-loop:
    Java Code:
    for (i = 0; i == list.length - 1; i++)

  3. #3
    Keesjuh is offline Member
    Join Date
    Jul 2013
    Posts
    8
    Rep Power
    0

    Default Re: Logic error in file creation

    The purpose of that loop is to make sure that all bytes of one img are stored under the same file. The inner loop copies all bytes to the new file, when it reaches the border (location in the input stream) where the new img starts, the outer loop will perform i++ and then a second file will be created. The i == list.length-1 isnīt exactly correct but it is fine for the test.

  4. #4
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default Re: Logic error in file creation

    Quote Originally Posted by Keesjuh View Post
    The purpose of that loop is to make sure that all bytes of one img are stored under the same file. The inner loop copies all bytes to the new file, when it reaches the border (location in the input stream) where the new img starts, the outer loop will perform i++ and then a second file will be created. The i == list.length-1 isnīt exactly correct but it is fine for the test.
    Actually, that exact part is the thing that's causing it to... well, not work. First i is set to 0. It then checks if 0 == 10-1. Seeing that it isn't, it exits the loop without it being done once. Remember that the middle statement is "while", not "until".

Similar Threads

  1. "Access is denied" error while file creation
    By Bharath_M in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2011, 12:23 PM
  2. PDF File creation Error
    By siva_vs_siva in forum New To Java
    Replies: 2
    Last Post: 12-24-2010, 12:42 AM
  3. help with a logic error
    By ShinTec in forum Advanced Java
    Replies: 11
    Last Post: 05-02-2010, 10:19 PM
  4. Logic Error: Not Writing To File
    By JDCAce in forum Advanced Java
    Replies: 6
    Last Post: 10-21-2008, 02:13 AM
  5. Replies: 3
    Last Post: 10-06-2008, 11:03 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •