您的位置:首页 > 其它

读写文件及创建目录

2011-04-11 16:46 176 查看
public static void main(String[] args) {
String filePath="D:/java/move2/move2.txt";
File file = new File(filePath);
if(!file.getParentFile().exists()){
//			如果目录不存在
file.getParentFile().mkdirs();//创建目录
try {
file.createNewFile();//再创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
writeFile(filePath);
readFile(filePath);
}
/**
* 写文件
*/
public static void writeFile(String path){
try {
BufferedWriter bw =new BufferedWriter(new FileWriter(path));
bw.write("hello fileWriter :");
bw.newLine();
bw.write("/t"+new Date().toString());
bw.newLine();
bw.write("add string! ");
bw.write("/n");
bw.write("追加/"//n/"");
bw.newLine();
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 读文件
*/
public static void readFile(String path){
String content="";
try {
BufferedReader br = new BufferedReader(new FileReader(path));
try {
content = br.readLine();
while(content!=null&&!content.equals("")){
System.out.println(content);
content=br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
System.err.println("地址文件不存在……"+path);
//			e.printStackTrace();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  string path file date null