我的java生涯
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 46,comments - 37,trackbacks - 0
<
2007年8月
>
日
一
二
三
四
五
六
29
30
31
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
8
IT同胞们大家好!
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(2)
给我留言
查看公开留言
查看私人留言
随笔分类
(45)
eclipse日记(1)
java文件操作(4)
java日记(16)
JS日记(2)
swt日记(1)
学习日记(13)
应用服务器(2)
数据库日记(4)
生活随笔(2)
随笔档案
(46)
2008年4月 (3)
2008年3月 (1)
2007年12月 (1)
2007年11月 (5)
2007年10月 (1)
2007年9月 (1)
2007年8月 (3)
2007年7月 (4)
2007年6月 (1)
2007年5月 (2)
2007年4月 (2)
2007年3月 (13)
2006年8月 (3)
2006年7月 (2)
2006年6月 (3)
2006年4月 (1)
相册
senlin
心理测试
成名早风光无限 看昔日童星的旧貌新颜
友情联接
Rex Mao
小左
心情小站
阿郎
高副司令
最新随笔
1. js校验常用方法
2. js中控制小数点的位数
3. 控制你的网页进行翻页打印
4. 解决RandomAccessFile写入文件乱码的办法
5. java调用webService例子
6. tomcat5.0.28优化
7. tomcat5.0.28指定具体虚拟目录
8. java通过服务名动态实例化对象并调用指定方法
9. 读取指定的文件并转成字符串
10. Hibernate实现Clob和Blob对象的存取
搜索
最新评论
1. re: tomcat5.0.28优化
我也想知道
--suyuan
2. re: 解决RandomAccessFile写入文件乱码的办法
评论内容较长,点击标题查看
--Find it, try it, experience it
3. re: 解决RandomAccessFile写入文件乱码的办法
你会说明你能,但不一定谁都会的,我是说明解决乱码的问题
--senlin-blog
4. re: 解决RandomAccessFile写入文件乱码的办法[未登录]
明明就是追加,谁都会的
--AA
5. re: 解决RandomAccessFile写入文件乱码的办法
I didn't see any thing here can resolve the problem related to encoding.
--Find it, try it, experience it
阅读排行榜
1. java调用webService例子(3758)
2. oracle数据库中导入一个.dmp文件(1609)
3. MyEclipse+Weblogic开发EJB(一) (1300)
4. java读写删.text,.xml文件内容(1093)
5. java通过服务名动态实例化对象并调用指定方法(950)
评论排行榜
1. java通过服务名动态实例化对象并调用指定方法(8)
2. 读取指定的文件并转成字符串(7)
3. 解决RandomAccessFile写入文件乱码的办法(4)
4. MyEclipse+Weblogic开发EJB(一) (4)
5. java画带箭头的方法(4)
java读写删.text,.xml文件内容
1
package
com.paiao.bdpm.flow.base;
2
3
import
java.io.BufferedReader;
4
import
java.io.BufferedWriter;
5
import
java.io.File;
6
import
java.io.FileReader;
7
import
java.io.FileWriter;
8
9
public
class
ReadTextFile
{
10
public
BufferedReader bufread;
11
public
BufferedWriter bufwriter;
12
File writefile;
13
String filepath, filecontent, read;
14
String readStr
=
""
;
15
//
从文本文件中读取内容
16
public
String readfile(String path)
17
{
18
try
{
19
filepath
=
path;
//
得到文本文件的路径
20
File file
=
new
File(filepath);
21
FileReader fileread
=
new
FileReader(file);
22
bufread
=
new
BufferedReader(fileread);
23
while
((read
=
bufread.readLine())
!=
null
)
{
24
read
=
read
+
"
\r\n
"
;
25
readStr
=
readStr
+
read;
26
}
27
}
catch
(Exception d)
{
28
System.out.println(d.getMessage());
29
}
30
return
readStr;
//
返回从文本文件中读取内容
31
}
32
33
//
向文本文件中写入内容
34
public
void
writefile(String path, String content,
boolean
append)
{
35
try
{
36
boolean
addStr
=
append;
//
通过这个对象来判断是否向文本文件中追加内容
37
filepath
=
path;
//
得到文本文件的路径
38
filecontent
=
content;
//
需要写入的内容
39
writefile
=
new
File(filepath);
40
if
(writefile.exists()
==
false
)
//
如果文本文件不存在则创建它
41
{
42
writefile.createNewFile();
43
writefile
=
new
File(filepath);
//
重新实例化
44
}
45
FileWriter filewriter
=
new
FileWriter(writefile, addStr);
46
//
删除原有文件的内容
47
java.io.RandomAccessFile file
=
new
java.io.RandomAccessFile(path,
"
rw
"
);
48
file.setLength(
0
);
49
//
写入新的文件内容
50
filewriter.write(filecontent);
51
filewriter.close();
52
filewriter.flush();
53
}
catch
(Exception d)
{
54
System.out.println(d.getMessage());
55
}
56
}
57
58
public
static
void
main(String[] args)
throws
Exception
{
59
ReadTextFile parse
=
new
ReadTextFile();
60
String filecontent
=
parse.readfile(
"
c:/applicationContext.xml
"
);
61
parse.writefile(
"
c:/applicationContext.xml
"
,filecontent,
true
);
62
63
}
64
}
posted on 2007-08-15 15:10
我的java生涯
阅读(1093)
评论(0)
编辑
收藏
所属分类:
java文件操作
新闻频道
新用户注册
刷新评论列表
标题
姓名
主页
验证码
*
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-11-19 15:27 编辑过
博客园
BlogJava
博客生活
IT博客网
C++博客
PHP博客
博客园社区
管理博客
教师博客
天文博客
汽车博客
足球博客
股票博客
电子博客
管理
相关文章:
解决RandomAccessFile写入文件乱码的办法
读取指定的文件并转成字符串
java读写删.text,.xml文件内容
拷贝一个文件到另一个地方