gpt4 book ai didi

java - 如何避免这种冗余并关闭未使用的文件类?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:44:51 25 4
gpt4 key购买 nike

简介

我正在做一些作业,我的任务是编写一个使用路径(绝对)管理文件的程序。读取文件信息、写入/读取它们等。

我的尝试

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fichersapren;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;

/**
*
* @author alumneM
*/
public class FitxersApren {

public static String working_path = "";
public static Calendar cal;


/**
* Get username from system
*/
public static void obtenirNomUsuari()
{
System.out.println(System.getProperty("user.name"));
}
/**
* Create file
*
* @param path
* @throws IOException
*/
public static void crearFicher(String path) throws IOException
{
File cf = new File(path);
cf.createNewFile();
}
/**
* Create Directory
*
* @param path
*/
public static void crearDirectori(String path)
{
File cd = new File(path);
cd.mkdir();
}
/**
* Get file info
*
* @param path
*/
public static void mostrarInfoFitcher(String path)
{
File infoF = new File(path);
System.out.println("Nombre fichero: " + infoF.getName());
System.out.println("Permisos: ");
System.out.println(" - Escritura: " + infoF.canWrite() + " ");
System.out.println(" - Lectura: " + infoF.canRead() + " ");
System.out.println(" - Ejecucion: " + infoF.canExecute() + " ");
System.out.println("Ruta: " + infoF.getAbsolutePath() + " ");
System.out.println("Tamaño: " + infoF.length() + " ");
//cal.setTimeInMillis(infoF.lastModified());
//System.out.println("Fecha: " + cal.get(Calendar.YEAR));
}
/**
* Get directory info
*
* @param path
*/
public static void mostrarInfoDirectori(String path)
{
File infoD = new File(path);
System.out.println("Nombre directorio: " + infoD.getName());
System.out.println("Permisos: ");
System.out.println(" - Escritura: " + infoD.canWrite() + " ");
System.out.println(" - Lectura: " + infoD.canRead() + " ");
System.out.println(" - Ejecucion: " + infoD.canExecute() + " ");
System.out.println("Ruta: " + infoD.getAbsolutePath() + " ");
System.out.println("Tamaño: " + infoD.length() + " ");
//cal.setTimeInMillis(infoD.lastModified());
//System.out.println("Fecha: " + cal.get(Calendar.YEAR));
}
/**
* Delete dile
*
* @param path
*/
public static void esborrarFitxer(String path)
{
File file_del = new File(path);
file_del.delete();

// closeMe(file_del);
}
/**
* Delete directory
*
* @param path
*/
public static void esborrarDirectori(String path)
{
File dir_del = new File(path);
dir_del.delete();

//closeMe(dir_del);
}
/**
* Change file name
*
* @param path
* @param nom
*/
public static void canviarNom(String path, String nom)
{
File nom_file = new File(path);




}
/**
* My try at a general method to close File classes
*
* @param path
*/
public static void closeMe(Closeable path)
{
try
{
path.close();
}
catch (Exception e)
{
System.out.println("Could not be closed");
}
}



/**
* Main method
*
* @param args the command line arguments
*/
public static void main(String[] args) {
mostrarInfoFitcher("C:\\Users\\alumneM\\Desktop\\bitcoin.txt");
mostrarInfoFitcher("C:\\Users\\alumneM\\Desktop\\bitcoin.docx");





}
}

问题

  1. 在这些方法中包含所有这些 File 类让我相信它效率不高。必须有其他更好、更专业的东西。

  2. 继我遇到的第一个问题之后,当每个方法的 File 类变量未被使用时会发生什么?会不会消耗资源?我怎样才能关闭它?

    2.1 在第二个问题之后,我可以简单地对这些变量使用 .close() 方法,但我不明白为什么。

我在这个问题中寻找什么?

  • 了解我的错误并从中吸取教训
  • 编写类似程序或处理文件的新技巧
  • 建议是如何改进我的编码方式

注意事项

  • 我添加了 Javadoc 以帮助理解该方法及其目标。

目标

我想制作一个 Java 程序,我可以在其中输入一个文件的路径,然后我可以在上面应用这些方法。

最佳答案

在您的 main 或任何您声明新文件的地方,您可以这样做来创建文件并调用方法:

    String path = "/example";
File file = new File(path);
esborrarDirectori(file);

如果您让每个方法都使用 File 作为参数,例如一个将如下所示:

public static void esborrarDirectori(File file)
{
file.delete();
}

如果你当时想对它做其他操作,你也可以创建你自己的方法来创建一个返回的文件:

public static File createFile(String path) {
File file = new File(path);
return file;
}

可以通过调用它来使用:

    String path = "/example";
File file = createFile(path);

除非您计划向 createFile() 方法添加一些其他功能,否则这没有多大用处。

通常,为了良好的习惯,您不应该创建一个新的File,除非您实际上是在打开一个新文件。如果您多次使用同一个 File,您应该创建它一次,然后将它发送给需要使用它的方法。

关于java - 如何避免这种冗余并关闭未使用的文件类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56131737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com