资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

java文件路径代码 java在指定路径生成文件

关于java代码中文件路径的问题

这就是相对路径

创新互联专业为企业提供玉泉网站建设、玉泉做网站、玉泉网站设计、玉泉网站制作等企业网站建设、网页设计与制作、玉泉企业网站模板建站服务,十年玉泉做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。

指的是相对于工程文件的位置而言

在eclipse的结构图中的位置

在windows的文件夹里的位置

在查看属性里的绝对路径的位置

代码来找文件路径

public class Test {

public static void main(String[] args) throws Exception {

System.out.println("当前目录的路径\t"+new File(".").getCanonicalPath());// "."表示当前目录

File file = new File("Buffered.txt");

if(!file.exists()){//如果不存在,就新建该文件

file.createNewFile();

}

System.out.println("Buffered.txt的绝对路径\t"+file.getCanonicalPath());

System.out.println("Buffered.txt的相对路径\t"+file.getPath());

}

}

输出

当前目录的路径 D:\space\workspace\Demo

Buffered.txt的绝对路径 D:\space\workspace\Demo\Buffered.txt

Buffered.txt的相对路径 Buffered.txt

谁告诉我java中 读取文件时想用相对路径,代码怎么写?

1绝对路径:加上盘符,就是绝对正确的地址,一般通过我的电脑打开到那个位置,复制地址就可以。

2相对路径:

a;同级目录: 直接填写文件名称;

b;下级链接:带上文件夹,写上文件名称;

c:上级链接:"..\文件名称"

java中怎么写文件路径

第一个 : "C:\\mydoc\\aa.doc" , 这个用双斜线

第二个 : "C:/mydoc/aa.doc" ,这个单斜线就行了

我建议你用

String path = "C:"+File.separator+"my.doc" ;

System.out.println(path);

File.separator  这是用你所用的系统默认的文件分割符,,

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中怎么把文件上传到服务器的指定路径?

文件从本地到服务器的功能,其实是为了解决目前浏览器不支持获取本地文件全路径。不得已而想到上传到服务器的固定目录,从而方便项目获取文件,进而使程序支持EXCEL批量导入数据。

java中文件上传到服务器的指定路径的代码:

在前台界面中输入:

form method="post" enctype="multipart/form-data"  action="../manage/excelImport.do"

请选文件:input type="file"  name="excelFile"

input type="submit" value="导入" onclick="return impExcel();"/

/form

action中获取前台传来数据并保存

/**

* excel 导入文件

* @return

* @throws IOException

*/

@RequestMapping("/usermanager/excelImport.do")

public String excelImport(

String filePath,

MultipartFile  excelFile,HttpServletRequest request) throws IOException{

log.info("action:{} Method:{} start","usermanager","excelImport" );

if (excelFile != null){

String filename=excelFile.getOriginalFilename();

String a=request.getRealPath("u/cms/www/201509");

SaveFileFromInputStream(excelFile.getInputStream(),request.getRealPath("u/cms/www/201509"),filename);//保存到服务器的路径

}

log.info("action:{} Method:{} end","usermanager","excelImport" );

return "";

}

/**

* 将MultipartFile转化为file并保存到服务器上的某地

*/

public void SaveFileFromInputStream(InputStream stream,String path,String savefile) throws IOException

{    

FileOutputStream fs=new FileOutputStream( path + "/"+ savefile);

System.out.println("------------"+path + "/"+ savefile);

byte[] buffer =new byte[1024*1024];

int bytesum = 0;

int byteread = 0;

while ((byteread=stream.read(buffer))!=-1)

{

bytesum+=byteread;

fs.write(buffer,0,byteread);

fs.flush();

}

fs.close();

stream.close();

}


文章标题:java文件路径代码 java在指定路径生成文件
当前路径:http://cdkjz.cn/article/dododch.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220