好好學習,好好工作,好好生活
終身學習,努力賺錢,健康愉快 注意:轉發本blog文章請注明文章出處(http://m.tkk7.com/lusm)謝謝支持!
首頁
新隨筆
聯系
聚合
管理
隨筆 - 303 文章 - 883 trackbacks - 0
<
2007年3月
>
日
一
二
三
四
五
六
25
26
27
28
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
28
29
30
31
1
2
3
4
5
6
7
歡迎光臨!
閑聊
QQ:
1074961813
隨筆分類
(357)
Ajax、Flex(6)
Blog公告(3)
c/c++/C#/pasic/vb/php/asp(.net)/win-cgi/xml...(53)
database(13)
j2se/j2ee/j2me(142)
kernel (16)
linux configuration(6)
Listening 所有的聲音都是音樂(1)
matlab~~~值得一學的工具(2)
network(5)
SUN OS(8)
unix program(10)
書法(欲罷不能,神魂顛倒的美)(4)
入侵反入侵(4)
關于搜索引擎
哲學世界(8)
多媒體開發(4)
嵌入式開發(14)
常用資源(19)
匯編 *asm/at&t ....(1)
科學百科(4)
算法(2)
編程問題(4)
計算機病毒反病毒學(4)
身體鍛煉(5)
軟件 & 書籍 推薦(19)
我管理的群
j2ee
struts Hibernate spring等學習 基礎不扎實 可以跳到j2se學習群 37486623 請先退出該群
j2se(37486623)
j2se學習 若人滿請加到12999758 學完的可以跳到j2ee學習群 37486483 請先退出該群
java || .net(50982019)
java & .net都是熱門的web技術 當然可以一起學啦 歡迎前來探討技術 解決問題
java 2(35917927)~~~請加這個
該群在發展中,踢人是經常的事情
java(12999758)
學完的可以跳到j2ee學習群 37486483 請先退出該群
Java應用(14821632)
學無止境 好好努力! 討論 web 技術 、 Java (j2se j2ee) 、C/S編程 、 數據庫 。。。。
Linux C、C++(33318370)
將 Linux 和 C、C++ 編程進行到底!
Think in java(16705375)~~~請加這個
《Think in java》 我們的最愛
國學(111230500)
歷史、宗教、哲學、氣功、傳統技藝,歡迎加入,知無不言,言無不盡!
文祠實習群(32644370)
07年文祠實習群
驅動開發群(27054589)
arm || intel ...platform Windows || Linux device driver development 有一定基礎方可加入
公共blog
n維空間
Email : java3d@126.com 群 : 12999758
參與管理的論壇
ineedyou
ourlinux
好友的blog
lyforever
Resplendent技術blog
java、VC和嵌入開發
一夢如是
c/c++/windows/mfc
三駕馬車
仙人指路
弄VC和匯編的家伙
呂鑫磊
吳衛-3D工作室
好好學習,天天向上,態度決定一切
同學的blog
廖益花
琦琦
北京的朋友,和我一起創建了不少群 呵呵!
菜菜C++
VC++
陳敏師兄
霍霍
一個和我差不多大的北京女孩
我的其他blog
kernel world(csdn)
尋尋覓覓—— C語言
我的世界 不被打擾
虛擬世界-自由自在
音樂在身邊
朋友的網站
china1996.cn
ourlinux.org
ourlinux.org
笑話大全
累了的時候啦 可以尋下開心
搜索
最新評論
1.?re: Windows Embedded CE 6.0 下載地址和序列號
評論內容較長,點擊標題查看
--碼無止莖
2.?re: Caused by: java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool
評論內容較長,點擊標題查看
--一
3.?re: linux0.11內核編程入門全集視頻下載
博主,看不了啊,方便的話發一份到ren1198997229@163.com 謝謝( ̄▽ ̄)"
--renchunhui
4.?re: JVM 、JDK(c/c++)源代碼下載[未登錄]
還可以吧
--劉勇
5.?re: linux0.11內核編程入門全集視頻下載
視頻怎么下不了啊
--MSM
[轉] JAVA文件操作大全
?from :
http://wjxk.blog.sohu.com/15362196.html
文件的建立/檢查與刪除?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?
import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
文件的建立、檢查與刪除
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String?path
=
request.getRealPath(
""
);?
//
out.println(path);?
File?f
=
new
?File(path,
"
File.txt
"
);?
//
out.println(f);?
//
out.println(f.exists());?
if
(f.exists())
{
//
檢查File.txt是否存在?
f.delete();
//
刪除File.txt文件?
out.println(path?
+
?
"
File.txt?存在,已刪除。
"
);?
}
else
{?
f.createNewFile();
//
在當前目錄下建立一個名為File.txt的文件?
out.println(path?
+
?
"
File.txt?不存在,已建立。
"
);
//
輸出目前所在的目錄路徑?
}
?
%>
?
?
目錄的建立/檢查與刪除
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
目錄的建立/檢查與刪除
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
""
);?
path
=
path?
+
?
"
Sub
"
;
//
將要建立的目錄路徑?
File?d
=
new
?File(path);
//
建立代表Sub目錄的File對象,并得到它的一個引用?
if
(d.exists()){
//
檢查Sub目錄是否存在?
d.delete();?
out.println(
"
Sub目錄存在,已刪除
"
);?
}
else
{?
d.mkdir();
//
建立Sub目錄?
out.println(
"
Sub目錄不存在,已建立
"
);?
}?
%>
?
</
body
>
?
</
html
>
?
如何在JSP中處理虛擬目錄?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
JSP中如何處理虛擬目錄
</
title
>
?
</
head
>
?
<
body
>
?
取得虛擬目錄對應的磁盤路徑
<
br
>
?
Web站點主目錄的位置為
<
font?
color
=#ff0000
>
<%
=
request.getRealPath(
"
/
"
)
%>
</
font
><
br
>
?
JSP網頁所在的目錄位置
<
font?
color
=#ff0000
>
<%
=
request.getRealPath(
"
./
"
)
%>
</
font
><
br
>
?
JSP網頁所在目錄上一層目錄的位置
<
font?
color
=#ff0000
>
<%
=
request.getRealPath(
"
../
"
)
%>
</
font
><
br
>
?
</
body
>
?
</
html
>
?
文件屬性的取得?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.util.Date,java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
文件屬性的取得
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
/
"
);?
File?f
=
new
?File(path,
"
ReadData.txt
"
);?
if
(f.exists()){?
%>
?
<%
=
f.getName()
%>
的屬性如下:
<
br
><
br
>
?
文件長度為:
<%
=
f.length()
%>
?
<%
=
f.isFile()?
"
是文件
"
:
"
不是文件
"
%>
<
br
>
?
<%
=
f.isDirectory()?
"
是目錄
"
:
"
不是目錄
"
%>
<
br
>
?
<%
=
f.canRead()?
"
可讀取
"
:
"
不可讀取
"
%>
<
br
>
?
<%
=
f.canWrite()?
"
可寫入
"
:
"
不可寫入
"
%>
<
br
>
?
<%
=
f.isHidden()?
"
是隱藏文件
"
:
"
不是隱藏文件
"
%>
<
br
>
?
文件的最后修改日期為:
<%
=
new
?
Date
(f.lastModified())
%>
<
br
>
?
<%
?
}
else
{?
f.createNewFile();
//
在當前目錄下建立一個名為ReaData.txt的文件?
%>
?
<%
=
f.getName()
%>
的屬性如下:
<
br
><
br
>
?
文件長度為:
<%
=
f.length()
%>
?
<%
=
f.isFile()?
"
是文件
"
:
"
不是文件
"
%>
<
br
>
?
<%
=
f.isDirectory()?
"
是目錄
"
:
"
不是目錄
"
%>
<
br
>
?
<%
=
f.canRead()?
"
可讀取
"
:
"
不可讀取
"
%>
<
br
>
?
<%
=
f.canWrite()?
"
可寫入
"
:
"
不可寫入
"
%>
<
br
>
?
<%
=
f.isHidden()?
"
是隱藏文件
"
:
"
不是隱藏文件
"
%>
<
br
>
?
文件的最后修改日期為:
<%
=
new
?
Date
(f.lastModified())
%>
<
br
>
?
<%
?
}?
%>
?
</
body
>
?
</
html
>
?
取出目錄中文件的方法
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
取出目錄中文件的方法--列出目錄中的文件
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
/
"
);?
File?d
=
new
?File(path);
//
建立當前目錄中文件的File對象?
File?list[]
=
d.listFiles();
//
取得代表目錄中所有文件的File對象數組?
out.println(
"
<font?color=#ff0000>
"
?
+
?path?
+
?
"
目錄下的文件:</font><br>
"
);?
for
(
int
?i
=
0
;i
<
list.length;i
++
){?
if
(list
<
I
>
.isFile()){?
out.println(list
<
I
>
.getName()?
+
?
"
<br>
"
);?
}?
}?
out.println(
"
<br><font?color=#ff0000>
"
?
+
?path?
+
?
"
目錄下的目錄:</font><br>
"
);?
for
(
int
?i
=
0
;i
<
list.length;i
++
){?
if
(list
<
I
>
.isDirectory()){?
out.println(list
<
I
>
.getName()?
+
?
"
<br>
"
);?
}?
}?
%>
?
</
body
>
?
</
html
>
?
判斷是否為空白文件?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
判斷是否為空白文件
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
/
"
);?
out.println(path);?
FileReader?fr
=
new
?FileReader(path?
+
?
"
AtEnd.txt
"
);
//
建立FileReader對象,并實例化為fr?
//
對FileReader類生成的對象使用read()方法,可以從字符流中讀取下一個字符。?
if
(fr.read()
==-
1
)
//
判斷是否已讀到文件的結尾?
{?
out.print(
"
AtEnd.txt文件中沒有數據<br>
"
);?
}
else
{?
out.println(
"
AtEnd.txt文件中有數據
"
);?
}?
fr.close();?
%>
?
</
body
>
?
</
html
>
?
????
<
B
>
讀取所有的文件數據
</
B
>
?
<
ccid_nobr
>
?
<
table?
width
="400"
?border
="1"
?cellspacing
="0"
?cellpadding
="2"
?
bordercolorlight?
=?"black"
?bordercolordark?
=?"#FFFFFF"
?align
="center"
>
?
<
tr
>
?
<
td?
bgcolor
="e6e6e6"
?class
="code"
?style
="font-size:9pt"
>
?
<
pre
><
ccid_code
>
?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*,java.lang.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
讀取所有的文件數據
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
.
"
);?
FileReader?fr
=
new
?FileReader(path?
+
?
"
ReadData.txt
"
);?
//
關鍵在于讀取過程中,要判斷所讀取的字符是否已經到了文件的末尾,?
并且這個字符是不是文件中的斷行符,即判斷該字符值是否為13。?
int
?c
=
fr.read();
//
從文件中讀取一個字符?
//
判斷是否已讀到文件結尾?
while
(c!
=-
1
){?
out.print((char)c);
//
輸出讀到的數據?
c
=
fr.read();
//
從文件中繼續讀取數據?
if
(c
==
13
){
//
判斷是否為斷行字符?
out.print(
"
<br>
"
);
//
輸出分行標簽?
fr.skip(
1
);
//
略過一個字符?
//
c
=
fr.read();
//
讀取一個字符?
}?
}?
fr.close();?
%>
?
</
body
>
?
</
html
>
?
一行一行讀取數據
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
文件讀取
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
""
);
//
取得當前目錄的路徑?
FileReader?fr
=
new
?FileReader(path?
+
?
"
fileinct.txt
"
);
//
建立FileReader對象,并實例化為fr?
BufferedReader?br
=
new
?BufferedReader(fr);
//
建立BufferedReader對象,并實例化為br?
String
?Line
=
br.readLine();
//
從文件讀取一行字符串?
//
判斷讀取到的字符串是否不為空?
while
(Line!
=
null
){?
out.println(Line?
+
?
"
<br>
"
);
//
輸出從文件中讀取的數據?
Line
=
br.readLine();
//
從文件中繼續讀取一行數據?
}?
br.close();
//
關閉BufferedReader對象?
fr.close();
//
關閉文件?
%>
?
</
body
>
?
</
html
>
?
略過文件中的字符不讀取?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
略過字節不讀取
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
.
"
);?
FileReader?fr
=
new
?FileReader(path?
+
?
"
ReadData.txt
"
);?
fr.skip(
2
);
//
跳過2個字符
int
?c
=
fr.read();
//
讀取一個字符?
while
(c!
=-
1
){?
out.print((char)c);?
c
=
fr.read();?
}?
fr.close();?
%>
?
</
body
>
?
</
html
>
?
將數據寫入文件?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
將數據寫入文件
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
.
"
);?
FileWriter?fw
=
new
?FileWriter(path?
+
?
"
WriteData.txt
"
);
//
建立FileWriter對象,并實例化fw?
//
將字符串寫入文件?
fw.write(
"
大家好!
"
);?
fw.write(
"
本書是《JSP編程技巧》
"
);?
fw.write(
"
請多多指教!
"
);?
fw.write(
"
email:stride@sina.com
"
);?
fw.close();?
FileReader?fr
=
new
?FileReader(path?
+
?
"
WriteData.txt
"
);?
BufferedReader?br
=
new
?BufferedReader(fr);
//
建立BufferedReader對象,并實例化為br?
String
?Line
=
br.readLine();?
//
讀取一行數據?
out.println(Line?
+
?
"
<br>
"
);?
br.close();
//
關閉BufferedReader對象?
fr.close();?
%>
?
</
body
>
?
</
html
>
?
將寫入文件的數據分行?
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
將寫入文件的數據分行
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
.
"
);?
FileWriter?fw
=
new
?FileWriter(path?
+
?
"
WriteData.txt
"
);?
BufferedWriter?bw
=
new
?BufferedWriter(fw);?
bw.write(
"
大家好!
"
);?
bw.write(
"
本書是《JSP編程技巧》。
"
);?
bw.newLine();
//
斷行?
bw.write(
"
請多多指教!
"
);?
bw.newLine();
//
斷行?
bw.write(
"
email:?stride@sina.com
"
);?
bw.flush();
//
將數據更新至文件?
fw.close();
//
關閉文件流?
out.println(
"
寫入文件內容為:<br>
"
);?
FileReader?fr
=
new
?FileReader(path?
+
?
"
WriteData.txt
"
);?
BufferedReader?br
=
new
?BufferedReader(fr);?
String
?Line
=
br.readLine();
//
讀取一行數據?
while
(Line!
=
null
){?
out.println(Line?
+
?
"
<br>
"
);?
Line
=
br.readLine();?
}?
fr.close();?
%>
?
</
body
>
?
</
html
>
?
如何將數據追加寫入到文件
<%
@?page?contentType
=
"
text/html;charset=gb2312
"
%>
?
<%
@?page?import
=
"
java.io.*
"
%>
?
<
html
>
?
<
head
>
?
<
title
>
將寫入文件的數據分行
</
title
>
?
</
head
>
?
<
body
>
?
<%
?
String
?path
=
request.getRealPath(
"
.
"
);?
RandomAccessFile?rf
=
new
?RandomAccessFile(path?
+
?
"
WriteData.txt
"
,
"
rw
"
);?
//
定義一個類RandomAccessFile的對象,并實例化?
rf.seek(rf.length());
//
將指針移動到文件末尾?
rf.writeBytes(
"
nAppend?a?line?to?the?file!
"
);?
rf.close();
//
關閉文件流?
out.println(
"
寫入文件內容為:<br>
"
);?
FileReader?fr
=
new
?FileReader(path?
+
?
"
WriteData.txt
"
);?
BufferedReader?br
=
new
?BufferedReader(fr);
//
讀取文件的BufferedRead對象?
String
?Line
=
br.readLine();?
while
(Line!
=
null
){?
out.println(Line?
+
?
"
<br>
"
);?
Line
=
br.readLine();?
}?
fr.close();
//
關閉文件?
%>
?
</
body
>
?
</
html
>
?
地震讓大伙知道:居安思危,才是生存之道。
posted on 2007-03-28 16:13
小尋
閱讀(363)
評論(3)
編輯
收藏
所屬分類:
j2se/j2ee/j2me
FeedBack:
#
re: [轉] JAVA文件操作大全 2007-04-02 13:27
皿里
[quote]fr.skip(2);//跳過2個字節
int c=fr.read();//讀取一個字節
[/quote]
這里的方法skip(int n)里的參數n應該是指跳過的字符數,不是字節數!FileReader的read方法是讀一個字符的數據,不是一個字節!
回復
更多評論
#
re: [轉] JAVA文件操作大全 2007-04-03 10:04
幻想~@@~
謝謝指正! 呵呵,下次發表,如有需要,可選擇高級回復方式;
下次轉資料前一定會仔細閱讀一次的;謝謝師兄支持!
參考資料:
java.lang.Object
|_ java.io.Reader
The url
java.lang.Object
|_java.io.Reader
|_java.io.InputStreamReader
|_java.io.FileReader
The url
網上找的參考程序:
public
String read(File file)
throws
IOException
{
int
length
=
(
int
)file.length();
//
這里的文件不能太大, 太大
//
的話就不要 一次性讀到內存中.
//
起碼不能超過int的最大值.
char
[] context
=
new
char
[length];
//
可見的、該方法在這里
//
讀的是字符;
FileReader reader
=
new
FileReader(file);
reader.read(context);
reader.close();
return
new
String(context );
}
回復
更多評論
#
re: [轉] JAVA文件操作大全
2007-04-03 10:08
lusm
看了發該文還不清楚者:
由于本blog這方面內容頗多,
所以本人為您找來另一個文章
<Java文件操作詳解可運行版>
rul :
http://www.javaresearch.org/article/54605.htm
回復
更多評論
新用戶注冊
刷新評論列表
只有注冊用戶
登錄
后才能發表評論。
網站導航:
博客園
IT新聞
Chat2DB
C++博客
博問
相關文章:
[原創]淺析JACOB
Struts && Spring 登陸實驗
[分頁]之 servlet分頁
一個通用的Hibernate DAO
JSF下拉列表簡單實現視頻及代碼
在 arm920T 上使用 java 雜雜
spring整合hibernate提供代碼下載
小小hibernate3.1代碼
小小spring2.0代碼擴充
小小的spring2.0例子
Copyright ©2025 小尋 Powered by:
博客園
模板提供:
滬江博客
主站蜘蛛池模板:
手机永久免费的AV在线电影网
|
天天摸夜夜摸成人免费视频
|
男人的天堂av亚洲一区2区
|
亚洲国产综合91精品麻豆
|
午夜亚洲国产成人不卡在线
|
丁香花免费完整高清观看
|
亚洲熟妇无码AV在线播放
|
国产亚洲精品美女2020久久
|
亚洲日韩乱码中文无码蜜桃
|
亚洲精品少妇30p
|
亚洲国产精品碰碰
|
成在线人永久免费视频播放
|
国产精品久久免费
|
无码国产精品一区二区免费模式
|
午夜不卡AV免费
|
国产亚洲综合久久
|
亚洲精品乱码久久久久久V
|
亚洲一区无码中文字幕乱码
|
亚洲国产精品婷婷久久
|
亚洲精品高清国产一线久久
|
亚洲伊人成无码综合网
|
四虎永久免费地址在线网站
|
午夜精品在线免费观看
|
久久久久久国产精品免费免费
|
亚洲人成网亚洲欧洲无码久久
|
免费又黄又爽又猛的毛片
|
精品国产精品久久一区免费式
|
久久精品a一国产成人免费网站
|
2019中文字幕免费电影在线播放
|
黄色网址在线免费
|
在线观看免费播放av片
|
国产一精品一av一免费爽爽
|
爽爽爽爽爽爽爽成人免费观看
|
9久热这里只有精品免费
|
一区二区三区免费看
|
亚洲视频在线免费
|
久草免费福利在线
|
成全动漫视频在线观看免费高清版下载
|
东北美女野外bbwbbw免费
|
a级日本高清免费看
|
久久久久免费精品国产
|