如何定位目录和删除文件?(How to target directories and delete files?)

编程入门 行业动态 更新时间:2024-10-26 00:25:08
如何定位目录和删除文件?(How to target directories and delete files?)

我在大学时已经学习了4个星期的JavaScript,而且我已经在2周前开始学习Java了。 它证明是有用的,因为我们有一个项目,我们必须制定一个项目(这就是我们为项目选择的项目)。

问题在于,虽然我了解了Java的大部分基础知识,但我并不知道如何创建我们想要制作的程序,而我所有其他团队成员甚至从未接触过Java,甚至有人甚至在课堂上苦苦挣扎。

对不起,顺便说一句,我只是想确保人们明白我来自哪里。

无论如何,我们决定制作一个类似于Windows控制面板中的磁盘清理功能的程序。 如同,它删除临时和不必要的文件以释放空间。

我不知道如何开始编写类似的东西。 我不是要求任何人为我做整个编码,但我需要快速学习如何制作一个针对列出目录的程序,然后从该目录中删除特定或所有文件。 因此,例如,我需要它定位的第一件事是Windows文件夹中的“Temp”文件夹,然后删除其中的所有文件及其任何子文件夹。

我该怎么办?

另外,这是我第一次在Stack Overflow上发布一个问题,而且我真的被不知所措的左右复杂事情所困扰,而且我一直在问这个问题,因为我觉得如果我问一些非常简单的东西,不要把我的问题放在一大堆复杂的代码中,我会被终止或是什么。

I've been learning JavaScript for 4 weeks now at college, and I've starting learning Java 2 weeks ago in my own time. It proved useful as we have a project, where we have to make a program (that's what we chose for the project anyway).

The problem is, while I know most of the fundamentals of Java, I don't know enough to make the program we want to make, and all my other team members have never even touched Java, and some are even struggling with JavaScript in class.

Sorry for the rambling btw, I just want to make sure people understand where I'm coming from.

Anyway, we decided to make a program that is similar to the disk cleanup function in the Windows Control Panel. As in, it deletes temporary and unnecessary files to free up space.

And I have no idea how to even begin coding something like that. I'm not asking anyone to do the whole coding for me, but I need to quickly learn how to make a program that targets listed directories, and then deletes specific or all files from that directory. So for example, the first thing I would need it to target is the "Temp" folder in the Windows folder, and then delete all the files and any sub-folders in it.

How would I do this?

Also, this is my first time ever posting a question on Stack Overflow, and I've been really overwhelmed with the amount of really complex things being asked left and right, and I was holding off on asking this, because I feel like if I ask something very simple, and don't put in a huge block of complex code with my question, I'm gonna get terminated or something.

最满意答案

为了获得临时目录,你可以参考这个帖子 ,这表明它是这样的:

System.getProperty("java.io.tmpdir")

另外,对于如何处理文件操作,我建议你阅读这里找到的java文档。

例如,该链接指示要删除文件,您可能对该方法感兴趣:

public boolean delete()

删除此抽象路径名表示的文件或目录。 如果此路径名表示目录,则该目录必须为空才能被删除。

请注意,Files类定义了删除方法,以便在无法删除文件时抛出IOException。 这对于错误报告和诊断文件不能被删除的原因很有用。

返回: 当且仅当文件或目录被成功删除时才返回true; 否则为假

抛出: SecurityException - 如果安全管理器存在并且其SecurityManager.checkDelete(java.lang.String)方法拒绝对文件的删除访问

我还建议你四处搜索并寻找一些例子,以及阅读在线Javadocs的相关部分。

另外,我可以从经验中得知,如果你不知道自己在做什么,删除文件可能有点麻烦。 为了尝试这一点,你可能想要确保你知道你在做什么。

For getting the temporary directory, you can refer to this post, which indicates it is this:

System.getProperty("java.io.tmpdir")

Also, for how to deal with file manipulation, I suggest you read the java documentation found here.

For example, that link indicates that to delete a File, you might be interested in the method:

public boolean delete()

Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory must be empty in order to be deleted.

Note that the Files class defines the delete method to throw an IOException when a file cannot be deleted. This is useful for error reporting and to diagnose why a file cannot be deleted.

Returns: true if and only if the file or directory is successfully deleted; false otherwise

Throws: SecurityException - If a security manager exists and its SecurityManager.checkDelete(java.lang.String) method denies delete access to the file

I'd also reccommend you search around and look for some examples, as will as read relevant portions of the online Javadocs.

Also, I can speak from experience that deleting files can be a bit messy if you do not know what you are doing. Beforing trying that, you might want to really make sure you know what you are doing.

更多推荐

本文发布于:2023-08-05 14:31:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1434005.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   目录   target   files   delete

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!