10 Replies - 730 Views - Last Post: 17 May 2011 - 03:00 AM Rate Topic: -----

#1 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Adding and Deleting Files

Posted 12 May 2011 - 09:23 PM

So this is so much of a coding problem as it is a conceptual problem. I'm not sure how to go about doing this...

I'm working on a program that will have a bunch of pictures in a folder and will randomly pick one to show up on a button push. The pictures don't repeat until all have been viewed at least once. I'm making it for my teacher as a way to make sure everyone will have to answer a question in a given class period but make it fun. Anyway, I'm not quite sure how I would go about picking the images, adding and deleting, and then I'm looking for any kind of suggestions you guys would have to help me out.

I'm not sure if that was clear, but it's basically how I would go about doing the background file management that I'm stumped on. I haven't worked with files to this extent much and I've never used a JFileChooser before which is what I'm assuming I'll have to use.

This is what the GUI looks like so far...it's far from done so don't judge.
Attached Image

Any help would be greatly appreciated...I'm stuck here...

This post has been edited by zim1985: 12 May 2011 - 09:30 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Adding and Deleting Files

#2 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Adding and Deleting Files

Posted 12 May 2011 - 09:47 PM

If the files are being randomly chosen, then you will not need to use a JFileChooser - that class is used to let the user pick one or more files.

If you have all of these pictures in a single folder, then your job is easy. You can use the File class to get an array of all files in that folder.

Once you have that info, you will want to make sure that each picture is displayed exactly once. Search the forums/tutorials for how to shuffle an array - this will let you put those image files in random order, which means you can just iterate through it to have a non-repeated sequence.

This is the same way you would go about dealing cards from a single deck.
Was This Post Helpful? 1
  • +
  • -

#3 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Adding and Deleting Files

Posted 12 May 2011 - 09:52 PM

View Postmoobler, on 12 May 2011 - 09:47 PM, said:

If the files are being randomly chosen, then you will not need to use a JFileChooser - that class is used to let the user pick one or more files.

If you have all of these pictures in a single folder, then your job is easy. You can use the File class to get an array of all files in that folder.

Once you have that info, you will want to make sure that each picture is displayed exactly once. Search the forums/tutorials for how to shuffle an array - this will let you put those image files in random order, which means you can just iterate through it to have a non-repeated sequence.

This is the same way you would go about dealing cards from a single deck.

I think I understand that for the most part. I need the functionality to add files to the program from the computer and store them in some kind of file system. That's what I need the JFileChooser for. That structuring is what I'm having issues with. How to set that up and deal with deleting files and adding files to that system of folders that I imagine I'll probably have to make.
Was This Post Helpful? 0
  • +
  • -

#4 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Adding and Deleting Files

Posted 12 May 2011 - 09:59 PM

Ahh, I think I get it now...

In that case, there are a few ways you can handle it, depending on the environment and how you want to store data.

Once the user selects one or more files to be used by the program, you can...
1) Physically copy the images to a folder used by your program. This solution is probably the best if you will have a small number of images or plenty of disk space to store them all.

2) Just store the locations of the selected files. This is a good choice if you will have hundreds or thousands of images chosen, but you will have no guarantee that those files will still be there when you try to read them later.


Is that what you meant?
Was This Post Helpful? 1
  • +
  • -

#5 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Adding and Deleting Files

Posted 12 May 2011 - 10:04 PM

View Postmoobler, on 12 May 2011 - 09:59 PM, said:

Ahh, I think I get it now...

In that case, there are a few ways you can handle it, depending on the environment and how you want to store data.

Once the user selects one or more files to be used by the program, you can...
1) Physically copy the images to a folder used by your program. This solution is probably the best if you will have a small number of images or plenty of disk space to store them all.

2) Just store the locations of the selected files. This is a good choice if you will have hundreds or thousands of images chosen, but you will have no guarantee that those files will still be there when you try to read them later.


Is that what you meant?

Yeah, option 1 is what I'm looking for. The max amount of pictures is probably like 60, so that seems perfect. I just have no idea what kind of classes and the kind of things I'd have to do to set this up. Like I just need some guidance on how I would start doing this. I can do the file reading and deleting, that's easy stuff. It's mostly organizing it and how to add and store it that I'm stuck on.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2675
  • View blog
  • Posts: 11,296
  • Joined: 20-September 08

Re: Adding and Deleting Files

Posted 13 May 2011 - 01:16 AM

Why don't you let the user pick a directory (filtered so that only the appropriate directories are shown) and then have the app choose n random files from it?
Was This Post Helpful? 0
  • +
  • -

#7 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Adding and Deleting Files

Posted 13 May 2011 - 10:44 AM

View Postg00se, on 13 May 2011 - 01:16 AM, said:

Why don't you let the user pick a directory (filtered so that only the appropriate directories are shown) and then have the app choose n random files from it?

