Some pieces of Java

本文详细解析了Java中文件读取路径问题,包括类路径、打包方式及资源获取方法,帮助开发者解决本地开发中遇到的路径困扰。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

@(Java基础相关)[ClassPath, 文件读取]

Java文件读取问题

当我们遇到读取项目文件的时候,经常遇到的是对文件路径问题的困扰。有很多完善的教程给出了范例,但是我们放到本地却经常报错,那么问题出在哪里?这里来从根源去解释一下:
首先,我们要知道我们编写的代码,是经过编译成类文件(.class)之后再调入JVM中执行的。所以,当我们说一个类的路径的时候,指的不是.java文件的路径,而是.class的路径。

我们有一个这样的示例项目,他的结构为:
这里写图片描述

先看Test类:
这里写图片描述
运行结果为:
这里写图片描述

下面一行一行来看:
注意到this.getClass().getResources("").toString();这里输出的当前类的路径。为:
这里写图片描述

首先看到,类的路径实在/bin目录下的,而不是在.java文件所在的/src目录。
然后还可以看到,因为package del.test这一句把test类打包到del.test包中。所以当前类所在的路径是在根路径的基础上再深入到del/test中

下面一行是:this.getClass().getResource("/").toString();
这里输出的是根目录的路径。为:
这里写图片描述

一定会奇怪为什么根目录在bin呢?在哪里指定了呢?如果你是用的是Eclipse,那么你会发现项目目录下有一个隐藏文件叫做.classpath,打开之,你会发现有这样的两行:

<classpathentry kind="src" path="src/com"/>
<classpathentry kind="output" path="bin"/>

这两行分别指定了kind=”src”,即源代码的根目录,为src/com。所以我们打包的时候会把src/com目录下的所有类进行编译。编译之后的.class文件就放在由kind=”output”指定的bin目录下。同时,会发现不可编译的配置文件.properties也被直接复制到这个目录下。

其实了解这些之后,第三行就很好理解了。读取文件,如果有/起头,则从根目录,即bin目录开始索引。如果没有/起头,则从当前类所在路径开始索引。

THE END

### Java Project Interface Service Template Inheritance Example Best Practices In object-oriented programming (OOP), inheritance allows classes to inherit fields and methods from other classes. When designing services within a Java project, using interfaces and abstract classes can significantly enhance code reusability and maintainability. #### Using Interfaces for Flexibility Interfaces provide a way to achieve abstraction without committing to any implementation details. By defining an interface, multiple implementations can be created while adhering to the same contract specified by the interface[^1]. ```java public interface PaymentService { boolean processPayment(double amount); } ``` This `PaymentService` interface declares a method signature but does not specify its behavior. Different concrete classes implementing this interface will define their own versions of these behaviors based on specific requirements. #### Abstract Classes as Templates Abstract classes serve as templates where some common functionality may already be implemented, leaving certain parts open for subclasses to implement according to particular needs[^2]. An abstract class might look like: ```java public abstract class BaseService implements PaymentService { protected String serviceName; @Override public final boolean processPayment(double amount) { System.out.println("Processing payment via " + getServiceName()); return doProcessPayment(amount); // Delegating actual processing logic to subclass } private String getServiceName() { return serviceName; } protected abstract boolean doProcessPayment(double amount); } ``` Here, `BaseService` provides shared setup such as logging or validation before delegating core operations back down into more specialized child classes through abstract methods. #### Concrete Implementations Following Design Patterns Concrete implementations extend the abstract base class and fill out missing pieces defined earlier. Each derived type represents distinct ways of handling payments depending upon business rules or external systems involved[^3]: ```java public class CreditCardPayment extends BaseService { public CreditCardPayment(String name){ super.serviceName = name; } @Override protected boolean doProcessPayment(double amount) { // Implementation-specific credit card transaction here... return true; // Assuming successful operation. } } // Similarly create another service e.g., PayPalPayment extending BaseService with different internal workings. ``` By structuring projects around well-defined abstractions provided either directly via interfaces or indirectly through abstract parent types, developers ensure loose coupling between components leading towards better scalability over time.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值