如何检查文件名?

huangapple go评论58阅读模式
英文:

How to check for a file name?

问题

好的,以下是您要翻译的内容:

好的我知道标题确实很模糊但我无法准确表达我的问题因此我无法通过搜索引擎找到答案这就是为什么我向大家请教的原因

基本上我有一个构造函数它接受一个文件输入并根据文件名在我的示例中我们使用file.txt或employees.txt对其进行处理现在我不知道这是否是一个好主意或者是否有另一种方法可以创建多个相似的构造函数以下是我目前的代码感谢您的帮助!:

public class CarDealershipSystem {

    public CarDealershipSystem(File file) {

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            ArrayList<Car> carObjectArray = new ArrayList<Car>();
            while((br.readLine()) != null) {

                if (file == cars.txt) {
                    String line = br.readLine();
                    String[] lineArray = line.split(",");

                    Car car = new Car();
                    car.setMake(lineArray[0]);
                    car.setModel(lineArray[1]);

                    carObjectArray.add(car);
                }
                else if (file == employees.txt) {
                    ;
                }
            }

        }catch(IOException e) {
            e.getLocalizedMessage();
            e.printStackTrace();
        }
    }
}
英文:

Alright I know the title is really vague but I can't wrap my head around how to word my problem and therefore I can't google it; that's why I'm asking you guys

Basically I have a constructor that takes a file input and depending on the file name (in my example we're using file.txt or employees.txt) we do something to it. Now I have no idea if this is even a good idea or not or if there is another way to do multiple duplicate constructors. Here's my code so far and thanks for helping me out!:

public class CarDealershipSystem {

	public CarDealershipSystem(File file) {

		try (BufferedReader br = new BufferedReader(new FileReader(file))) {
			ArrayList&lt;Car&gt; carObjectArray = new ArrayList&lt;Car&gt;();
			while((br.readLine()) != null) {

				if (file == cars.txt) {
					String line = br.readLine();
					String[] lineArray = line.split(&quot;,&quot;);

					Car car = new Car();
					car.setMake(lineArray[0]);
					car.setModel(lineArray[1]);

					carObjectArray.add(car);
				}
				else if (file == employees.txt) {
					;
				}
			}

		}catch(IOException e) {
			e.getLocalizedMessage();
			e.printStackTrace();
		}
	}
}  

答案1

得分: 3

简单的答案是:

if (file.getName().equals("cars.txt")) {
    ...
} else if (file.getName().equals("employees.txt")) {
    ...
}

但这里存在一些更深层次的问题:

  1. 一个单一的方法或构造函数读取文件,而文件可以具有两种完全不同的格式……和含义,这似乎很奇怪(错误!)。

  2. 构建一个汽车经销商,只有汽车或只有员工似乎很奇怪(错误!)。汽车经销商需要既有汽车又有员工。

英文:

The simple answer is

if (file.getName().equals(&quot;cars.txt&quot;)) {
    ...
else if (file.getName().equals(&quot;employees.txt&quot;)) {
    ...
}

But there are some deeper problems here:

  1. It seems odd (wrong!) to have a single method or constructor that reads a file that can have two completely different formats ... and meanings.

  2. It seems odd (wrong!) that you can construct a car dealership with either only cars, or only employees. A car dealership needs to have both cars and employees

huangapple
  • 本文由 发表于 2020年5月4日 16:19:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/61587898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定