怎么把一条数据写入到一个文本文件中,而不删除原有的数据 String mateValue = "<meta name=\"" + value + "\" content=\"concept\">" 这是我的一条数据 我想把它加入到html文件中,又不能删除html原有的文件,要怎么做最好 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先读取全部文本, 然后修改, 然后写入或者用jar包吧jsoup不错 先取到原先的数据,然后和要写入的放到一块,接着全部写入package com.test;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileReader;import java.io.FileWriter;public class FileTest {public static void main(String[] args) throws Exception{File file = new File("d:/d.txt");StringBuilder sb = new StringBuilder();String s ="";BufferedReader br = new BufferedReader(new FileReader(file));while( (s = br.readLine()) != null) {sb.append(s + "\n");}BufferedWriter out = new BufferedWriter(new FileWriter(file));String words = "what you want to write";out.write(sb.toString()+"\n"+words);br.close();out.close();}} FileWriter(String fileName, boolean append) 在给出文件名的情况下构造 FileWriter 对象,它具有指示是否挂起写入数据的 boolean 值。 <meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 6.0"><meta name="ProgId" content="FrontPage.Editor.Document"><title>给项目打标签</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>这是一个html的标识 ,如果没有"<meta name=\"" + value + "\" content=\"concept\">" 这个标志 就在<title>前面添加这条数据,如果有这条数据了就要替换掉 我看过所有的写的方法,好像都没有提供把数据 inset和replace的方法 那用那个方法了? 这里不好写啊 ,用RandomAccessFile也不能满足需求while ((line = br.readLine()) != null) { if(line.indexOf("<meta name=\"DC.Type\" ")!=-1){ ........... } public static void writeText(File file, String txt, boolean append) { PrintWriter pw = null; try { pw = new PrintWriter(new FileWriter(file, append), true); pw.println(txt); } catch (Exception e) { if(LOG.isWarnEnabled()) { LOG.warn("error occured while writing file.", e); } } finally { IOUtil.close(pw); } } 这是写入后的数据<title>俇s?/╯T椪</title><meta name="DC.Type" content="Instruction"><meta name="DC.Type" content="Instruction"> content="Instruction">a name="GENERATOR" content="Microsoft FrontPage 6.0"><meta name="ProgId" content="FrontPage.Editor.Document"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>这是写入前的数据<head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 6.0"><meta name="ProgId" content="FrontPage.Editor.Document"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>这是我写的代码 while ((line = br.readLine()) != null) { if (line.indexOf("<meta name=\"DC.Type\"") != -1) { sb.append(line + "\n"); String relute = line.replace(target, mateKey); // raf.writeBytes(sb.toString() + "\n" + relute); pw = new PrintWriter(new FileWriter(strUrl, appent), true); pw.println(relute); } else { if (line.indexOf("<title>") != -1) { sb.append(line + "\n"); // BufferedWriter out = new BufferedWriter(new // FileWriter( // strUrl)); // out.write(sb.toString() + "\n" + mateKey); // raf.writeBytes("\n" + mateKey +"\n"); pw = new PrintWriter(new FileWriter(strUrl, appent), true); pw.println(mateKey); } 帮楼主看了看,逻辑部分应该是可以的:import java.io.*;public class ReplaceLine { public static void foo(String inFilename, String outFilename, String name, String content) { BufferedReader br = null; BufferedWriter bw = null; try { String line = null; br = new BufferedReader(new FileReader(inFilename), 1024); bw = new BufferedWriter(new FileWriter(outFilename), 1024); StringBuilder sb = new StringBuilder(1024); boolean found = false; while ((line = br.readLine()) != null) { if (line.matches("^\\s*<meta\\s*name=.*content=.*>\\s*$")) { found = true; sb.append("<meta name=\"").append(name).append("\" content=\"") .append(content).append("\">"); bw.write(sb.toString(), 0, sb.length()); bw.newLine(); sb.delete(0, sb.length()); } else if (line.matches("^\\s*<title>.*</title>\\s*$")) { if (!found) { sb.append("<meta name=\"").append(name).append("\" content=\"") .append(content).append("\">"); bw.write(sb.toString(), 0, sb.length()); bw.newLine(); sb.delete(0, sb.length()); } bw.write(line, 0, line.length()); bw.newLine(); } else { bw.write(line, 0, line.length()); bw.newLine(); } } } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (bw != null) { try { bw.close(); } catch (IOException e) { e.printStackTrace(); } } } } public static void main(String[] args) { foo("samp.html", "hoho.html", "name", "content"); }}Output:情况一:先看输入文件:xxx$ cat samp.html <head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="GENERATOR" content="Microsoft FrontPage 6.0"><meta name="ProgId" content="FrontPage.Editor.Document"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>运行后输出的hoho.html文件:xxx$ cat hoho.html <head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="name" content="content"><meta name="name" content="content"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>情况二:输入文件:<head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head>输出文件:<head><meta http-equiv="Content-Language" content="zh-cn"><meta http-equiv="Content-Type" content="text/html; charset=gb2312"><meta name="name" content="content"><title>如何关闭或启动关联计算算法</title><link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css"></head> 彩票摇号系统`如何避免出现重复号? Cache更新不了怎么办 关于输入函数的问题 FileFilter Filenamefilter JFileChooser可以选择文件,那选择文件夹该如何实现呢? 高分请教各位老大们两大问题。 如何改变画图的坐标原点 SimpleDateFormat的实际应用 新手又来请教大神关于堆栈和队列的问题了 ====== 水晶报表 -- Crystal Report8.0 ====== 公司新人的代码鉴定考试 大家感觉怎么样 java arryList的问题
或者用jar包吧
jsoup不错
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;public class FileTest {public static void main(String[] args) throws Exception{File file = new File("d:/d.txt");StringBuilder sb = new StringBuilder();
String s ="";
BufferedReader br = new BufferedReader(new FileReader(file));while( (s = br.readLine()) != null) {
sb.append(s + "\n");
}BufferedWriter out = new BufferedWriter(new FileWriter(file));
String words = "what you want to write";
out.write(sb.toString()+"\n"+words);br.close();
out.close();
}}
在给出文件名的情况下构造 FileWriter 对象,它具有指示是否挂起写入数据的 boolean 值。
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>给项目打标签</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>
这是一个html的标识 ,
如果没有"<meta name=\"" + value + "\" content=\"concept\">" 这个标志
就在<title>前面添加这条数据,如果有这条数据了就要替换掉
那用那个方法了? 这里不好写啊 ,用RandomAccessFile也不能满足需求
while ((line = br.readLine()) != null) {
if(line.indexOf("<meta name=\"DC.Type\" ")!=-1){
...........
}
public static void writeText(File file, String txt, boolean append) {
PrintWriter pw = null;
try {
pw = new PrintWriter(new FileWriter(file, append), true);
pw.println(txt);
} catch (Exception e) {
if(LOG.isWarnEnabled()) {
LOG.warn("error occured while writing file.", e);
}
} finally {
IOUtil.close(pw);
}
}
这是写入后的数据
<title>俇s?/╯T椪</title>
<meta name="DC.Type" content="Instruction"><meta name="DC.Type" content="Instruction">
content="Instruction">a name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>这是写入前的数据
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>这是我写的代码
while ((line = br.readLine()) != null) {
if (line.indexOf("<meta name=\"DC.Type\"") != -1) {
sb.append(line + "\n");
String relute = line.replace(target, mateKey);
// raf.writeBytes(sb.toString() + "\n" + relute);
pw = new PrintWriter(new FileWriter(strUrl, appent), true);
pw.println(relute);
} else {
if (line.indexOf("<title>") != -1) {
sb.append(line + "\n");
// BufferedWriter out = new BufferedWriter(new
// FileWriter(
// strUrl));
// out.write(sb.toString() + "\n" + mateKey);
// raf.writeBytes("\n" + mateKey +"\n");
pw = new PrintWriter(new FileWriter(strUrl, appent),
true);
pw.println(mateKey); }
public static void foo(String inFilename, String outFilename,
String name, String content) {
BufferedReader br = null;
BufferedWriter bw = null;
try {
String line = null;
br = new BufferedReader(new FileReader(inFilename), 1024);
bw = new BufferedWriter(new FileWriter(outFilename), 1024);
StringBuilder sb = new StringBuilder(1024);
boolean found = false;
while ((line = br.readLine()) != null) {
if (line.matches("^\\s*<meta\\s*name=.*content=.*>\\s*$")) {
found = true;
sb.append("<meta name=\"").append(name).append("\" content=\"")
.append(content).append("\">");
bw.write(sb.toString(), 0, sb.length());
bw.newLine();
sb.delete(0, sb.length());
} else if (line.matches("^\\s*<title>.*</title>\\s*$")) {
if (!found) {
sb.append("<meta name=\"").append(name).append("\" content=\"")
.append(content).append("\">");
bw.write(sb.toString(), 0, sb.length());
bw.newLine();
sb.delete(0, sb.length());
}
bw.write(line, 0, line.length());
bw.newLine();
} else {
bw.write(line, 0, line.length());
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} public static void main(String[] args) {
foo("samp.html", "hoho.html", "name", "content");
}
}Output:
情况一:
先看输入文件:
xxx$ cat samp.html
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 6.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>
运行后输出的hoho.html文件:
xxx$ cat hoho.html
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="name" content="content">
<meta name="name" content="content">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>情况二:
输入文件:
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>输出文件:
<head>
<meta http-equiv="Content-Language" content="zh-cn">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="name" content="content">
<title>如何关闭或启动关联计算算法</title>
<link rel="stylesheet" type="text/css" href="../../CSS/hwhelp.css">
</head>