How can you choose which file you delete?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mike131
    New Member
    • Sep 2007
    • 12

    How can you choose which file you delete?

    Hi guys!
    I have a little question, and i found that forum as the best place for asking it.
    so, i built a little code, that deletes a file wich the user choose.

    Code:
    import java.io.File;
    import java.io.IOException;
    public class Delete {
      public static void main(String[] argv) throws IOException {
        File del = new File("Delete.java");
        del.delete();
      }
    }
    as you all know, codes that suppose to use any output file have an approach only to files in the specific folder that holds their Java doc (of the code).
    my question is how can i choose the place of my output file?, how can i change it's folder?

    Thank you very much!
    mike
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    You can have your program take a command line argument (the values of args[]) that specifies the complete path to the desired folder. Note that in Java argument 0 (args[0]) is not the name of the running program as it is in C++, so here it will be the file path you want.

    Comment

    • N002199B
      New Member
      • Feb 2007
      • 41

      #3
      Originally posted by mike131
      Hi guys!
      I have a little question, and i found that forum as the best place for asking it.
      so, i built a little code, that deletes a file wich the user choose.

      Code:
      import java.io.File;
      import java.io.IOException;
      public class Delete {
        public static void main(String[] argv) throws IOException {
          File del = new File("Delete.java");
          del.delete();
        }
      }
      as you all know, codes that suppose to use any output file have an approach only to files in the specific folder that holds their Java doc (of the code).
      my question is how can i choose the place of my output file?, how can i change it's folder?

      Thank you very much!
      mike
      read java doc on the File Class instantiation. You can actually specify the URL in one of the costructors.

      Comment

      • BigDaddyLH
        Recognized Expert Top Contributor
        • Dec 2007
        • 1216

        #4
        Please remember to provide a meaningful Title for any threads started (see the FAQ entry Use a Good Thread Title).

        This helps to ensure that other members, and also the general public, will have a better chance of finding answers to any similar questions.

        MODERATOR

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          @OP: have a look at the JFileChooser class.

          kind regards,

          Jos

          Comment

          • mike131
            New Member
            • Sep 2007
            • 12

            #6
            First, thank you all guys!
            So, for example i had found the next code in a Java codes ?site. how can i make it delet a file calls: "aa.txt" on hard drive D:/ ??

            Code:
            public class FileLocking {
              public static void main(String[] argv) {
                if (argv.length != 2) {
                  System.err.println("usage: KillFilesByName dirname pattern");
                  System.exit(1);
                }
            
                File dir = new File(argv[0]);
                if (!dir.exists()) {
                  System.out.println(argv[0] + " does not exist");
                  return;
                }
                String patt = argv[1];
            
                String[] info = dir.list();
                for (int i = 0; i < info.length; i++) {
                  File n = new File(argv[0] + dir.separator + info[i]);
                  if (!n.isFile()) { // skip ., .., other directories, etc.
                    continue;
                  }
                  if (info[i].indexOf(patt) == -1) { // name doesn't match
                    continue;
                  }
                  System.out.println("removing " + n.getPath());
                  if (!n.delete())
                    System.err.println("Couldn't remove " + n.getPath());
                }
              }
            }
            thank you very much!
            mike

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Read the responses in this thread again.
              You have the code to delete a file. Now you need to select the file to delete using the JFileChooser.

              Comment

              • BigDaddyLH
                Recognized Expert Top Contributor
                • Dec 2007
                • 1216

                #8
                It's still not clear what you are trying to ask, especially since you're posting random code you find on the internet. So what do you *really* want to do? Please be clear, complete and precise.

                Comment

                Working...