I'm not sure how this would help with my problem? :/
Was This Post Helpful? 0
  • +
  • -

#8 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: Adding and Deleting Files

Posted 13 May 2011 - 12:29 PM

View Postzim1985, on 13 May 2011 - 01:04 AM, said:

Yeah, option 1 is what I'm looking for. The max amount of pictures is probably like 60, so that seems perfect. I just have no idea what kind of classes and the kind of things I'd have to do to set this up. Like I just need some guidance on how I would start doing this. I can do the file reading and deleting, that's easy stuff. It's mostly organizing it and how to add and store it that I'm stuck on.


Sorry again for misunderstanding. Let's try again...

As far as the structure of your code goes, it looks like you have a reasonable mock-up of the GUI, so we don't need to be concerned with that.

I would start off with an ImageManager class. In this class you can put all method that would interact with picture files: copying from their source to the image folder, deleting a file from the image folder, getting a random file from the folder, reading an image file into a displayable Image object, etc.

Once you have that class (even if some of the methods are not implemented and left as empty 'skeletons'), you can start calling methods from it from your GUI code.
Was This Post Helpful? 2
  • +
  • -

#9 zim1985  Icon User is offline

  • Grand Inquisitor
  • member icon

Reputation: 74
  • View blog
  • Posts: 568
  • Joined: 19-February 10

Re: Adding and Deleting Files

Posted 16 May 2011 - 10:35 AM

View Postmoobler, on 13 May 2011 - 12:29 PM, said:

View Postzim1985, on 13 May 2011 - 01:04 AM, said:

Yeah, option 1 is what I'm looking for. The max amount of pictures is probably like 60, so that seems perfect. I just have no idea what kind of classes and the kind of things I'd have to do to set this up. Like I just need some guidance on how I would start doing this. I can do the file reading and deleting, that's easy stuff. It's mostly organizing it and how to add and store it that I'm stuck on.


Sorry again for misunderstanding. Let's try again...

As far as the structure of your code goes, it looks like you have a reasonable mock-up of the GUI, so we don't need to be concerned with that.

I would start off with an ImageManager class. In this class you can put all method that would interact with picture files: copying from their source to the image folder, deleting a file from the image folder, getting a random file from the folder, reading an image file into a displayable Image object, etc.

Once you have that class (even if some of the methods are not implemented and left as empty 'skeletons'), you can start calling methods from it from your GUI code.

Hmmmm that sounds like it will work. I suppose I'll work on this and post back again if I need help from there. Much appreciated, my friend.
Was This Post Helpful? 0
  • +
  • -

#10 ceyesumma  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 14-December 10

Re: Adding and Deleting Files

Posted 16 May 2011 - 07:00 PM

I have examples of two classes that will create dirs and or copy
everything from one dir to another. You will have to canableize it
to make it work for you.

the code I was most impressed with in the way of convention

is how these methods work together. You should mold the program
so that fileSystemDir can be any file and folderlocation changes too.
Hope this helps
public boolean bExists() {
        boolean bExists = false;

        File fileSystemDir = new File(folderLocation);
        if (fileSystemDir.exists()) {
            bExists = true;
        }
        return bExists;
    }





public static String getFolderLocation() {
        return folderLocation;
    }

    public static void setFolderLocation(String folderLocation) {
        CreateSystemStartFolders.folderLocation = folderLocation;
    }


Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#11 ceyesumma  Icon User is offline

  • New D.I.C Head

Reputation: -2
  • View blog
  • Posts: 31
  • Joined: 14-December 10

Re: Adding and Deleting Files

Posted 17 May 2011 - 03:00 AM

I forgot to add another class I found that will copy all dir's from on dir to destination folders.



/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package model.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class CopyDirectory {
    private static boolean bDir;

   

    public CopyDirectory() {

       
    }

    public static boolean copyFolder(File src, File dest) throws IOException {

     
        bDir=true;

        if (src.isDirectory()) {

            //if directory not exists, create it
            if (!dest.exists()) {
                dest.mkdir();
                System.out.println("Directory copied from "
                        + src + "  to " + dest);
            }

            //list all the directory contents
            String files[] = src.list();

            for (String file : files) {
                //construct the src and dest file structure
                File srcFile = new File(src, file);
                File destFile = new File(dest, file);
                //recursive copy
                copyFolder(srcFile, destFile);
            }

        } else {
            //if file, then copy it
            //Use bytes stream to support all file types
            InputStream in = new FileInputStream(src);
            OutputStream out = new FileOutputStream(dest);

            byte[] buffer = new byte[1024];

            int length;
            //copy the file content in bytes
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }

            in.close();
            out.close();
            System.out.println("File copied from " + src + " to " + dest);
        }
        return bDir;
    }
}



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1