Archive for Java

Properties: load(InputStream inStream)

import java.io.FileInputStream;
import java.util.Properties;

public class MainClass {
public static void main(String args[]) throws Exception {
Properties p = new Properties();
p.load(new FileInputStream(“colon.txt"));
p.list(System.out);
}
}
//File: colon.txt
/*
foo:bar
one
two
three=four
five  six seven eight
nine ten
*/

文章出處:

http://www.java2s.com/Code/JavaAPI/java.util/PropertiesloadInputStreaminStream.htm

發表迴響

getResource Sample

java.net.URL mappingFileUrl = this.class.getResource(“/com/mithra/restful/api/esml/mapping/esmlMapping.xml");

發表迴響

this.class.getResource()用法補充

还需要注意的是,this.class.getresource()方法是以这个类的相对路径去查找的,如果要查找的文件是在这个类的上层,需要改变相对路径
如图这里用ConfigurationUtil类找ScopeToolConfig.xml文件,代码如下
注意要写http://www.cnblogs.com/../XMLConfig  才能找到,因为是以Configuration.java的相对路径作为基点的

 

文章出處:

http://www.cnblogs.com/j3eee/archive/2009/09/14/1566168.html

發表迴響

Class.getResource()怎麼使用

比如我们有以下目录:

|–project

|–src

|–javaapplication

|–Test.java

|–file1.txt

|–file2.txt

|–build

|–javaapplication

|–Test.class

|–file3.txt

|–file4.txt

在上面的目录中,有一个src目录,这是JAVA源文件的目录,有一个build目录,这是JAVA编译后文件(.class文件等)的存放目录

那么,我们在Test类中应该如何分别获得 file1.txt  file2.txt  file3.txt  file4.txt这四个文件呢?

首先讲file3.txtfile4.txt

file3.txt:

方法一:File file3 = new File(Test.class.getResource(“file3.txt").getFile());

方法二:File file3 = new File(Test.class.getResource(“/javaapplication/file3.txt").getFile());

方法三:File file3 = new File(Test.class.getClassLoader().getResource(“javaapplication/file3.txt").getFile());

file4.txt:

方法一:File file4 = new File(Test.class.getResource(“/file4.txt").getFile());

方法二:File file4 = new File(Test.class.getClassLoader().getResource(“file4.txt").getFile());

对于file3.txtfile4.txt,我们可以有多种方法选择,但是file1与file2文件呢?如何获得?

答案是,我们只能写上它们的绝对路径,不能像file3与file4一样用class.getResource()这种方法获得,它们的获取方法如下

假如整个project目录放在c:/下,那么file1与file2的获取方法分别为

file1.txt

方法一:File file1 = new File(“c:/project/src/javaapplication/file1.txt");

方法二:。。。没有

file2.txt

方法一:File file2 = new File(“c:/project/src/file2.txt");

方法二:。。。也没有

总结一下,就是你想获得文件,你得从最终生成的.class文件为着手点,不要以.java文件的路径为出发点,因为真正使用的就是.class,不会拿个.java文件就使用,因为java是编译型语言。至于getResouce()方法的参数,你以class为出发点,再结合相对路径的概念,就可以准确地定位资源文件了.

所以当我们想要使用getResource()方法时,就只好把文件放在.class文件相同目录下或者它的父目录中了。

文章出處:

http://www.cnblogs.com/j3eee/archive/2009/09/14/1566110.html

發表迴響

getParentFile()、getPath()、getAbsolutePath()與getCanonicalPath()

getParentFile(): Returns the parent directory as a File object, or null if this File object does not have a parent.

getPath()返回一个绝对的路径的字符串();
getAbsolutePath()   返回一个完整的路径字符串
getCanonicalPath()返回一个规范的路径(区分大小写)

發表迴響

getServletContext 與 getRealPath

String myJrxmlFile = this.getServletContext().getRealPath(myJrxmlWebPath);

getServletContext() : Returns a reference to the ServletContext in which the caller is executing.
getRealPath(java.lang.String path)
Returns a String containing the real path for a given virtual path.

這樣可以輕易的把我們要用的檔案拿來使用.

發表迴響

ToStringBuilder和ToStringStyle

则需要使用ToStringBuilder和ToStringStyle这两个类。
ToStringBuilder类中的append方法可以向该类添加基本类型、数组和对象 ,只有添加的方法才会被toString方法输出。
ToStringStyle类则是对输出的内容进行格式化。

public String toString() …{
return new ToStringBuilder(this, ToStringStyle.MULTI_LINE_STYLE)
.append(“name", name)
.append(“age", age)
.toString();
}

發表迴響

Java 教程《語法說明》instanceof

instanceof
object instanceof ClassName
[說明]
如果某個 object 屬於該 ClassName(或其衍生類別)的 instance,則傳回true;否則傳回 false。

發表迴響

從HashMap中取得 key 與 鍵值 的比對

Map map = new LinkedHashMap();

//這邊回傳一個Iterator, 注意map使用的方法, 可從eclipse中找尋得到Iterator的method

java.util.Iterator itr = map.keySet().iterator();

while( itr.hasNext() ){
String key = (String)itr.next();
String val = (String)map.get(key);

System.out.println(“key is : " + key + " , val is : " + val);
}

發表迴響

資料庫Realm的寫法

一般專案連結資料庫的方式, 如果是採用<Realm…的寫法

則必須要在TOMCAT中放置連結資料庫的jar檔 (如Postgres與Oracle的Driver)

在專案中放置是沒有用的.

因為Realm的連線方式, 是利用tomcat去連線資料庫, 不是由web application去連線.

發表迴響

Older Posts »
Follow

Get every new post delivered to your Inbox.