java读取文件的路径怎么写
1. 如何查看java读取文件的路径
File类有两个常用方法可以得到文件路径一个是:getCanonicalPath(),另一个是:getAbsolutePath(),可以通过File类的实例调用这两个方法例如file.getAbsolutePath()其中file是File的实例对象。
下面是一个具体例子:123456789101112131415public 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\baiduG:\xhuoj\konw\src\baidu。
2. java web中读取文件,相对路径怎么写
相对路径的话,可以先获取到当前文件的编译路径,之后在找到想找文件的路径的思路来实现。
举例:
XMLS.class.getClass().getResourceAsStream("/test/test.txt");
解释:XMLS.class.getClass()是获取当前的类编译路径,之后通过getResourceAsStream的形式即可找到要读取的文件的路径。
备注:这个方法中后面的路径也可以通过截取的形式来进行路径获取,实现原理都是找到当前类路径,之后通过相对位置找到另外文件路径。
3. 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
4. java文件读写的文件应该放哪个路径
1。
如果你用的eclipse,那么project>>properties>>打开属性设置窗口有一项java build path,点开最下面有个default output folder,那里有路径,比如:axis2如果你要读的文件名字为1.txt的话你就把它放到C:\eclipse\workspaces\axis2\下面就行了。2。
如果你直接用cmd javac编译的那么假设你的java文件放在*axis2\Exam9\FileInputStream.java编译好的class文件也在同一个目录执行程序要在*axis2\目录java Exam9.FileInputDemo而你的文件要放在*axis2\Exam9\ 3。也可以用绝对路径。
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读取文件路径怎么写
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获取文件的路径怎么写
1. 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
2. java中获取文件路径的几种方式
获取当前类的所在工程路径;如果不加“/”File f = new File(this.getClass().getResource("").getPath());System.out.println(f);结果:C:\Documents%20and%20Settings\Administrator\workspace\projectName\bin\com\test获取当前类的绝对路径;第二种:File directory = new File("");//参数为空String courseFile = directory.getCanonicalPath() ;System.out.println(courseFile);结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前类的所在工程路径;第三种:URL xmlpath = this.getClass().getClassLoader().getResource("selected.txt");System.out.println(xmlpath);结果:file:/C:/Documents%20and%20Settings/Administrator/workspace/projectName/bin/selected.txt获取当前工程src目录下selected.txt文件的路径第四种:System.out.println(System.getProperty("user.dir"));结果:C:\Documents and Settings\Administrator\workspace\projectName获取当前工程路径第五种:System.out.println( System.getProperty("java.class.path"));结果:C:\Documents and Settings\Administrator\workspace\projectName\bin获取当前工程路径。
3. java 根据文件获取文件名及路径的方法
我写了一段遍历某个文件查找指定文件的,你自己改成你需要的功能。
import java.io.File;
import java.util.HashMap;
public class Test1 {
static HashMapfilelist=new HashMap();
/**
* 递归方法
* @param path 文件路径
*/
public static void find(String path){
File file=new File(path);
File[] files = file.listFiles();
//如果文件数组为null则返回
if (files == null)
return;
for (int i = 0; i if (files[i].isDirectory()) {
//判断是不是文件夹,如果是文件夹则继续向下查找文件
find(files[i].getAbsolutePath());
} else {
//记录文件路径
String filePath = files[i].getAbsolutePath().toLowerCase();
//记录文件名
String fileName=files[i].getName().toLowerCase();
// System.out.println("---"+strFileName);
filelist.put(fileName, filePath);
}
}
}
public static void main(String[] args) {
//需要遍历的路径,也就是你要查找文件所在的路径
String path="D:\\kpi\\";
find(path);
System.out.println("kpi.9的路径:"+filelist.get("kpi.9"));
//输出结果:d:\kpi\kpi.9
}
}
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();
}
java的文件路径怎么写
1. Java 文件路径的不同写法
首先先澄清 这个三个都是绝对路径 没有相对路径, E://test.txt 和 E:/test.txt 是一个意思 E:\\text.txt 用这样的写法是 \\ 转义了 ‘\’ .
为什么有\\ 的写法 和 / 这样的写法呢 ,因为window 默认的是\\ 这样的写法 如果你把这样的写法放到了 linux系统下面,那么这样的写法就是错误的了,linux 系统下面只认识/ 的写法。
File file = New File("E://test.txt")、
File file = New File("E:\\test.txt")、
File file = New File("E:/test.txt")
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项目中文件的路径
java项目中文件的路径-方法大全 一、相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) System.getProperty("user.dir"); 上述相对路径中,java项目中的文件是相对于项目的根目录web项目中的文件路径视不同的web服务器不同而不同(tomcat是相对于tomcat安装目录\bin)二 类加载目录的获得(即当运行时某一类时获得其装载目录)1.1)通用的方法一(不论是一般的java项目还是web项目,先定位到能看到包路径的第一级目录)InputStreamis=TestAction.class.getClassLoader().getResourceAsStream("test.txt"); (test.txt文件的路径为 项目名\src\test.txt;类TestPath所在包的第一级目录位于src目录下) 上式中将TestPath,test.txt替换成对应成相应的类名和文件名字即可1.2)通用方法二 (此方法和1.1中的方法类似,不同的是此方法必须以'/'开头) InputStream is=Test1.class.getResourceAsStream("/test.txt"); (test.txt文件的路径为 项目名\src\test.txt,类Test1所在包的第一级目录位于src目录下) 三 web项目根目录的获得(发布之后)1 从servlet出发 可建立一个servlet在其的init方法中写入如下语句(没有请求的话会抛空指针导常) ServletContext s1=this.getServletContext();String temp=s1.getRealPath("/"); (关键) 结果形如:F:\tomcat-6.0.36\webapps\test\(test为项目名字) 如果是调用了s1.getRealPath("")则输出F:\tomcat-6.0.36\webapps\test(少了一个"\") 2 从.chinacreator.report.listener; import javax.servlet.ServletContext; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; /** * @authorxiaoqun.yi */ public class PathListener implementsServletContextListener { private staticServletContext servletContext; public voidcontextDestroyed(ServletContextEvent sce) { this.servletContext= sce.getServletContext(); System.out.println("path=======:"+servletContext.getRealPath("/")); } public voidcontextInitialized(ServletContextEvent arg0) { } } 在web.xml中加入如下配置 Java代码 :
转载请注明出处育才学习网 » java中文件路径怎么写
育才学习网