对于我正在做的项目,我创建了一个java程序,用于搜索用户输入指定的文件.
代码开始在用户指定的基目录中搜索(即:C :).它遍历此目录中的所有文件,检查文件名是否与用户给出的搜索项匹配,如果匹配,则将文件绝对路径添加到字符串中.如果文件是目录,则将其添加到列表中以便稍后处理.
当基础文件夹完成搜索时,它将以相同的方式搜索/删除列表中的第一个目录(再次添加找到列表的任何目录)并继续直到没有其他目录要搜索.然后显示找到的文件给用户.
我的问题; 有更好的方法来搜索文件吗?也许立即搜索目录而不是将它们添加到列表中?任何建议都会很棒,提前谢谢!这是我的代码.
public String SearchDir(File directory){
this.directory = directory;
do{
File[] files = this.directory.listFiles();
if(files != null){
for(int i = 0; i < files.length; i++){
// The current file.
File currentFile = files[i];
// The files name without extension and path
// ie C:\Documents and Settings\myfile.file = myfile
String fileName = this .removeExtension(this.removePath(currentFile.getName()));
// Don't search hidden files
if(currentFile.isHidden()){
continue;
}
System.out.println(currentFile.getAbsolutePath());
// Check if the user wanted a narrow search
if(this.narrow){
// Narrow search = …Run Code Online (Sandbox Code Playgroud) 我正在制作一个包含文件复制的应用程序,但是当我浏览一个大目录(1000+)文件并将它们复制到另一个文件夹时,它使用290+ MB的RAM.
那么,有没有办法改变File的FileOutputStream,而无需创建一个新的实例FileOutoutStream类?
编辑:
这是我的Java 7 API版本.
Path source = FileSystems.getDefault().getPath(Drive.getAbsolutePath(), files[i].getName());
Path destination = FileSystems.getDefault().getPath(Save);
try {
Files.copy(source, destination);
} catch (FileAlreadyExistsException e) {
File file = new File(Save + files[i]);
file.delete();
}
Run Code Online (Sandbox Code Playgroud)
请记住,这是在一个for循环中,正在测试1000多个文件计数.使用当前的方法,我使用270+ MB的RAM
我看到以下代码,想知道编码器的意图。它是自动装箱的相关性能吗?
map.put("doesntMatter", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)
他本可以这样做:
map.put("doesntMatter", true);
Run Code Online (Sandbox Code Playgroud)
做第一个有什么好处吗?
我有一个Java调用的线程
t.interrupt();
Run Code Online (Sandbox Code Playgroud)
使t(不同的线程)中断.我希望"t"线程然后捕获一个InterruptedException但是Eclipse不会让我InterruptedException说它不会在try体中抛出.我怎样才能得到这个InterruptedException名字?我用t.interrupt()错了吗?
创建对象并调用其公共方法之一时,可以使用本地属性.它们存放在哪里?我的意思是,在像C这样的语言中,一切都很清楚:要使用的变量必须作为参数传递给函数(或者必须是全局的).
它在Java(以及其他OO语言)中会发生什么?本地方法如何使用实例的属性?
简而言之:当我们说this.variable,方法如何找到自己的变量?"this"指针是否作为函数参数隐式传递?
我有以下代码示例
int i = 1;
while(i != 0) {
i++;
}
Run Code Online (Sandbox Code Playgroud)
我期待这个在无限循环中运行,但事实并非如此.然后,当我在while循环后打印值时,我得到了:
i value is 0.
Run Code Online (Sandbox Code Playgroud)
谁能告诉我究竟发生了什么?
我一直在从事 JSF 工作,遇到了 RI(参考实现)这个术语。这是什么 RI。另外,这仅限于 JSF 框架还是所有框架都有这个?
我尝试了数据类型blob.这给了一些Datastax例外.我尝试了对象本身,bytearray.仍然没有好处:
Caused by: com.datastax.driver.core.exceptions.InvalidQueryException: Invalid STRING constant ([B@547248ad) for user_object of type blob
Run Code Online (Sandbox Code Playgroud)
这是失败的INSERT:
executeSting.append("INSERT INTO htadb.objecttable (object_id, bucket_name, object_key, link, user_status, user_object) ")
.append("VALUES (")
.append(objectId).append(",'")
.append(bucketName).append("','")
.append(key).append("','")
.append(link).append("','")
.append("online").append("','")
.append(serializer(register)).append("')"
+ ";");
Run Code Online (Sandbox Code Playgroud) 我有以下代码
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1, 555);
}
Run Code Online (Sandbox Code Playgroud)
它用10个元素初始化,但我得到例外
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.rangeCheckForAdd(ArrayList.java:612)
at java.util.ArrayList.add(ArrayList.java:426)
at ListTest.main(ListTest.java:9)
Run Code Online (Sandbox Code Playgroud)
虽然下面的代码工作正常
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(0, 555);
}
Run Code Online (Sandbox Code Playgroud)
为什么有人可以解释我?以及如何解决问题我想在我的代码中将项目放在第1,第2或第5位?
我需要在Java中创建一个64位唯一整数,以便碰撞机会很低.系统不是分布式的,因此不同计算机之间的冲突不是问题.
有没有办法,我们可以在Java中创建一个64位整数,它总是唯一的?
截至目前我正在使用 -
long number = System.nanoTime();
Run Code Online (Sandbox Code Playgroud)
这是在Java中生成64位唯一整数的正确方法还是我可以尝试其他任何东西?
更新: -
这样做怎么样?这会是独一无二的吗?
UUID number = UUID.randomUUID();
long uniqueNumber = number.timestamp();
Run Code Online (Sandbox Code Playgroud)