java - copy files preserving time/date information

MarkLoveExEx
MarkLoveExEx used Ask the Experts™
on
I need to copy files in Java, preserving time/date information. How do I do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2015

Commented:
have you got any code so far?
This is normally handled as CopyOptions in java

Author

Commented:
gurpsbassi -- I think your right. I'll be able to test in a couple hours. I THINK the CopyOption for copying attributes was added in Java 7 (I was using Java 6). After I upgraded, the copy attributes was available. I let you know and assign points after I test...   Mark

Author

Commented:
I got the following runtime error:

java.lang.UnsupportedOperationException: COPY_ATTRIBUTES not supported

I'm on a linux sytem.
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

Top Expert 2016

Commented:
Mark,

I think we've been here before. If the source is not a regular file (e.g. you're reading from an archive) then attributes can't be preserved

Author

Commented:
I really wish a workaround could be found. Even if third party software must be used.  Mark
Top Expert 2016

Commented:
Don't you remember we discussed this earlier. Got to go out but back later

Author

Commented:
CEHJ -- I think it said it wasn't supported. These date/time attributes are very important to me, because I need to know when these files arrive on the system. So, I'm trying to use ProcessBuilder to get the linux os to do it. However, I'm having trouble getting it to work. Can you tell what I'm doing wrong?

                  try {
                  String[] command = {"/usr/bin/gunzip", "*.gz"};
              ProcessBuilder probuilder = new ProcessBuilder( command );

              //work directory
              probuilder.directory(new File("/home/public/getweb/Q2/ldm_q2rad_hsr"));
              
              Process process = probuilder.start();
              
              //Read out dir output
              InputStream is = process.getInputStream();
              InputStreamReader isr = new InputStreamReader(is);
              BufferedReader br = new BufferedReader(isr);
              String line;
              System.out.printf("Output of running %s is:\n",
                      Arrays.toString(command));
              while ((line = br.readLine()) != null) {
                  System.out.println(line);
              }
              
              //Wait to get exit value
              try {
                  int exitValue = process.waitFor();
                  System.out.println("\n\nExit Value is " + exitValue);
              } catch (InterruptedException e) {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
              }
                  } catch (IOException e) {
                        e.printStackTrace();
                  }

My output:
Output of running [/usr/bin/gunzip, *.gz] is:


Exit Value is 1
gurpsbassi -- CopyOption did not work.
gurpsbassi & CEHJ -- Found a solution. ProcessBuilder couldn't run gunzip directly. I had to put the gunzip command in a bash script. Processbuilder successfully ran the script.

Author

Commented:
Solution not provided by experts.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial