文件操作 文件内容: hello=hi现在要做的事是从文件中搜索hello这个字符,找到后把hi改成world,完成后文件内容为: hello=world多谢!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 import java.io.*;import java.util.*;class test{ private String str=""; private String strTotal="";; private String strReplace="";; test(){ try { BufferedReader in = new BufferedReader(new FileReader("a.txt")); while ((str = in.readLine()) != null) { strTotal=strTotal+str+"\r\n"; } in.close(); } catch (IOException e) { } strReplace=strTotal.replaceAll("hi","world"); System.out.println(strReplace); try { BufferedWriter out = new BufferedWriter(new FileWriter("a.txt")); out.write(strReplace); out.close(); } catch (IOException e) { } }};public class pack{ public static void main(String args[]){ test t=new test(); }}; to freshman520(freshman520):忘记说了,我这个文件比较大,有100万条记录,内存里放不下的 使用PropertyResourceBundle类: …… public static final String BASE_PROP_FILE =“DISP”; public static final String SUFFIX =“.properties”; locale = Locale.getDefault(); String propFile = BASE_PROP_FILE + “_” + locale.toString()+ SUFFIX; ResourceBundle rb; try { File file = new File(propFile); if (file.exists()) { is = new FileInputStream(file); rb = new PropertyResourceBundle(is); if (rb == null) System.out.println(“No Resource”); } } catch (IOException ioe) { System.out.println(“Error open file named ” + propFile); } Enumeration e = rb.getKeys(); while (e.hasMoreElements()){ key = (String)e.nextElement(); value = (String)rb.handleGetObject(key); System.out.println(“KEY: ” + key +“\t\t Value: ” + value); } …… 求大神救我出苦海????? 增强的for循环 菜鸟又来了! 本人小菜鸟,求大虾帮忙,自定义swing的问题 如何在产生jar的时候去掉不需要的class? java 开源的代码生成工具 有谁知道如何把 Together7集成到JBuilder 2005??? Runtime.getRuntime().totalMemory();是得到整个虚拟机内存吗? 关于applet和application的几个问题。 在线等待:弱弱的问:刚下载了java sdk,怎么安装阿? 新手求教~~ 快过年了,希望大家新的一年快乐,顺利,健康
import java.util.*;
class test
{
private String str="";
private String strTotal="";;
private String strReplace="";;
test(){
try {
BufferedReader in = new BufferedReader(new FileReader("a.txt"));
while ((str = in.readLine()) != null) {
strTotal=strTotal+str+"\r\n";
}
in.close();
} catch (IOException e) {
}
strReplace=strTotal.replaceAll("hi","world");
System.out.println(strReplace);
try {
BufferedWriter out = new BufferedWriter(new FileWriter("a.txt"));
out.write(strReplace);
out.close();
} catch (IOException e) {
}
}
};
public class pack
{ public static void main(String args[]){
test t=new test(); }
};
忘记说了,我这个文件比较大,有100万条记录,内存里放不下的
……
public static final String BASE_PROP_FILE =
“DISP”;
public static final String SUFFIX =
“.properties”;
locale = Locale.getDefault();
String propFile = BASE_PROP_FILE + “_” + locale.toString()+ SUFFIX;
ResourceBundle rb;
try {
File file = new File(propFile);
if (file.exists()) {
is = new FileInputStream(file);
rb = new PropertyResourceBundle(is);
if (rb == null) System.out.println(“No Resource”);
}
} catch (IOException ioe) {
System.out.println(“Error open file named ” + propFile);
}
Enumeration e = rb.getKeys();
while (e.hasMoreElements()){
key = (String)e.nextElement();
value = (String)rb.handleGetObject(key);
System.out.println(“KEY: ” + key +
“\t\t Value: ” + value);
}
……