File file = new File("");
RandomAccessFile ras = new RandomAccessFile(file, "rw");
long length = ras.length();
byte[] contentByte = new byte[(int) length];
ras.read(contentByte);
String contentString = new String(contentByte);
long pos = contentString.lastIndexOf("test");
String after=contentString.substring(contentString.indexOf("test")+"test".length());
ras.seek(pos);
ras.writeBytes(" 12341\r\n");
ras.writeBytes(" 1344344\r\n");
ras.writeBytes(after);
ras.close();
RandomAccessFile ras = new RandomAccessFile(file, "rw");
long length = ras.length();
byte[] contentByte = new byte[(int) length];
ras.read(contentByte);
String contentString = new String(contentByte);
long pos = contentString.lastIndexOf("test");
String after=contentString.substring(contentString.indexOf("test")+"test".length());
ras.seek(pos);
ras.writeBytes(" 12341\r\n");
ras.writeBytes(" 1344344\r\n");
ras.writeBytes(after);
ras.close();
import java.util.*;class p2
{
public static void main(String[] args)
{
try
{ BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream("p1.java")));
DataOutputStream dos=new DataOutputStream(new FileOutputStream("pp.java"));
String str;Object o;
//while(br.ready())
while((str=br.readLine())!=null)
{
l.add(str);
//System.out.println(str);
}
//ListIterator it=l.listIterator();
int i=1;
while ((o=l.removeLast())!=null)
{
System.out.println((String)o);
dos.writeBytes(i+" "+(String)o.replaceAll("xxx","yyy")+"\n");
i++;
} /*
while(it.hasNext())
{
System.out.println((String)it.next());
}
while(it.hasPrevious())
{
System.out.println((String)it.previous());
}
*/
//System.out.println("Hello World!");
}
catch (Exception e)
{
} }
}
就是:<font ...>asf</font>输出以后还是:<font ...>asf</font>
就好象ASP的Server.HTMLEncoding()一样
我明白你的方法了
我是这样写的!
File file = new File("");
RandomAccessFile ras = new RandomAccessFile(file, "rw");
long length = ras.length();
byte[] contentByte = new byte[(int) length];
ras.read(contentByte);
String contentString = new String(contentByte);
contentString = contentString.replaceAll( "xxx", "yyy" );
ras.seek(0);
ras.writeBytes( contentString + "\r\n");
ras.close();
这样可以完成,但有几个问题!!
1:我是要生成一个新的文件,而不是在原有文件下修改!
2:如果替换的是中文,会有乱码!!
第一个问题,我曾这样想,先拷贝一个文件过去,然后修改!但拷贝我也不知道怎么写
第二个问题是根本就不知道怎么解决了!!
谢谢,再指点一下!!
现在就想知道,怎么解决乱码的问题了,顺便说说,我生成的是HTML的文件!!
还有前面我问的两个问题
1是怎么输出包含HTML标签的STRING
2是怎么拷贝文件,没有直接的方法??
ras.read(contentByte);
String contentString = new String(contentByte,"gb2312");//2.关于输出包含HTML标签的STRING,使用转义字符,将>用>替换,将<用<替换3.拷贝文件:
public void copy(String from, String to) throws IOException {
File f = new File(to);
f.getParentFile().mkdirs(); byte[] buffer = new byte[1024];
int length = 0;
InputStream fis = new FileInputStream(from);
FileOutputStream fos = new FileOutputStream(f); while ((length = fis.read(buffer)) >= 0) {
fos.write(buffer, 0, length);
}
fos.close();
}
你的拷贝文件的代码不能运行,而且都不知道参数是什么?!请解释一下!!
to:目标文件路径,比如c:/test.txt关于乱码,主要是字符集的转换,多试几下
直接打开浏览正确,但是用TOMCAT浏览,就会出现乱码!!
应该怎么设置,才能让TOMCAT浏览html文件不会这样!!
<HEAD>
<meta http-equiv="Content-Language" content="zh-cn">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
</HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
有一个“非”字,用TOMCAT浏览以后,显示的是
·Ç
这是什么样的字符啊,怎么会事,帮帮我!!
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=gb2312">
</HEAD>
<body>
非
</body>
</html>就那么简单,在HTML里面不是乱码,也说了,直接浏览打开是没错的,用TOMCAT就会有问题
我也改了ISO8859-1等,但还是没用,我觉得好奇怪啊!!!
这是怎么会事!!
用记事本写的啊!!那么TOMCAT能不能正常浏览HTML文件,正常情况下,还是要设置什么!!
<meta http-equiv="Content-Language" content="zh-cn">
http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=190524 长知识啊!!!