Rain's Blog
The man who has made up his mind to win will never say“Impossible”. ——Napoleon
BlogJava
|
首页
|
发新随笔
|
发新文章
|
联系
|
聚合
|
管理
随笔:43 文章:0 评论:3 引用:0
文件操作(jsp)
1
文件的建立/检查与删除
2
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
3
<%
@ page import
=
"
java.io.*
"
%>
4
<
html
>
5
<
head
>
6
<
title
>
文件的建立、检查与删除
</
title
>
7
</
head
>
8
<
body
>
9
<%
10
String
path
=
request.getRealPath(
""
);
11
//
out.println(path);
12
File f
=
new
File(path,
"
File.txt
"
);
13
//
out.println(f);
14
//
out.println(f.exists());
15
16
if
(f.exists()){
//
检查File.txt是否存在
17
f.delete();
//
删除File.txt文件
18
out.println(path
+
"
\\File.txt 存在,已删除。
"
);
19
}
else
{
20
f.createNewFile();
//
在当前目录下建立一个名为File.txt的文件
21
out.println(path
+
"
\\File.txt 不存在,已建立。
"
);
//
输出目前所在的目录路径
22
}
23
%>
24
25
目录的建立/检查与删除
26
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
27
<%
@ page import
=
"
java.io.*
"
%>
28
<
html
>
29
<
head
>
30
<
title
>
目录的建立/检查与删除
</
title
>
31
</
head
>
32
<
body
>
33
<%
34
String
path
=
request.getRealPath(
""
);
35
path
=
path
+
"
\\Sub
"
;
//
将要建立的目录路径
36
File d
=
new
File(path);
//
建立代表Sub目录的File对象,并得到它的一个引用
37
if
(d.exists()){
//
检查Sub目录是否存在
38
d.delete();
39
out.println(
"
Sub目录存在,已删除
"
);
40
}
else
{
41
d.mkdir();
//
建立Sub目录
42
out.println(
"
Sub目录不存在,已建立
"
);
43
}
44
%>
45
</
body
>
46
</
html
>
47
48
49
如何在JSP中处理虚拟目录
50
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
51
<%
@ page import
=
"
java.io.*
"
%>
52
<
html
>
53
<
head
>
54
<
title
>
JSP中如何处理虚拟目录
</
title
>
55
</
head
>
56
<
body
>
57
取得虚拟目录对应的磁盘路径
<
br
>
58
Web站点主目录的位置为
<
font
color
=#ff0000
>
<%
=
request.getRealPath(
"
/
"
)
%>
</
font
><
br
>
59
JSP网页所在的目录位置
<
font
color
=#ff0000
>
<%
=
request.getRealPath(
"
./
"
)
%>
</
font
><
br
>
60
JSP网页所在目录上一层目录的位置
<
font
color
=#ff0000
>
<%
=
request.getRealPath(
"
../
"
)
%>
</
font
><
br
>
61
</
body
>
62
</
html
>
63
64
65
文件属性的取得
66
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
67
<%
@ page import
=
"
java.util.Date,java.io.*
"
%>
68
<
html
>
69
<
head
>
70
<
title
>
文件属性的取得
</
title
>
71
</
head
>
72
<
body
>
73
<%
74
String
path
=
request.getRealPath(
"
/
"
);
75
File f
=
new
File(path,
"
ReadData.txt
"
);
76
if
(f.exists()){
77
%>
78
<%
=
f.getName()
%>
的属性如下:
<
br
><
br
>
79
文件长度为:
<%
=
f.length()
%>
80
<%
=
f.isFile()?
"
是文件
"
:
"
不是文件
"
%>
<
br
>
81
<%
=
f.isDirectory()?
"
是目录
"
:
"
不是目录
"
%>
<
br
>
82
<%
=
f.canRead()?
"
可读取
"
:
"
不可读取
"
%>
<
br
>
83
<%
=
f.canWrite()?
"
可写入
"
:
"
不可写入
"
%>
<
br
>
84
<%
=
f.isHidden()?
"
是隐藏文件
"
:
"
不是隐藏文件
"
%>
<
br
>
85
文件的最后修改日期为:
<%
=
new
Date
(f.lastModified())
%>
<
br
>
86
<%
87
}
else
{
88
f.createNewFile();
//
在当前目录下建立一个名为ReaData.txt的文件
89
%>
90
<%
=
f.getName()
%>
的属性如下:
<
br
><
br
>
91
文件长度为:
<%
=
f.length()
%>
92
<%
=
f.isFile()?
"
是文件
"
:
"
不是文件
"
%>
<
br
>
93
<%
=
f.isDirectory()?
"
是目录
"
:
"
不是目录
"
%>
<
br
>
94
<%
=
f.canRead()?
"
可读取
"
:
"
不可读取
"
%>
<
br
>
95
<%
=
f.canWrite()?
"
可写入
"
:
"
不可写入
"
%>
<
br
>
96
<%
=
f.isHidden()?
"
是隐藏文件
"
:
"
不是隐藏文件
"
%>
<
br
>
97
文件的最后修改日期为:
<%
=
new
Date
(f.lastModified())
%>
<
br
>
98
<%
99
}
100
%>
101
</
body
>
102
</
html
>
103
104
105
取出目录中文件的方法
106
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
107
<%
@ page import
=
"
java.io.*
"
%>
108
<
html
>
109
<
head
>
110
<
title
>
取出目录中文件的方法--列出目录中的文件
</
title
>
111
</
head
>
112
<
body
>
113
<%
114
String
path
=
request.getRealPath(
"
/
"
);
115
File d
=
new
File(path);
//
建立当前目录中文件的File对象
116
File list[]
=
d.listFiles();
//
取得代表目录中所有文件的File对象数组
117
out.println(
"
<font color=#ff0000>
"
+
path
+
"
目录下的文件:</font><br>
"
);
118
for
(
int
i
=
0
;i
<
list.length;i
++
){
119
if
(list
<
I
>
.isFile()){
120
out.println(list
<
I
>
.getName()
+
"
<br>
"
);
121
}
122
}
123
out.println(
"
<br><font color=#ff0000>
"
+
path
+
"
目录下的目录:</font><br>
"
);
124
for
(
int
i
=
0
;i
<
list.length;i
++
){
125
if
(list
<
I
>
.isDirectory()){
126
out.println(list
<
I
>
.getName()
+
"
<br>
"
);
127
}
128
}
129
%>
130
</
body
>
131
</
html
>
132
133
134
判断是否为空白文件
135
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
136
<%
@ page import
=
"
java.io.*
"
%>
137
<
html
>
138
<
head
>
139
<
title
>
判断是否为空白文件
</
title
>
140
</
head
>
141
<
body
>
142
<%
143
String
path
=
request.getRealPath(
"
/
"
);
144
out.println(path);
145
FileReader fr
=
new
FileReader(path
+
"
\\AtEnd.txt
"
);
//
建立FileReader对象,并实例化为fr
146
//
对FileReader类生成的对象使用read()方法,可以从字符流中读取下一个字符。
147
if
(fr.read()
==-
1
)
//
判断是否已读到文件的结尾
148
{
149
out.print(
"
AtEnd.txt文件中没有数据<br>
"
);
150
}
else
{
151
out.println(
"
AtEnd.txt文件中有数据
"
);
152
}
153
fr.close();
154
%>
155
</
body
>
156
</
html
>
157
158
159
读取所有的文件数据
160
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
161
<%
@ page import
=
"
java.io.*,java.lang.*
"
%>
162
<
html
>
163
<
head
>
164
<
title
>
读取所有的文件数据
</
title
>
165
</
head
>
166
<
body
>
167
<%
168
String
path
=
request.getRealPath(
"
.
"
);
169
FileReader fr
=
new
FileReader(path
+
"
\\ReadData.txt
"
);
170
//
关键在于读取过程中,要判断所读取的字符是否已经到了文件的末尾,并且这个字符是不是文件中的断行符,即判断该字符值是否为13。
171
int
c
=
fr.read();
//
从文件中读取一个字符
172
//
判断是否已读到文件结尾
173
while
(c!
=-
1
){
174
out.print((char)c);
//
输出读到的数据
175
c
=
fr.read();
//
从文件中继续读取数据
176
if
(c
==
13
){
//
判断是否为断行字符
177
out.print(
"
<br>
"
);
//
输出分行标签
178
fr.skip(
1
);
//
略过一个字符
179
//
c
=
fr.read();
//
读取一个字符
180
}
181
}
182
fr.close();
183
%>
184
</
body
>
185
</
html
>
186
187
188
一行一行读取数据
189
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
190
<%
@ page import
=
"
java.io.*
"
%>
191
<
html
>
192
<
head
>
193
<
title
>
文件读取
</
title
>
194
</
head
>
195
<
body
>
196
<%
197
String
path
=
request.getRealPath(
""
);
//
取得当前目录的路径
198
FileReader fr
=
new
FileReader(path
+
"
\\file\\inc\\t.txt
"
);
//
建立FileReader对象,并实例化为fr
199
BufferedReader br
=
new
BufferedReader(fr);
//
建立BufferedReader对象,并实例化为br
200
String
Line
=
br.readLine();
//
从文件读取一行字符串
201
//
判断读取到的字符串是否不为空
202
while
(Line!
=
null
){
203
out.println(Line
+
"
<br>
"
);
//
输出从文件中读取的数据
204
Line
=
br.readLine();
//
从文件中继续读取一行数据
205
}
206
br.close();
//
关闭BufferedReader对象
207
fr.close();
//
关闭文件
208
%>
209
</
body
>
210
</
html
>
211
212
213
略过文件中的字符不读取
214
<%
@ page contentType
=
"
text/html;charset=gb2312
"
%>
215
<%
@ page import
=
"
java.io.*
"
%>
216
<
html
>
217
<
head
>
218
<
title
>
略过字节不读取
</
title
>
219
</
head
>
220
<
body
>
221
<%
222
String
path
=
request.getRealPath(
"
.
"
);
223
FileReader fr
=
new
FileReader(path
+
"
\\ReadData.txt
"
);
224
fr.skip(
2
);
//
跳过2个字节
225
int
c
=
fr.read();
//
读取一个字节
226
while
(c!
=-
1
){
227
out.print((c