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 如何获取文件路径
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();}。
3. java里文件路径怎么写
File file = new File("D:\\123.txt");
你这种不用绝对路径是不行的,
只有一个方法,在web工程启动servlet中获取到webroot路径,在servlet的init中使用String webRoot = getServletContext().getRealPath("/");获取,然后使用这webRoot变量追加路径,再new File(),这样的话要求就是,你的服务必须要启动,否则不会init,无法得到工程发布目录的相对路径
4. 在java中怎么获得,本文件的路径
File的getPath方法得到相对路径 getAbsolutePath方法得到绝对路径举个例子String fileName = "yourfile.txt";File aFile = new File(fileName);//这里可以把路径拼在fileName前面 可以用相对路径 也可以用绝对 注意分隔符System.out.println(aFile.getPath()); //相对路径System.out.println(aFile.getAbsolutePath()); //绝对路径。
转载请注明出处育才学习网 » java获取本地文件路径怎么写