「java写入指定路径」java配置文件路径怎么写
本篇文章给大家谈谈java写入指定路径,以及java配置文件路径怎么写对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
在java中如何把一个文件放到指定的文件夹中
首先获得fileoutput对象时,写入具体的目录就可以了。
比如:你要写入到d:\java\test目录下。
方法一:
java代码
string
name
=
"out.html";
string
dir
=
"d:\\java\\test";
file
file
=
new
file(dir,name);
fileoutputstream
out
=
new
fileoutputstream(file);
方法二:
java代码
fileoutputstream
out
=
new
fileoutputstream(dir+"\\"+name);
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复制文件到指定目录? 在线等,急!!!!
借助工具包commons-io.jar
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
public class Admin {
public static void main(String[] args) {
File from = new File("d:/a");
File to = new File("d:/b");
try {
FileUtils.copyDirectory(from, to);
} catch (IOException e) {
e.printStackTrace();
}
}
}
java用户自定义输入文件路径
可以先定义定义一个path路径,之后创建文件,通过“FileOutputStream”创建文件实例,之后过“OutputStreamWriter”流的形式进行来实现自定义文件路径输出,举例:
OutputStreamWriter pw = null;//定义一个流
string path = "D:";//自定义一个路径
pw = new OutputStreamWriter(new FileOutputStream(path+"/test.txt”),"GBK");//确认流的输出文件和编码格式,此过程创建了“test.txt”实例
pw.write("我是要写入到记事本文件的内容");//将要写入文件的内容,可以多次write
pw.close();//关闭流
备注:文件流用完之后必须及时通过close方法关闭,否则会一直处于打开状态,直至程序停止,增加系统负担。
关于java写入指定路径和java配置文件路径怎么写的介绍到此就结束了,不知道你从中找到你需要的信息了吗 ?如果你还想了解更多这方面的信息,记得收藏关注本站。