Java文件操作

hzcy223 2010-03-13 04:48:37
如何把文件中的某个单词换成另一个单词
统计文件中的单词个数
...全文
148 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
智鹿软件 2010-03-14
  • 打赏
  • 举报
回复
替换的话,在API里面查一下,我有这方面的例子:
package IO;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* test.getBytes("iso8859-1"))
* 用OutputStream和InputStream实现文件的写入和读取(字节的写入和读取)
*
*/
public class InAndOut {
String path="e:\\abc.txt";
public static void main(String[] args) {
InAndOut io=new InAndOut ();
io.write("我是邪恶少年!");
io.read();
}
public void write(String content)
{
File f=new File (path);
boolean add=true;//是否写入
try {
if (f.exists()==false) {
f.createNewFile();
FileOutputStream os=new FileOutputStream (f);//不用判断是否写入
//FileOutputStream os=new FileOutputStream (f,add);//要判断是否写入
os.write(content.getBytes());
os.flush();
os.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void read()
{
try {
InputStream is=new FileInputStream(path);
//BufferedInputStream bis=new BufferedInputStream(new FileInputStream(path));
int temp=0;
String test="";
try {
while ((temp=is.read())!=-1) {
test+=(char)temp;
}
System.out.println(new String (test.getBytes("iso8859-1")));//会出现乱码
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
package IO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
/**
*
*用read和write实现文件的写入和读取(字符的写入和读取)
*
*/
public class ReadAndWrite {
//定义文件的路径,一边写入和读取使用
String path="e:\\abc.txt";

public static void main(String[] args) {
ReadAndWrite r=new ReadAndWrite ();
r.writeFile("我乃邪恶少年是也!");
r.readFile();
}
private void readFile() {
// TODO Auto-generated method stub
try {
Reader r=new BufferedReader (new FileReader(path));
String test="";
int temp=0;
try {
while ((temp=r.read())!=-1) {
test+=(char)temp;
}
System.out.println(test);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void writeFile(String content) {
// TODO Auto-generated method stub
File f=new File (path);
if (f.exists()==false) {
try {
f.createNewFile();
FileWriter fw=new FileWriter (f);
fw.write(content);
fw.flush();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}else {
System.out.println(path+"已存在,写入失败,源文件是");
}
}
}
hzcy223 2010-03-14
  • 打赏
  • 举报
回复
谁能写下代码,本人刚学文件操作,不是很懂
hzcy223 2010-03-14
  • 打赏
  • 举报
回复
怎么把从文件中读取的字符转化为String类型呢?
musiclee 2010-03-13
  • 打赏
  • 举报
回复
遍历 匹配 是否成?替换(计数+1):继续下一个
qingyuan18 2010-03-13
  • 打赏
  • 举报
回复
Unix环境下调shell命令做这种字符操作最方便:
单词转换:
tr "oldWord" "newWord" “你的文件名”
单词数统计:
wc "你的文件名"|awk '{print $2}'

Java里面Runtime.exe可以执行底层操作系统命令
JavaAlpha 2010-03-13
  • 打赏
  • 举报
回复
http://blog.csdn.net/JavaAlpha/archive/2009/12/02/4926157.aspx
楼主 可以看看这里的代码实现。

62,624

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