linuxjava获取文件路径怎么写
1. java linux怎么获取文件路径
一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中, 如何用绝对路径访问呢?
现在建立一个目录:
File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径
File f=new File(fDir,strFile);
2. java在linux下操作文件路径怎么写
一般文件路径在windows中用 \ 表示,但是在其他系统平台下比如linux中就不是 \ 所以java给我们提供了一个与平台无关的表示路径的常量 File.separator在windows中则表示 \ 比如现在有一个文件在D:\java\src\myjava中, 如何用绝对路径访问呢? 现在建立一个目录: File fDir=new File(File.separator); //File.separator表示根目录,比如现在就表示在D盘下。
String strFile="java"+File.separator+"src"+File.separator+"myjava"; //这个就是绝对路径 File f=new File(fDir,strFile);。
3. linux下 Java如何获取文件的绝对路径
需要使用路径时,用下面的方法取得项目根目录的绝对路径(Tools为方法类)
public static String getRootPath() {
String classPath = Tools.class.getClassLoader().getResource("/").getPath();
String rootPath = "";
//windows下
if("\\".equals(File.separator)){
rootPath = classPath.substring(1,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("/", "\\");
}
//linux下
if("/".equals(File.separator)){
rootPath = classPath.substring(0,classPath.indexOf("/WEB-INF/classes"));
rootPath = rootPath.replace("\\", "/");
}
return rootPath;
}
4. 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
java获取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怎么获取文件的相对路径
一个例子,如复果有一个Test文件夹里面有test.java和hello.txt.如果你想用test.java操作制hello.txt只要在test.java中这样写2113File file=new File("hello.txt");//这样就是相对路径。
如果文件结构是Test文件夹。 |------test.java。
|------hello.txt。 |------source文件夹。
.. |---------world.txt如果想5261在test.java中操作world.txt。只要这样写File file=new File("source/world.txt");另外,在web开发中/代表项目文件夹根目录,当然也有可能代替webapps,区分方法是:如果/开头4102的uri是给浏览器解析则/代表webapps,如果是给服务1653器后台解析,则代表项目文件。
3. java 根据文件获取文件名及路径的方法
我写了一段遍历某个文件查找指定文件的,你自己改成你需要的功能。
import java.io.File;
import java.util.HashMap;
public class Test1 {
static HashMap<String, String> filelist=new HashMap<String, String>();
/**
* 递归方法
* @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 < files.length; 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
}
}
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获取文件路径和相关参数
育才学习网