编程语言
首页 > 编程语言> > java本地文件读写

java本地文件读写

作者:互联网

String  strFilePath="本地路径"

1.获取文件名

File tempFile =new File( strFilePath.trim());
String fileName = tempFile.getName();

2.复制本地文件

String mburl="docmb\\temp.doc";
String dest ="目标路径"  //精确到文件名
File source = new File(mburl);
File dest = new File(zwurl);
try {
FileUtils.copyFile(source, dest);          //路径中无文件夹  自动创建
} catch (IOException e) {
e.printStackTrace();

3.检测路径是否合法

 File file = new File(path);

if (!file.exists())
{}

4.将本地文件转化为字节流

response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream");

String fileName = tempFile.getName();
response.addHeader("Content-Disposition", "attachment; filename*=utf-8'zh_cn'"+fileName);

InputStream inStream=new FileInputStream(strFilePath);// 文件的存放路径
byte[] b = new byte[100];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}

标签:java,本地,读写,路径,inStream,File,new,response,String
来源: https://www.cnblogs.com/ccs1998/p/15836163.html