java读取src文件路径怎么写
1.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.war2: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/classesthis.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/classes3:Thread.currentThread().getContextClassLoader().getResource(".").getPath()Thread.currentThread().getContextClassLoader().getResource("").getPath() 得到的是 ClassPath的绝对URI路径。
如:/D:/myProjects/hp/WebRoot/WEB-INF/classesThread.currentThread().getContextClassLoader().getResource("/").getPath() X 不 能运行最后在Web应用程序中,我们一般通过ServletContext.getRealPath("/")方法得到Web应用程序的根目录的绝对路径。还有request.getContextPath(); 在Weblogic中要用request.getServletContext().getContextPath();但如果打包成war部署到Weblogic服务器,项目内部并没有文件结构的概念,用这种方式是始终得到null,获取不到路径,目前还没有找到具体的解决方案。
2.java获取某个文件夹的路径怎么写
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\baidu");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\baidu
G:\xhuoj\konw\src\baidu
3.编写java程序,读取本程序的源文件,从bin目录到src目录怎么写呢
1.读取源文件路径文件路径
类.getClass.getResource("/").getPath();
此处的路径是: 类编译后的Class文件的绝对路径.
2.此处是一个COPY的文件代码片段
FileInputStream oldPutFile = new FileInputStream(file);
FileOutputStream newPutFile = new FileOutputStream(copyFile);
byte[] buf = new byte[1024];
int i = 0;
while ((i = oldPutFile.read(buf)) != -1) {
newPutFile.write(buf, 0, i);
}
oldPutFile.close();
newPutFile.close();
希望我的回答能帮助到你.
4.java项目如何获取src目录以外的目录文件啊
src是根据class编译环境的相对路径查找的,外面的路径可以使用绝对路径。
绝对路径是指文件在硬盘上真正存在的路径。例如“bg.jpg”这个图片是存放在硬盘的“E:\book\网页布局代码\第2章”目录下,那么 “bg.jpg”这个图片的绝对路径就是“E:\book\网页布\代码\第2章\bg.jpg"。
为了避免这种隋况发生,通常在网页里指定文件时,都会选择使用相对路径。所谓相对路径,就是相对于自己的目标文件位置。例如上面的例子,“s1.htm” 文件里引用了“bg.jpg”图片,由于“bg.jpg”图片相对于“s1.htm”来说,是在同一个目录的,那么要在“s1.htm”文件里使用以下代 码后,只要这两个文件的相对位置没有变(也就是说还是在同一个目录内),那么无论上传到Web服务器的哪个位置,在浏览器里都能正确地显示图片。
java读取文件路径怎么写
1. java web中读取文件,相对路径怎么写
相对路径的话,可以先获取到当前文件的编译路径,之后在找到想找文件的路径的思路来实现。
举例:
XMLS.class.getClass().getResourceAsStream("/test/test.txt");
解释:XMLS.class.getClass()是获取当前的类编译路径,之后通过getResourceAsStream的形式即可找到要读取的文件的路径。
备注:这个方法中后面的路径也可以通过截取的形式来进行路径获取,实现原理都是找到当前类路径,之后通过相对位置找到另外文件路径。
2. java获取某个文件夹的路径怎么写
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。下面是一个具体例子:
public class PathTest
{
public static void main(String[] args)
{
File file = new File(".\\src\\baidu");
System.out.println(file.getAbsolutePath());
try
{
System.out.println(file.getCanonicalPath());
} catch (IOException e)
{
e.printStackTrace();
}
}
}
getAbsolutePath()和getCanonicalPath()的不同之处在于,getCanonicalPath()得到的是一个规范的路径,而getAbsolutePath()是用构造File对象的路径+当前工作目录。例如在上面的例子中.(点号)代表当前目录。getCanonicalPath()就会把它解析为当前目录但是getAbsolutePath()会把它解析成为目录名字(目录名字是点号)。
下面是上面程序在我电脑上的输出:
G:\xhuoj\konw\.\src\baidu
G:\xhuoj\konw\src\baidu
3. java读取文件路径
你的头是e://tttt11.PNG不是e://tttt11//1.PNG???如果头是e://tttt11//1.PNG,filepath没有用,去掉。
这段这么改:for (i=0; i <= str.length(); i += 2) { if (i == str.length() - 1 || i == str.length() - 2) break; else fileName = str.substring(i); } // System.out.println(filePath); if(ii!=100) fileName = str.substring(i);。
..后面不改.如果头是e://tttt11.PNG,那这个和下面的循环规律不一样,大概写下:if(ii==1)filePath=".PNG";把我上面修改后的代码加到else里就行了(用我上面修改后的代码,不然你的尾还是显不出来)。
4. java 如何获取文件路径
public void doGet(HttpServletRequest request ,HttpServletResponse response )
throws ServletException ,IOException{
OutputStream out;//输出响应正文的输出流
InputStream in; //读取本地文件的输入流
//获取filename 请求参数
String filename =requeset.getParameter("filename");
if(filename==null){
out=response.getOutputStream();
out.write("please input filename.".getBytes());
out.close;
return;
}
//获得读取本地文件的输入流
in=getServletContext().getResourceAsStream("/store"+filename);
int length=in.available();
}
5. java怎么通过文件的路径读取文件
package file.system.demo.exception;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFile {
public static String getFile(String realpath) {
Scanner scanner = null;
String text = "";
try {
File file= new File(realpath);
scanner = new Scanner(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if(scanner!=null){
while(scanner.hasNextLine()){
text+=scanner.nextLine();
}
scanner.close();
}
//System.out.println(text);
return text;
}
static class InnerTest{
public static void main(String[] args) {
String realpath = "D:\\test.txt";
String text=getFile(realpath);
System.out.println(text);
}
}
}
实现方式有很多,还可以用字节流FileInputStream,字符流FileReader等方式读取
转载请注明出处育才学习网 » java读取src路径下的txt文件
育才学习网