1:方式1:
public static List<String> userList;
static {
userList = new LinkedList<String>();
try {
**String filePath = TestClient.class.getClassLoader().getResource("users.txt").getPath();**
**BufferedReader reader = new BufferedReader(new FileReader(new File(filePath)));**
try {
String line = null;
while ((line = reader.readLine()) != null) {
userList.add(line);
}
} finally {
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}
2:方式2:
public staitc List<String> userList;
static {
userList = new LinkedList<String>();
try {
**InputStream is = TestClient.class.getResourceAsStream("/user.txt");**
**BufferedReader reader = new BufferedReader(new InputStreamReader(is));**
try {
String line = null;
while ((line = reader.readLine()) != null) {
userList.add(line);
}
} finally {
reader.close();
}
}catch (Exception e){
e.printStackTrace();
}
3:Other Demos
import java.io.InputStream;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class PropValue {
private static String BASE_URL;
static {
ResourceBundle rb=null;
try {
InputStream inputStream = PropValue.class.getResourceAsStream("/boot.properties");
rb = new PropertyResourceBundle(inputStream);
BASE_URL=rb.getString("boot_url");
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args){
System.out.println(PropValue.BASE_URL);
}
}