java 读写文件
java \u6587\u4ef6\u8bfb\u5199import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class OutputStreamDemo05 {
public static void main(String args[]) throws Exception
{
File f = new File("\u6587\u4ef6\u8def\u5f84");
OutputStream out = new FileOutputStream(f);
String str = "Hello World!!!";
byte b[] = str.getBytes();//\u5b57\u7b26\u4e32\u8f6c\u6362\u6210byte\u6570\u7ec4
out.write(b);
//out.close();
}
}
\u5728\u7f51\u4e0a\u67e5\u4e86\u5f88\u591a\u5173\u4e8e\u4fee\u6539\u6587\u4ef6\u7684\u65b9\u6cd5\uff0c\u4e0d\u5f97\u5176\u8981\u9886\u3002\u81ea\u5df1\u60f3\u4e86\u4e24\u4e2a\u53d6\u5de7\u7684\u529e\u6cd5\uff0c\u6765\u89e3\u51b3\u5bf9\u6587\u4ef6\u7684\u4fee\u6539\u3002\u4e00\uff1a\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6file1\uff08FileReader and BufferedReader\uff09\uff0c\u8fdb\u884c\u64cd\u4f5c\u540e\u5199\u5165file2(FileWriter and BufferedWriter)\uff0c\u7136\u540e\u5220\u9664file1\uff0c\u66f4\u6539file2\u6587\u4ef6\u540d\u4e3afile1(Rename()\u65b9\u6cd5)\u3002\u4e8c\uff1a\u521b\u5efa\u5b57\u7b26\u7f13\u51b2\u6d41\uff08StringBuffer\uff09\uff0c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u8d4b\u7ed9\u5b57\u7b26\u7f13\u51b2\u6d41\uff0c\u518d\u5c06\u5b57\u7b26\u7f13\u51b2\u6d41\u4e2d\u7684\u5185\u5bb9\u5199\u5165\u5230\u8bfb\u53d6\u7684\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a test.txt \u8fd9\u91cc\u662f\u653e\u5728d\u76d8\u7684\u6839\u76ee\u5f55\u4e0b\uff0c\u5185\u5bb9\u5982\u4e0b able adj \u6709\u624d\u5e72\u7684\uff0c\u80fd\u5e72\u7684 active adj \u4e3b\u52a8\u7684\uff0c\u6d3b\u8dc3\u7684 adaptable adj \u9002\u5e94\u6027\u5f3a\u7684 adroit adj \u7075\u5de7\u7684\uff0c\u673a\u654f\u7684 \u8fd0\u884c\u7ed3\u679c\u751f\u6210\u5728\u540c\u76ee\u5f55\u7684 test1.txt\u4e2d able #adj*\u6709\u624d\u5e72\u7684\uff0c\u80fd\u5e72\u7684 active #adj*\u4e3b\u52a8\u7684\uff0c\u6d3b\u8dc3\u7684 adaptable #adj*\u9002\u5e94\u6027\u5f3a\u7684 adroit #adj*\u7075\u5de7\u7684\uff0c\u673a\u654f\u7684 \u4ee3\u7801\uff1a public class Test { public static void main(String[] args) throws Exception{ BufferedReader br = new BufferedReader(new FileReader("D:\\test.txt")); StringBuffer sb = new StringBuffer(); String lineContent = null ;while( (lineContent = br.readLine()) != null){ String[] sp = lineContent.split(" ");sp[0] = sp[0].concat(" *");sp[1] = sp[1].concat("# ");for(int i=0;i sb.append(sp[i]);}sb.append("\r\n");}FileWriter fw = new FileWriter("D:\\test2.txt"); fw.write(sb.toString()); br.close(); fw.close(); }}
StringBuffer sb = new StringBuffer();//File file = new FileWindow().load();
File file;
file = new File("F:/jtest/from.txt");
TextReader tr = new TextReader(file);
sb.append(tr.readAll());
Out.println(sb.toString());
String [] tags = {"\"", " ", "'"};
String str;
str = sb.toString();
for(String reg: tags) {
Out.println(reg);
str = str.replaceAll(reg, "");
}
Out.println(str);
查下API文档,你就会做了.
String类的split(String regex);
扩展阅读:zookeeper java api ... java读取txt文件 ... java编程常用软件 ... java api在线文档 ... java api文档下载 ... java文件输入输出 ... java api文档最新 ... java文件读写所占内存 ... java从txt文件中读写数据 ...