资讯

精准传达 • 有效沟通

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

java路径代码 java 路径规划

java中怎么写文件路径

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

目前创新互联建站已为数千家的企业提供了网站建设、域名、网络空间、网站运营、企业网站设计、张家口网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。

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

我建议你用

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

System.out.println(path);

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

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

test

|

src

|

t090417

|

test.properties

Read.java

test.properties:

TEST=test

Read.java:

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.IOException;

import java.util.Properties;

public class Read {

public static String TEST ;

private static Properties loadPropertyFile() throws FileNotFoundException,IOException{

Properties properties = new Properties() ;

FileInputStream fs = new FileInputStream("src/t090417/test.properties");

properties.load(fs);

return properties ;

}

public static void loadProperty(){

try{

Properties properties = loadPropertyFile();

TEST = properties.getProperty("TEST");

System.out.println("read from properties: "+TEST);

}catch(Exception e){

e.printStackTrace();

}

}

public static void main(String[] args) {

loadProperty();

}

}

其中用的就是相对路径!

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代码中文件路径的问题

这就是相对路径

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

在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路径代码 java 路径规划
网站URL:http://cdkjz.cn/article/dodsihs.html
多年建站经验

多一份参考,总有益处

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

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

大客户专线   成都:13518219792   座机:028-86922220