1

Hello I took on to a little side project today

It's a program that copies files from one folder to another to simplify playing a hotSeat game via dropbox

Now I need to copy a file called Medieval2.preference.cfg

public void setMultiplayer() throws Exception{
    Path origin = Paths.get(System.getProperty("user.home"), "Dropbox", "MTWMultiplayer","propertyFiles","multi.cfg");
    Path destination = Paths.get("C:\\Program Files (x86)\\Steam\\steamapps\\common\\Medieval II Total War\\medieval2.preference.cfg");

    Files.copy(origin, destination, REPLACE_EXISTING);
}

public void setSingleplayer() throws Exception{
    Path origin = Paths.get(System.getProperty("user.home"), "Dropbox", "MTWMultiplayer","propertyFiles","single.cfg");
    Path destination = Paths.get("C:\\Program Files (x86)\\Steam\\steamapps\\common\\Medieval II Total War\\medieval2.preference.cfg");

    Files.copy(origin, destination, REPLACE_EXISTING);
}

this code doesn't work and I've got a feeling it's because of the double extension in medieval2.preference.cfg

Is there a way to solve this problem? Thanks in advance

fox125
  • 97
  • 2
  • 11
  • Can you post the stack trace of the error you're getting? – childofsoong Sep 24 '15 at 17:58
  • Ok after having a better look at my stacktrace I figure the error occurs because the file is read only is there a way to edit the settings in order to make the file not read only, copy it and then make it read only-again? – fox125 Sep 24 '15 at 18:05
  • Read only wouldn't affect the source file, only the destination. I'm almost completely certain you're running into a Windows security issue (program files are a protected area). Check out this link: http://www.sevenforums.com/general-discussion/179931-writing-program-files-folder-how-access-denied.html TL;DR: Run your program as an administrator (if using command line, run command line as admin then run your program). – childofsoong Sep 24 '15 at 18:09
  • hmmm I doubt this is the problem because I have working code that copy's other files from and to the same folder. also the code works when I leave out 1 of the extensions. – fox125 Sep 24 '15 at 18:13
  • Interesting! I suppose it's possible that copying in that way tries to open the file in write mode rather than read - can you edit the file permissions (I think by right clicking and going to 'Properties', but I haven't used Windows in a while so I might be wrong) to allow writing, and see what that does? – childofsoong Sep 24 '15 at 18:17
  • Same error java.nio.file.AccessDeniedException: C:\Program Files (x86)\Steam\steamapps\common\Medieval II Total War\medieval2.preference.cfg at sun.nio.fs.WindowsException.translateToIOException(WindowsException.java:83) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:97) at sun.nio.fs.WindowsException.rethrowAsIOException(WindowsException.java:102) at sun.nio.fs.WindowsFileCopy.copy(WindowsFileCopy.java:165) .... – fox125 Sep 24 '15 at 18:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/90555/discussion-between-soong-and-fox125). – childofsoong Sep 24 '15 at 18:20

1 Answers1

3

You're running into a file permissions error - the output file already exists and it's read only. You can change the file permissions programmatically in Java via the instructions found at Manipulating Windows file permissions in Java. This will let you modify the file.

Community
  • 1
  • 1
childofsoong
  • 1,918
  • 16
  • 23