-2

I am working in Java platform. I need to copy a file from the package to some folders in desktop. I am using input stream and output stream classes to do it, it is doing the job pretty well inside NetBeans.

The problem is, it's not copying the file while I am running the JAR file to test the application, and it is saying NULL.

       File source = new File("src/jrepo/css/bs.css");

       File dest = new File(ResultPath + "/css/bs.css");

        InputStream is = null;
        OutputStream os = null;
        try {
            is = new FileInputStream(source);
            os = new FileOutputStream(dest);
            byte[] buffer = new byte[1024];
            int length;
            while ((length = is.read(buffer)) > 0) {
                os.write(buffer, 0, length);
            }
        } finally {
            is.close();
            os.close();
        }
Abraham
  • 11
  • 4
  • My guess would be that you are using `File` or `FileInputStream` instead of reading the data as a resource. But without code, this can't be answered. – RealSkeptic Feb 06 '17 at 08:09
  • My proverbial money is on using relative paths and the working directory being different. But as mentioned; no help without code. – Biffen Feb 06 '17 at 08:12

2 Answers2

1

Your problem is with

new File("src/jrepo/css/bs.css");

The constructor for File(String) takes a full path to the file. You are using a relative path. If you are trying to read the file from the operating system, use the full path. If you are reading it from the jar file, then use this approach instead.

Community
  • 1
  • 1
JustinKSU
  • 4,875
  • 2
  • 29
  • 51
  • i have also tried that but it is still throwing NULL – Abraham Feb 07 '17 at 04:56
  • FileUtils.copyFile(new File("src/jrepo/css/bootstrap.css"), new File(ResultPath + "/css/bootstrap.css")); i have also tried this but the thing is it is working inside netbeans but when i try to access the jar it is not working so – Abraham Feb 07 '17 at 05:05
  • If you are trying to load a file from inside the jar, please follow the link I labeled as "this approach". Then once you have the bytes, writing to disk, should be trivial. – JustinKSU Feb 07 '17 at 19:32
  • yes i have tried that link also but still the same is happening – Abraham Feb 08 '17 at 05:14
  • The "src" directory is usually not included inside your jar/war. Try opening up the *ar file and finding the actual path to the file inside. – JustinKSU Feb 08 '17 at 18:07
0

I found the way since I am using JavaFX, there is a problem which stops the file copying of CSS files. In order to resolve that issue just change the run time settings of the project in Netbeans. Right click the title of the project→go to Properties→Build→Packaging→uncheck the Binary Encode JavaFX CSS files checkbox and then save the project and rebuild it.

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
Abraham
  • 11
  • 4