【案例1】创建一个新文件
1
2
3
4
5
6
7
8
9
10
11
|
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
File f=
new
File(
"D:\\hello.txt"
);
try
{
f.createNewFile();
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
【运行结果】:
程序运行之后,在d盘下会有一个名字为hello.txt的文件。
【案例2】File类的两个常量
1
2
3
4
5
6
7
|
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
System.out.println(File.separator);
System.out.println(File.pathSeparator);
}
}
|
【运行结果】:
\
;
1
2
3
4
5
6
7
8
9
10
11
12
|
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
try
{
f.createNewFile();
}
catch
(Exception e) {
e.printStackTrace();
}
}
}
|
删除一个文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* 删除一个文件
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
if
(f.exists()){
f.delete();
}
else
{
System.out.println(
"文件不存在"
);
}
}
}
|
创建一个文件夹
1
2
3
4
5
6
7
8
9
10
11
|
/**
* 创建一个文件夹
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator+
"hello"
;
File f=
new
File(fileName);
f.mkdir();
}
}
|
【运行结果】:
D盘下多了一个hello文件夹
列出指定目录的全部文件(包括隐藏文件):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
/**
* 使用list列出指定目录的全部文件
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator;
File f=
new
File(fileName);
String[] str=f.list();
for
(
int
i =
0
; i < str.length; i++) {
System.out.println(str[i]);
}
}
}
|
【运行结果】:
$RECYCLE.BIN
360
360Downloads
360Rec
360SoftMove
Config.Msi
da
Downloads
DriversBackup
eclipse
java web整合开发和项目实战
Lenovo
MSOCache
Program
Program Files
python
RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}
System Volume Information
Tomcat6
var
vod_cache_data
新建文件夹
(你的运行结果应该和这个不一样的,呵呵)
但是使用list返回的是String数组,。而且列出的不是完整路径,如果想列出完整路径的话,需要使用listFiles.他返回的是File的数组
列出指定目录的全部文件(包括隐藏文件):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* 使用listFiles列出指定目录的全部文件
* listFiles输出的是完整路径
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator;
File f=
new
File(fileName);
File[] str=f.listFiles();
for
(
int
i =
0
; i < str.length; i++) {
System.out.println(str[i]);
}
}
}
|
【运行结果】:
D:\$RECYCLE.BIN
D:\360
D:\360Downloads
D:\360Rec
D:\360SoftMove
D:\Config.Msi
D:\da
D:\Downloads
D:\DriversBackup
D:\eclipse
D:\java web整合开发和项目实战
D:\Lenovo
D:\MSOCache
D:\Program
D:\Program Files
D:\python
D:\RECYGLER.{8F92DA15-A229-A4D5-B5CE-5280C8B89C19}
D:\System Volume Information
D:\Tomcat6
D:\var
D:\vod_cache_data
D:\新建文件夹
通过比较可以指定,使用listFiles更加方便
判断一个指定的路径是否为目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
/**
* 使用isDirectory判断一个指定的路径是否为目录
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator;
File f=
new
File(fileName);
if
(f.isDirectory()){
System.out.println(
"YES"
);
}
else
{
System.out.println(
"NO"
);
}
}
}
|
【运行结果】:YES
搜索指定目录的全部内容
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
/**
* 列出指定目录的全部内容
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args) {
String fileName=
"D:"
+File.separator;
File f=
new
File(fileName);
print(f);
}
public
static
void
print(File f){
if
(f!=
null
){
if
(f.isDirectory()){
File[] fileArray=f.listFiles();
if
(fileArray!=
null
){
for
(
int
i =
0
; i < fileArray.length; i++) {
//递归调用
print(fileArray[i]);
}
}
}
else
{
System.out.println(f);
}
}
}
}
|
【运行结果】:
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\framepages\web4welcome_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\help_005fhome_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\home_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\index_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\login_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\modify_005fuser_005finfo_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\register_005fnotify_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.class
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\sign_005fup_jsp.java
D:\Tomcat6\work\Catalina\localhost\nevel\org\apache\jsp\transit_jsp.class
……
【使用RandomAccessFile写入文件】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 使用RandomAccessFile写入文件
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args)
throws
IOException {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
RandomAccessFile demo=
new
RandomAccessFile(f,
"rw"
);
demo.writeBytes(
"asdsad"
);
demo.writeInt(
12
);
demo.writeBoolean(
true
);
demo.writeChar(
'A'
);
demo.writeFloat(
1
.21f);
demo.writeDouble(
12.123
);
demo.close();
}
}
|
如果你此时打开hello。txt查看的话,会发现那是乱码。
字节流
【向文件中写入字符串】
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* 字节流
* 向文件中写入字符串
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args)
throws
IOException {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
OutputStream out =
new
FileOutputStream(f);
String str=
"你好"
;
byte
[] b=str.getBytes();
out.write(b);
out.close();
}
}
|
查看hello.txt会看到“你好”
当然也可以一个字节一个字节的写。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* 字节流
* 向文件中一个字节一个字节的写入字符串
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args)
throws
IOException {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
OutputStream out =
new
FileOutputStream(f);
String str=
"你好"
;
byte
[] b=str.getBytes();
for
(
int
i =
0
; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
|
结果还是:“你好”
向文件中追加新内容:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/**
* 字节流
* 向文件中追加新内容:
* */
import
java.io.*;
class
hello{
public
static
void
main(String[] args)
throws
IOException {
String fileName=
"D:"
+File.separator+
"hello.txt"
;
File f=
new
File(fileName);
OutputStream out =
new
FileOutputStream(f,
true
);
String str=
"Rollen"
;
//String str="\r\nRollen"; 可以换行
byte
[] b=str.getBytes();
for
(
int
i =
0
; i < b.length; i++) {
out.write(b[i]);
}
out.close();
}
}
|
【运行结果】:
你好Rollen