3

These are the related questions that might cause my question to be closed, even though I specify another question:

And now to the point. I've got a file with a specific extension, to be more exact it's .nbs. I want to create a file and then write the specific data to it.
That might have sounded vague so let me show you the code I have started with.

try {
    File bpdn = new File(getDataFolder() + "song.nbs");
    if (!bpdn.exists()) {
        bpdn.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(bpdn);

} catch (IOException e) {
    e.printStackTrace();
}

I'll provide you even more details. So I've got a song.nbs file that I have created in the past, for myself. And now, whenever a person runs my application, I want it so there's a new song.nbs file with the exact contents of a file that I have on my PC right now. Therefore, I need to somehow get the bytes of my existing song.nbs and then copy and paste them in my Java application... or is it the way? I neither know how to get the bytes of my own file right now, nor do I know how to write them.

LeopardL GD
  • 139
  • 13

2 Answers2

3

You need to create a resources folder. More info here.

Assuming your project structure is

ProjectName/src/main/java/Main.java

you can create a resources folder inside main/:

ProjectName/src/main/java/Main.java
ProjectName/src/main/resources/

Move your song.nbs you want to read inside resources/:

ProjectName/src/main/java/Main.java
ProjectName/src/main/resources/song.nbs

Now, get the InputStream of song.nbs stored there:

final ClassLoader classloader = Thread.currentThread().getContextClassLoader();
final InputStream is = classloader.getResourceAsStream("song.nbs");

Then, write this input stream to your new file:

final File bpdn = new File(getDataFolder() + "song.nbs");
if (!bpdn.exists()) bpdn.createNewFile();
final FileOutputStream os = new FileOutputStream(bpdn);

byte[] buffer = new byte[1024];
int read;
while ((read = is.read(buffer)) != -1) {
    os.write(buffer, 0, read);
}
enzo
  • 9,861
  • 3
  • 15
  • 38
  • I don't think you understood. I can't input anything. I just have a file that I want to clone and create whenever a user starts my application. I think I came up with a solution though – LeopardL GD Jun 26 '21 at 17:22
  • 2
    I think *you* didn't understand my answer. Just put the file A you want to copy in the *resources* folder, get its bytes using `InputStream`, create the file B you want to receive the copy and write the original file bytes to it using `OutputStream`. – enzo Jun 26 '21 at 17:25
  • 1
    enzo, perhaps you can explain that the resource file will be (should be) compiled into the application's JAR file and won't need to be distributed separately. – John Kugelman Jun 26 '21 at 17:27
  • Yes, you're correct. The file you want to copy will be available to be read using `ClassLoader` whenever the user runs your application. You can read more about it in [Accessing Resources](https://docs.oracle.com/javase/8/docs/technotes/guides/lang/resources.html). – enzo Jun 26 '21 at 17:31
-1

I think I came up with a solution, but I am not sure if this is works. I'd appreciate if you would take a look.

try {
    File bpdn = new File(getDataFolder() + "badpiggiesdrip.nbs");
    if (!bpdn.exists()) {
        bpdn.createNewFile();
    }

    FileOutputStream fos = new FileOutputStream(bpdn);
    fos.write(new byte[] {
            Byte.parseByte(Arrays.toString(Base64.getDecoder().decode(Common.myString)))
    });

} catch (IOException e) {
    e.printStackTrace();
}

Common.myString is just a string, that contains data of this type:

(byte) 0x21, (byte) 0x5a, .....

and it's encoded in Base64.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
LeopardL GD
  • 139
  • 13