Java目录和文件操作整理_KevinFFK_新浪博客
创建时间:
2016/6/22 12:00
来源:
http://blog.sina.com.cn/s/blog_507e84890100ch8q.html
Java目录和文件操作整理
(2009-04-14 14:45:12)
转载
▼
标签:
杂谈
分类:
java
public
class
TestFile
{
public
static
void
main(String[]
args)
throws
IOException
{
//
1.获取当前目录
//
当前目录的URI
System.out.println(Thread.currentThread().getContextClassLoader().
getResource(
""
));
System.out.println(ClassLoader.getSystemResource(
""
));
//
当前目录绝对路径
System.out.println(
new
File(
""
).getAbsolutePath());
System.out.println(
new
File(
""
).getCanonicalPath());
System.out.println(System.getProperty(
"user.dir"
));
//
2.文件的创建/删除
String
pwd=System.getProperty(
"user.dir"
);
File
f
=
new
File(pwd,
"1.txt"
);
if
(f.exists())
{
f.delete();
System.out.println(pwd
+
"\\1.txt
存在,已删除。"
);
}
else
{
f.createNewFile();
System.out.println(pwd
+
"\\1.txt
不存在,已建立。"
);
}
//
3.目录的创建/删除
pwd
=
pwd
+
"\\Sub"
;
//
将要建立的目录路径
File
d
=
new
File(pwd);
//
建立代表Sub目录的File对象,并得到它的一个引用
if
(d.exists())
{
//
检查Sub目录是否存在
d.delete();
System.out.println(
"Sub目录存在,已删除"
);
}
else
{
d.mkdir();
//
建立Sub目录
System.out.println(
"Sub目录不存在,已建立"
);
}
//
4.获取目录下的所有子目录和文件
File
d1=
new
File(System.getProperty(
"user.dir"
));
//
建立当前目录中文件的File对象
File
list[]=d1.listFiles();
//
取得代表目录中所有文件的File对象数组
System.out.println(pwd+
"
目录下的内容:"
);
for
(
int
i=
0
;i<list.length;i++)
if
(list[i].isDirectory()){
String
tmp=String.format(
"%s
<DIR>
%s"
,
new
SimpleDateFormat(
"yyyy-MM-dd
HH:mm"
).format(
new
Date(list[i].lastModified())).toString(),list[i].getName());
System.out.println(tmp);
}
else
if
(list[i].isFile()){
String
tmp=String.format(
"%s
%5s
bytes
%s"
,
new
SimpleDateFormat(
"yyyy-MM-dd
HH:mm"
).format(
new
Date(list[i].lastModified())).toString(),list[i].length(),
list[i].getName());
System.out.println(tmp);
}
//
5.写文件
pwd=System.getProperty(
"user.dir"
);
FileWriter
fw
=
new
FileWriter(pwd
+
"\\2.txt"
);
fw.write(
"aaaa"
);
fw.write(
"bbbb"
+System.getProperty(
"line.separator"
));
fw.write(
"cccc"
);
fw.close();
fw
=
new
FileWriter(pwd
+
"\\2.txt"
,
true
);
//
append=true
BufferedWriter
bw=
new
BufferedWriter(fw);
bw.write(
"dddd"
);
bw.newLine();
bw.write(
"eeee"
);
bw.close();
//
6.读文件
FileReader
fr
=
new
FileReader(pwd
+
"\\2.txt"
);
BufferedReader
br
=
new
BufferedReader(fr);
String
Line
=
br.readLine();
while
(Line
!=
null
)
{
System.out.println(Line);
Line
=
br.readLine();
}
br.close();
fr.close();
readFromConsole();
}
//
从控制台读入
public
static
void
readFromConsole()
{
try
{
BufferedReader
in
=
new
BufferedReader(
new
InputStreamReader(System.in));
System.out.print(
"input
sth:"
);
String
temp
=
in.readLine();
System.out.print(
"your
input
is:
\""
+temp+
"\""
);
}
catch
(IOException
ioe)
{
ioe.printStackTrace();
}
}
//
简单的文件复制
public
void
copyFile(String
src,String
dest)
throws
IOException{
FileInputStream
in=
new
FileInputStream(src);
File
file=
new
File(dest);
if
(!file.exists())
file.createNewFile();
FileOutputStream
out=
new
FileOutputStream(file);
int
c;
byte
buffer[]=
new
byte
[
1024
];
while
((c=in.read(buffer))!=-
1
){
for
(
int
i=
0
;i<c;i++)
out.write(buffer[i]);
}
in.close();
out.close();
}
//
删除非空文件夹
public
static
boolean
delDir(String
path)
throws
IOException{
if
(path
==
null
||
path
==
""
)
return
false
;
File
rootDir=
new
File(path);
File[]
fileLst=rootDir.listFiles();
for
(
int
i=
0
;
i<fileLst.length;
i++)
{
if
(fileLst[i].isDirectory()){
if
(!delDir(fileLst[i].toString()))
return
false
;
System.out.println(
"dir:
"
+fileLst[i]+
"
is
deleted."
);
}
else
if
(fileLst[i].isFile()){
if
(!fileLst[i].delete())
return
false
;
System.out.println(
"file:
"
+fileLst[i]+
"
is
deleted."
);
}
}
if
(!rootDir.delete())
return
false
;
return
true
;
}
}
分享:
2
喜欢
0
赠金笔
阅读
(2516)
┊
评论
(0)
┊
收藏
(0)
┊
转载
(1)
┊
喜欢
▼
┊
打印
┊
举报
已投稿到:
排行榜
前一篇:
System.getProperty(String key)
后一篇:
JSP隐含对象所对应的servlet的API
.
评论
重要提示:警惕虚假中奖信息
[
发评论
]
评论加载中,请稍候...
发评论
登录名:
密码:
找回密码
注册
记住登录状态
分享到微博
评论并转载此博文
按住左边滑块,拖动完成上方拼图
点击上图按钮并沿道路拖动到终点处
移动到此开始验证
发评论
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。
< 前一篇
System.getProperty(String key)
后一篇 >
JSP隐含对象所对应的servlet的API
.