「java读取项目文件」java读取资源文件

博主:adminadmin 2023-01-27 23:39:10 256

本篇文章给大家谈谈java读取项目文件,以及java读取资源文件对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

java怎么读取同一个工程里面的src目录下的文件?

在java中获得文件的路径在我们做上传文件操作时是不可避免的。

web 上运行

1:this.getClass().getClassLoader().getResource("/").getPath();

this.getClass().getClassLoader().getResource("").getPath(); 得到的是 ClassPath的绝对URI路径。

如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/

System.getProperty("user.dir");

this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 项目的绝对路径。

如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

2:this.getClass().getResource("/").getPath();

this.getClass().getResource("").getPath(); 得到的是当前类 文件的URI目录。

如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/com/jebel/helper/

this.getClass().getResource(".").getPath(); X 不 能运行

3:Thread.currentThread().getContextClassLoader().getResource("/").getPath()

Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。

如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war/WEB-INF/classes/

Thread.currentThread().getContextClassLoader().getResource(".").getPath() 得到的是 项目的绝对路径。

如:/D:/jboss-4.2.2.GA/server/default/deploy/hp.war

在本地运行中

1:this.getClass().getClassLoader().getResource("").getPath();

this.getClass().getClassLoader().getResource(".").getPath(); 得到的是 ClassPath的绝对URI路径。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

this.getClass().getClassLoader().getResource(".").getPath(); X 不 能运行

2:this.getClass().getResource("").getPath();

this.getClass().getResource(".").getPath(); 得到的是当前类 文件的URI目录。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes/com/jebel/helper/

/D:/myProjects/hp/WebRoot/WEB-INF/classes/ 得到的是 ClassPath的绝对URI路径。

如:/D:/myProjects/hp/WebRoot/WEB-INF/classes

java项目中文件的上传与读取

互联网项目一般会有单独的服务器存放静态资源,图片就是一种静态资源,在这里就是区别于项目部署的另一台服务器。这时候你项目里面都是使用相对路径,像你上面所说的常量/opt/upload/这种做法是正确的,上传图片的时候,常见的有使用日期分目录存储的,如/opt/upload/2014/11/03/***.jpg,对于图片的路径,数据库里一般来说保存到2014/11/03/***.jpg就可以了。这是存图片。

取图片,一般来说资源都必须发布服务才能让外网访问。例如,你可以在你项目中写个servlet用来读取图片,如下面的服务地址***.jpg,其中2014前面的路径是固定的,后面的是你数据库里存储的图片地址,这时你页面代码里面只需固定前缀 + 图片相对地址则可将图片读出来。

上面这种方法用的其实比较少,一般来说静态服务器都会部署一个web容器,然后使用单独的域名,打个比方,你在静态服务器上有个tomcat,目录/opt/tomcat/webapp/staticprojectname,staticprojectname是工程名,然后在上传的所有图片在staticprojectname下面,例如/opt/tomcat/webapp/staticprojectname/2014/11/03/***.jpg,然后在你原工程里面直接使用http://静态服务ip:port/staticprojectname/2014/11/03/***.jpg就可以访问到图片了,同样的在你代码里面2014前面的地址是固定的,配置成常量,后面的则是数据库里存的图片相对地址。

说了这么多,有点乱,希望你能明白

JAVA开发读取文件的方法有哪些

/**

* 根据提供地址读取文件返回字符串

* @param filePath

* @return 文件字符串

*/

private String readFile(String filePath){

File javaFile = new File(filePath);

StringBuffer fileStr = new StringBuffer();//存储杜浒的文件字符串,.

int b;

InputStream fileIns = null;

InputStreamReader fileReder = null;

try {

fileIns = new FileInputStream(javaFile);

fileReder = new InputStreamReader(fileIns, "utf-8");

while ((b = fileReder.read()) != -1) {

fileStr.append((char) b);

}

// System.out.println(javaStr.toString());

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (fileReder != null) {

fileReder.close();

}

if (fileIns != null) {

fileIns.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

if(fileStr.length()0){

return fileStr.toString();

}else{

return "";

}

// System.out.println(result);

}

仅供参考!

如何读取Java项目不同路径的配置文件

public String getAbsolutepath() {

   String fileUrlPath = FileL.getPathPart(this.getClass().getResource("/")

         .getPath());

   String os = System.getProperty("os.name");

   if (os.toLowerCase().startsWith("win")) {

      fileUrlPath = fileUrlPath.substring(1, fileUrlPath.length());

   }

   return fileUrlPath;

}

java中如何从当前项目中读取运行另一项目中的配置文件?

使用url统一资源定位符,前提是需要有访问文件的权限。

import java.io.File;

import java.net.URL;

import org.apache.commons.io.FileUtils;

public class DownloadURLFile {

/**

 * @param args

 */

public static void main(String[] args) {

String res = downloadFromUrl("","d:/");

System.out.println(res);

}

public static String downloadFromUrl(String url,String dir) {

try {

URL httpurl = new URL(url);

String fileName = getFileNameFromUrl(url);

System.out.println(fileName);

File f = new File(dir + fileName);

FileUtils.copyURLToFile(httpurl, f);

} catch (Exception e) {

e.printStackTrace();

return "Fault!";

return "Successful!";

}

public static String getFileNameFromUrl(String url){

String name = new Long(System.currentTimeMillis()).toString() + ".X";

int index = url.lastIndexOf("/");

if(index  0){

name = url.substring(index + 1);

if(name.trim().length()0){

return name;

}

}

return name;

}

}

java文件怎么获取项目下的配置文件

package utils;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

public class PropertieUtil {

Properties pro = null;

public PropertieUtil(){

this.pro = new Properties();

}

public Properties getProperties(){

return this.pro;

}

public Properties load(String fileName){

FileInputStream in = null;

try {

fileName = String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", "") + fileName;

in = new FileInputStream(fileName);

pro.load(in);

} catch (FileNotFoundException e) {

System.out.println("==================================");

System.out.println(fileName+"配置文件不存在,请联系管理员");

System.out.println("异常信息:"+e);

} catch (IOException e) {

System.out.println("==================================");

System.out.println("读取"+fileName+"配置文件时发生异常,请联系管理员");

System.out.println("异常信息:"+e);

}

return pro;

}

public static void main(String[] args) {

FileInputStream in = null;

System.out.println(String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", ""));

String fileName = String.valueOf(PropertieUtil.class.getResource("/")).replace("file:/", "") + "fileconfig.properties";

try {

in = new FileInputStream(fileName);

System.out.println(""+true);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

上面是读取.properties后缀的文件,你想要这种吗?

希望能够帮到你!

关于java读取项目文件和java读取资源文件的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。