英文:
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<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();
		}
	}
}  
答案1
得分: 3
简单的答案是:
if (file.getName().equals("cars.txt")) {
    ...
} else if (file.getName().equals("employees.txt")) {
    ...
}
但这里存在一些更深层次的问题:
- 
一个单一的方法或构造函数读取文件,而文件可以具有两种完全不同的格式……和含义,这似乎很奇怪(错误!)。
 - 
构建一个汽车经销商,只有汽车或只有员工似乎很奇怪(错误!)。汽车经销商需要既有汽车又有员工。
 
英文:
The simple answer is
if (file.getName().equals("cars.txt")) {
    ...
else if (file.getName().equals("employees.txt")) {
    ...
}
But there are some deeper problems here:
- 
It seems odd (wrong!) to have a single method or constructor that reads a file that can have two completely different formats ... and meanings.
 - 
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
 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论