473,407 Members | 2,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

How can you choose which file you delete?

12
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.

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.IOException;
  3. public class Delete {
  4.   public static void main(String[] argv) throws IOException {
  5.     File del = new File("Delete.java");
  6.     del.delete();
  7.   }
  8. }
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
Jan 27 '08 #1
7 1416
Laharl
849 Expert 512MB
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.
Jan 27 '08 #2
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.

Expand|Select|Wrap|Line Numbers
  1. import java.io.File;
  2. import java.io.IOException;
  3. public class Delete {
  4.   public static void main(String[] argv) throws IOException {
  5.     File del = new File("Delete.java");
  6.     del.delete();
  7.   }
  8. }
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.
Jan 27 '08 #3
BigDaddyLH
1,216 Expert 1GB
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
Jan 27 '08 #4
JosAH
11,448 Expert 8TB
@OP: have a look at the JFileChooser class.

kind regards,

Jos
Jan 27 '08 #5
mike131
12
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:/ ??

Expand|Select|Wrap|Line Numbers
  1. public class FileLocking {
  2.   public static void main(String[] argv) {
  3.     if (argv.length != 2) {
  4.       System.err.println("usage: KillFilesByName dirname pattern");
  5.       System.exit(1);
  6.     }
  7.  
  8.     File dir = new File(argv[0]);
  9.     if (!dir.exists()) {
  10.       System.out.println(argv[0] + " does not exist");
  11.       return;
  12.     }
  13.     String patt = argv[1];
  14.  
  15.     String[] info = dir.list();
  16.     for (int i = 0; i < info.length; i++) {
  17.       File n = new File(argv[0] + dir.separator + info[i]);
  18.       if (!n.isFile()) { // skip ., .., other directories, etc.
  19.         continue;
  20.       }
  21.       if (info[i].indexOf(patt) == -1) { // name doesn't match
  22.         continue;
  23.       }
  24.       System.out.println("removing " + n.getPath());
  25.       if (!n.delete())
  26.         System.err.println("Couldn't remove " + n.getPath());
  27.     }
  28.   }
  29. }
thank you very much!
mike
Jan 29 '08 #6
r035198x
13,262 8TB
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.
Jan 29 '08 #7
BigDaddyLH
1,216 Expert 1GB
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.
Jan 29 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
1
by: dominic | last post by:
Hi all, I'm trying to create a temp file by copying from an existing file, perform some operation on the temp file, and then delete it. The code I used is as follow: // First, to copy the...
5
by: Hal Vaughan | last post by:
I have to go through and delete a bunch of files after another program used them. I'm doing: File oFile = new File(fileName); oFile.delete(); if (oFile.exists()) { oFile.deleteOnExit(); } ...
48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
0
by: SeanR | last post by:
I have a function to copare two files. It will first copy the original file form a different server to a local temp path and then compare that version to a version that has been restored form tape....
7
by: Diggler | last post by:
I have a web application that allows a user to manage files on the server. On the page is a counter that tells them how many files are in their folder. When you delete a file from the folder using...
3
by: Joe | last post by:
Hi, I have written a webpage that allows a user to delete files in asp.net with I am having a small problem. To access this page a user has to login via login.aspx page. After successful...
12
by: Lucas Tam | last post by:
I have a very simple loop: If (Directory.Exists(tempDirectory)) Then Try Dim Files() As String = Directory.GetFiles(tempDirectory) 'Clear out directory For Each Filename As String In Files...
9
by: Paul | last post by:
I'm trying to make get my app to delete all the files in a specified folder and all the files within the folders of the specified folder. e.g. Folder 1 contains three files (File1, File2, File3)...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.