Home > OS >  Java get File from enum class
Java get File from enum class

Time:01-19

I'm trying to get the File from enum in Java, like whenever I type DBIO.DBFOLDER, it should translate to a File i specified. Is there any way to do so?

package test.Test;

import java.io.File;

public enum DBIO{
    DBFOLDER(),DBFILE();
    public File DBFOLDER(){
        return new File("project/DBFolder");
    }
    public File DBFILE(){
        return new File(DBFOLDER() "/DBFile");
    }
}

Above code doesn't work; I just made it to show what I want it to do.

CodePudding user response:

I'm not sure I'd be using a enum for this use case, but, you need to associate a value with each enum case, for example...

public enum DBIO {
    DBFOLDER(new File("project/DBFolder")), DBFILE(new File(DBIO.DBFOLDER.getPath(), "DBFile"));

    private File file;
    
    private DBIO(File file) {
        this.file = file;
    }
    
    public File getPath() {
        return file;
    }
    
}

So if you were to do something like...

System.out.println(DBIO.DBFOLDER.getPath());
System.out.println(DBIO.DBFILE.getPath());

It would print...

project/DBFolder
project/DBFolder/DBFile

CodePudding user response:

you can use the valueOf() method to get the Enum value that corresponds to a given String. If you want to get the File associated with an Enum, you will need to store the File as a field in the Enum class and provide a getter method to access it.

Here is an example of an Enum class that stores a File and provides a getter method to access it:

public enum FileEnum {
    FILE_1("file1.txt", new File("file1.txt")),
    FILE_2("file2.txt", new File("file2.txt"));

    private final String name;
    private final File file;

    FileEnum(String name, File file) {
        this.name = name;
        this.file = file;
    }

    public File getFile() {
        return file;
    }
}

You can then use the getter method to access the file associated with the Enum, like this

File file = FileEnum.FILE_1.getFile();

CodePudding user response:

You can do something like this:

import java.io.File;

interface Get< T > {
    T get();
}

enum DBio {
    DBFOLDER( new Get< File >() {
        public File get() {
            return new File( "project/DBFolder" );
        }
    } ),

    DBFILE( new Get< File >() {
        public File get() {
            return new File( DBio.DBFOLDER.get()   "/DBFile" );
        }
    } );

    private Get< File > fileLoader;

    DBio( Get< File > fileLoader ) {
        this.fileLoader = fileLoader;
    }

    public File get() {
        return this.fileLoader.get();
    }

    public File DBFOLDER() {
        return new File( "project/DBFolder" );
    }

    public File DBFILE() {
        return new File( DBFOLDER()   "/DBFile" );
    }
}

We use the anonymous class of the Get interface to load the file only when requested.

If we pass directly, the file will be loaded once and kept in memory, always sharing a reference.

You could replace the anonymous class with a lambda:

// ... rest of code equals

DBFOLDER(() => new File( "project/DBFolder" )),
DBFILE(() => new File( DBio.DBFOLDER.get()   "/DBFile" ))

// ... rest of code equals
  • Related