1

I am trying to copy a file from one location to another. While copying it from source to target, target file is taking current date time. How can i make target file date as same as source file.

FileInputStream source = new FileInputStream("D:\\test\\test.txt");
OutputStream target = new FileOutputStream("D:\\test.txt");
byte[] buffer = new byte[source.available()];
source.read(buffer);
target.write(buffer);
source.close();
target.close();`
Janny
  • 127
  • 1
  • 12
  • https://stackoverflow.com/questions/10824027/get-the-metadata-of-a-file – Rajat Feb 16 '20 at 07:09
  • The API is your friend: [java.nio.file.Files#copy](https://docs.oracle.com/javase/8/docs/api/java/nio/file/Files.html#copy-java.io.InputStream-java.nio.file.Path-java.nio.file.CopyOption...-) – Abra Feb 16 '20 at 07:15
  • There is a specific warning in the Javadoc against the way you're using `available()`, and in any case I t isn't necessary to read the entire file into memory. – user207421 Feb 16 '20 at 11:02

3 Answers3

2

Just use the Path API if you may.

For example to keep all attributes of the original file in the new file, use Files.copy(Path source, Path target, CopyOption... options) :

try {
   Path copiedFile = 
   Files.copy(Paths.get("D:\\test\\test.txt"), Paths.get("D:\\test.txt"), 
             StandardCopyOption.COPY_ATTRIBUTES);   
}
catch (IOException e){
  // handle that
}

The StandardCopyOption.COPY_ATTRIBUTES enum states :

Minimally, the last-modified-time is copied to the target file if supported by both the source and target file stores.

If you want to copy only the last-modified time attribute, that is not more complicated, just add that setting after the copy and remove the CopyOption arg such as :

Path originalFile = Paths.get("D:\\test.txt")
try {
   Path copiedFile = 
   Files.copy(Paths.get("D:\\test\\test.txt"), originalFile);   
   Files.setLastModifiedTime(copiedFile, 
          Files.getLastModifiedTime(originalFile));
}
catch (IOException e){
  // handle that
}

At last, note that Path and File are interoperable : Path.toFile() returns the corresponding File and File.toPath() returns the corresponding Path.
So even if you manipulate Files as input, the implementation may still use the Path API without breaking that.

davidxxx
  • 125,838
  • 23
  • 214
  • 215
1

This is provided by the java.io.File class. You need to create an instance of it first and pass it to the streams:

File input = new File("D:\\test\\test.txt");
File output = new File("D:\\test.txt");
try( FileInputStream source = new FileInputStream(input);
     OutputStream target = new FileOutputStream(output)){
    byte[] buffer = new byte[source.available()];
    source.read(buffer);
    target.write(buffer);
}
long modified = input.lastModified();
output.setLastModified(modified);

BTW: I assume you use at least Java 7, so I changed your code to use the try-with-resources feature. This is strongly recommended since it also takes care of closing resources in case an exception is raised.

pxcv7r
  • 478
  • 5
  • 14
  • Do you know about class `java.nio.file.Files` and its `copy()` method? The class was added in JDK 7. – Abra Feb 16 '20 at 07:16
1

Since JDK 7, the following code will make a copy of a file and the copied file will have the same attributes as the original file which means the target file will have the same date as the source file.

java.nio.file.Path source = java.nio.file.Paths.get("D:\\test\\test.txt");
java.nio.file.Path target = java.nio.file.Paths.get("D:\\test.txt");
try {
    java.nio.file.Files.copy(source, target, java.nio.file.StandardCopyOption.COPY_ATTRIBUTES);
}
catch (java.io.IOException x) {
    x.printStackTrace();
}
Abra
  • 19,142
  • 7
  • 29
  • 41