有什么区别:
InputStream is = this.getClass().getClassLoader().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
和
InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
和
InputStream is = this.getClass().getResourceAsStream(fileName)
Run Code Online (Sandbox Code Playgroud)
每个人何时比其他人更适合使用?
我想要读取的文件在类路径中作为我的类来读取文件.我的类和文件位于同一个jar中,并打包在EAR文件中,并部署在WebSphere 6.1中.
当类在Eclipse中实现Serializable时,我有两个选项:添加默认值serialVersionUID(1L)
或生成serialVersionUID(3567653491060394677L)
.我认为第一个更酷,但很多时候我看到人们使用第二个选项.有没有理由生成long serialVersionUID
?
考虑一个例子(在java中编译)
public abstract interface Interface {
public void interfacing();
public abstract boolean interfacing(boolean really);
}
Run Code Online (Sandbox Code Playgroud)
为什么接口被"声明"抽象是必要的?是否有适用于抽象接口的其他规则?
最后:如果abstract
已经过时,为什么它包含在Java中?抽象界面有历史吗?
考虑简单的测试类:
import java.math.BigDecimal;
/**
* @author The Elite Gentleman
*
*/
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BigDecimal x = new BigDecimal("1");
BigDecimal y = new BigDecimal("1.00");
System.out.println(x.equals(y));
System.out.println(x.compareTo(y) == 0 ? "true": "false");
}
}
Run Code Online (Sandbox Code Playgroud)
你可以(有意识地)说它x
等于y
(不是对象引用),但是当你运行程序时,以下结果显示:
false
true
Run Code Online (Sandbox Code Playgroud)
问:什么是之间的区别compareTo()
,并equals()
在BigDecimal
该compareTo
可以确定x
等于y
?
PS:我看到BigDecimal在inflate()
方法上有一个equals()
方法.inflate()
实际上做了什么?
我有这样的枚举:
public static enum Command
{
login,
register,
logout,
newMessage
}
Run Code Online (Sandbox Code Playgroud)
格式化文件时,输出变为:
public static enum Command
{
login, register, logout, newMessage
}
Run Code Online (Sandbox Code Playgroud) 我已经在bootstrap 2.3中使用了glyphicons,但现在我已升级到bootstrap 3.0.现在,我无法使用icon属性.
在bootstrap 2.3中,下面的标签正在工作
<i class="icon-search"></i>
Run Code Online (Sandbox Code Playgroud)
在bootstrap 3.0中,它不起作用.
返回的一些JDBC驱动程序的唯一方法Statement.RETURN_GENERATED_KEYS
是执行以下操作:
long key = -1L;
Statement statement = connection.createStatement();
statement.executeUpdate(YOUR_SQL_HERE, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = statement.getGeneratedKeys();
if (rs != null && rs.next()) {
key = rs.getLong(1);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做同样的事情PreparedStatement
?
编辑
我问我是否可以这样做的原因PreparedStatement
考虑以下情况:
private static final String SQL_CREATE =
"INSERT INTO
USER(FIRST_NAME, MIDDLE_NAME, LAST_NAME, EMAIL_ADDRESS, DOB)
VALUES (?, ?, ?, ?, ?)";
Run Code Online (Sandbox Code Playgroud)
在USER
表中有一个PRIMARY KEY (USER_ID)
是BIGINT AUTOINCREMENT
(因此你没有在SQL_CREATE
字符串中看到它.
现在,我填充?
使用PreparedStatement.setXXXX(index, value)
.我想回来ResultSet rs = PreparedStatement.getGeneratedKeys()
.我怎样才能做到这一点?
我有以下代码
public Object handlePermission(ProceedingJoinPoint joinPoint, RequirePermission permission) throws AccessException, Throwable {
System.out.println("Permission = " + permission.value());
if (user.hasPermission(permission.value())) {
System.out.println("Permission granted ");
return joinPoint.proceed();
} else {
System.out.println("No Permission");
throw new AccessException("Current user does not have required permission");
}
}
Run Code Online (Sandbox Code Playgroud)
当我使用没有权限的用户时,我得到的java.lang.reflect.UndeclaredThrowableException
不是AccessException
.
我有一个包含清单文件的webapp,我在其中编写了一个ant构建任务期间我的应用程序的当前版本.清单文件是正确创建的,但是当我尝试在运行时读取它时,我会得到一些奇怪的副作用.我在清单中阅读的代码是这样的:
InputStream manifestStream = Thread.currentThread()
.getContextClassLoader()
.getResourceAsStream("META-INFFFF/MANIFEST.MF");
try {
Manifest manifest = new Manifest(manifestStream);
Attributes attributes = manifest.getMainAttributes();
String impVersion = attributes.getValue("Implementation-Version");
mVersionString = impVersion;
}
catch(IOException ex) {
logger.warn("Error while reading version: " + ex.getMessage());
}
Run Code Online (Sandbox Code Playgroud)
当我将eclipse附加到tomcat时,我看到上面的代码有效,但它似乎得到了一个不同于我预期的清单文件,我可以告诉它,因为ant版本和构建时间戳都是不同的.然后,我在那里放了"META-INFFFF",上面的代码仍然有效!这意味着我正在阅读其他一些清单,而不是我的清单.我也试过了
this.getClass().getClassLoader().getResourceAsStream(...)
Run Code Online (Sandbox Code Playgroud)
但结果是一样的.从tomcat中运行的webapp内部读取清单文件的正确方法是什么?
编辑:感谢您的建议到目前为止.另外,我应该注意到我正在运行tomcat独立; 我从命令行启动它,然后附加到Eclipse调试器中正在运行的实例.这不应该有所作为,不是吗?
我有一个巨大的excel文件,有大量的列,看起来像这样: -
Column1 Column2 Column3 Column4 Column5
abc def ghi
mno pqr
......
Run Code Online (Sandbox Code Playgroud)
这是我编写的用于打印这些值的代码:
try {
FileInputStream inputStr = new FileInputStream(fileName);
XSSFWorkbook xssfWork = new XSSFWorkbook(inputStr) ;
XSSFSheet sheet1 = xssfWork.getSheetAt(0);
Iterator rowItr = sheet1.rowIterator();
while ( rowItr.hasNext() ) {
XSSFRow row = (XSSFRow) rowItr.next();
System.out.println("ROW:-->");
Iterator cellItr = row.cellIterator();
while ( cellItr.hasNext() ) {
XSSFCell cell = (XSSFCell) cellItr.next();
System.out.println("CELL:-->"+cell.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
此代码生成的输出是: -
ROW:-->
CELL:-->Column1
CELL:-->Column2
CELL:-->Column3
CELL:-->Column4
CELL:-->Column5
ROW:-->
CELL:-->abc …
Run Code Online (Sandbox Code Playgroud)