系统变量中设定文件路径
这是一个小例子,经过测试过的。
package com.surekam.mpm.casemanagement.web.test;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class PropertyTest {
public static void main(String[] args) throws IOException {
System.setProperty("java.io.tmpdir", "d:\\test\\test");
System.out.println(System.getProperty("java.io.tmpdir"));
String filePath = System.getProperty("java.io.tmpdir");
File file = new File(filePath);
System.out.println(file.exists());
if(!file.exists()){
file.mkdirs();
}
file = null;
file = new File(filePath+"\\test.txt");
System.out.println(file.toURI().toASCIIString()+" "+file.toURL().toString());
try {
OutputStream output = new FileOutputStream(file);
output.write("我是一个中国人。".getBytes());
output.flush();
output.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}