您的位置:首页 > 其它

获取文件内容

2016-09-22 11:08 363 查看
获取当前项目的路径:

ClassLoaderTest.class.getClassLoader().getResourceAsStream("log4j.properties");

// 代码示例

package com.etongdai.depository.ctr.base;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

public class ClassLoaderTest {

    public static void main(String args[]){

    ClassLoader test =Thread.currentThread().getContextClassLoader();

   

      System.out.println("parent.."+test.getParent());

    System.out.println("parent..parent"+test.getParent().getParent());

    String src =Class.class.getResource("/").getFile();

    String pp=src=System.getProperty("user.dir");

    URL  yy =Thread.currentThread().getContextClassLoader().getResource("");

    //InputStream  aa =Class.class.getResourceAsStream("log4j.properties");

    InputStream aa =ClassLoaderTest.class.getClassLoader().getResourceAsStream("log4j.properties");

   

        try {

        //getClassLoader().getResourceAsStream("beans.xml")

        // InputStream  aa =new FileInputStream("D:/wpl/www.txt");

        byte[] bb =new byte[1024];

        int len =0;

        int temp =0;

            while((temp =aa.read())!=-1){

              bb[len]=(byte)temp;

              len++;

            }

            aa.close();
System.out.println(new String(bb,0,len)+"....test.."+yy.getPath());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

   

   

    }

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: