save file with JFileChooser save dialog

后端 未结 3 635
我在风中等你
我在风中等你 2021-01-03 12:06

I have written a Java program that opens all kind of files with a JFileChooser. Then I want to save it in another directory with the JFileChooser save dialog, but it only sa

3条回答
  •  -上瘾入骨i
    2021-01-03 12:36

    Just in addition to Kris' answer - I guess, you didn't read the contents of the file yet. Basically you have to do the following to copy a file with java and using JFileChooser:

    1. Select the source file with the FileChooser. This returns a File object, more or less a wrapper class for the file's filename
    2. Use a FileReader with the File to get the contents. Store it in a String or a byte array or something else
    3. Select the target file with the FileChooser. This again returns a File object
    4. Use a FileWriter with the target File to store the String or byte array from above to that file.

    The File Open Dialog does not read the contents of the file into memory - it just returns an object, that represents the file.

提交回复
热议问题