File Writing.. Overwriting

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • akoymakoy
    New Member
    • Oct 2006
    • 42

    File Writing.. Overwriting

    Hi
    what do i have to use in order to browse for a jpeg.file and save it to a specific directory... and overwrite the existing

    and does it only work for jpeg? or any kind of file



    thanks in advance..
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by akoymakoy
    Hi
    what do i have to use in order to browse for a jpeg.file and save it to a specific directory... and overwrite the existing

    and does it only work for jpeg? or any kind of file



    thanks in advance..
    Use a JFileChooser to browse to the file.
    Use the File.renameTo method for the moving. Note for the overwriting to work with this one, you'll probably need to delete the old file first. (Read about it in the docs)
    It's not limited to jpeg files only.

    Comment

    • akoymakoy
      New Member
      • Oct 2006
      • 42

      #3
      import java.awt.*;
      import java.awt.image. *;
      import java.io.*;
      import javax.imageio.* ;
      import java.awt.event. *;

      public class FileOverWrite {

      public static void main(String[] args) throws Exception {

      String outFileName = "jpg";
      String fileName=args[1];
      Image image;

      Toolkit toolkit = Toolkit.getDefa ultToolkit();
      image = toolkit.getImag e(fileName);
      ImageIO.write(i mage, "jpg", new File(outFileNam e));

      }
      }

      so far this is what i have made...

      the error it gives is:
      cannot find symbol method write(java.awt. Image,java.lang .String,java.io .File)

      whats wrong with the code.. i found the .write in the api

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by akoymakoy
        import java.awt.*;
        import java.awt.image. *;
        import java.io.*;
        import javax.imageio.* ;
        import java.awt.event. *;

        public class FileOverWrite {

        public static void main(String[] args) throws Exception {

        String outFileName = "jpg";
        String fileName=args[1];
        Image image;

        Toolkit toolkit = Toolkit.getDefa ultToolkit();
        image = toolkit.getImag e(fileName);
        ImageIO.write(i mage, "jpg", new File(outFileNam e));

        }
        }

        so far this is what i have made...

        the error it gives is:
        cannot find symbol method write(java.awt. Image,java.lang .String,java.io .File)

        whats wrong with the code.. i found the .write in the api

        Did you see my reply above?

        Comment

        • akoymakoy
          New Member
          • Oct 2006
          • 42

          #5
          ohh im sorry ill try that one ... just a little tired i guess ... :D


          is there a sample code i could refer to in using this?

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by akoymakoy
            ohh im sorry ill try that one ... just a little tired i guess ... :D


            is there a sample code i could refer to in using this?
            Perhaps if you were to actually open those links ...

            Comment

            • akoymakoy
              New Member
              • Oct 2006
              • 42

              #7
              public class FileOverWrite {

              public static void main(String[] args) throws Exception {

              String fileName=args[0];
              File fp = new File(fileName);
              String writeFolder="C: \\pic2";
              fp.renameTo(new File(writeFolde r));

              }
              }
              no file is being created but it gives no errors : this is my input C:\java FileOverWrite c:\pic\image.jp g


              c:\pic\image.jp g is the file i want to copy
              i want to put it in c:\ what should i add to the code?

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by akoymakoy
                public class FileOverWrite {

                public static void main(String[] args) throws Exception {

                String fileName=args[0];
                File fp = new File(fileName);
                String writeFolder="C: \\pic2";
                fp.renameTo(new File(writeFolde r));

                }
                }
                no file is being created but it gives no errors : this is my input C:\java FileOverWrite c:\pic\image.jp g


                c:\pic\image.jp g is the file i want to copy
                i want to put it in c:\ what should i add to the code?
                1.) Use code tags when posting code
                2.)
                Originally posted by API docs
                The rename operation might not be able to move a file from one filesystem to another, it might not be atomic, and it might not succeed if a file with the destination abstract pathname already exists. The return value should always be checked to make sure that the rename operation was successful.
                From the API docs (the link I posted above)

                Comment

                Working...