0

I am trying to write a program which will help manage some data. So far, the user can create a new file using a "New File" Dialogue. After the user has entered some parameters, I create a new instance of a "File" class. My questions now is: How can I store multiple files (like tabs) in memory. I was thinking about using some sort of ArrayList as a variable in an "Application" class. What would be an ideal approch here?

Fullk33
  • 230
  • 2
  • 9
  • If you intend to write objects to disk (why?) start here: https://docs.oracle.com/javase/tutorial/jndi/objects/serial.html – Reut Sharabani Jul 04 '16 at 08:18
  • no, I know how to store them to disk, but I need a way to handle them in memory while the program is running – Fullk33 Jul 04 '16 at 08:24
  • 1
    I don't get your question. Unless you write data into files, everything you do happens in memory. All your objects are memory only. – GhostCat Jul 04 '16 at 08:32
  • Maybe I didn't explain myself well enough. My problem is: how can I store my objects, so that I can accses them at a later point in time, whenever I want. I think the hashmap solution fits in quite well. – Fullk33 Jul 04 '16 at 09:19

2 Answers2

1

Yes, you can simply use List to store instances

int maxfiles = 150; //You can set the length of the list, by default is 10

List<File> files = new ArrayList<File>(maxfiles);

//Your multiple files here
File file1 = null;
File file2 = null;
File file3 = null;

//Simply add it to the List
files.add(file1);
files.add(file2);
files.add(file3);

//And remove it by
files.remove(0); //Index

//Or remove all
files.clear();
  • Nice answer with most operations given. For beginners in java: `maxfiles` is more or less irrelevant, it is the initial array size/capacity. You can leave it out. It has nothing to do with `size()`. – Joop Eggen Jul 04 '16 at 09:08
1

You can use a map to store your files in memory.

public class FileStorage{

    Map<String, File> fileMap;

    public FileStorage(){
        fileMap = new HashMap<String, File>();
    }

    public void addNewFile(File f, String fileName){
        if (fileMap.get(fileName) != null){
            // Do something if you do not want to erase previous file...
        else{
            fileMap.put(fileName, f);
        } 
    }

    public File getStoredFile(String fileName){
        File f = fileMap.get(fileName);
        if (f==null){
            // alert user the file is not found.
        }
        return f; 
    }

}

Then in your main class you can simply use the FileStorage class to manage your files

public static void main(String[] args){
    FileStorage fileStorage = new FileStorage();

    // User create a new file
    File file1 = new File();

    fileStorage.addNewFile(file1, "file1"); // name can also be file1.getName()

    [...]

    // Later you can access the file
    File storedFile = fileStorage.getStoredFile("file1");
}

The complexity to access or store file is O(1). It is better than a list for accessing a file.

L01c
  • 1,033
  • 10
  • 19