/**
* Get the specified file content
* If any exception found,return ""
* @param path the name of the file location
* @param isReadLine if true,add line break on each line
* @return
*/
public static String getFileContent(String path,boolean isReadLine)
{
String tmp = "";
java.io.RandomAccessFile raf;
try
{
File f = new File(path);
if( (!f.isFile()) && (!f.exists()))
{
System.out.println("Invalid file:" + path);
return tmp;
} raf = new java.io.RandomAccessFile(path,"r");
String s = "";
while((s = raf.readLine()) != null)
{
//System.out.println(tmp);
tmp += s;
if(isReadLine)
tmp += "\n";
}
raf.close();
return tmp;
}catch(java.io.FileNotFoundException e)
{
return tmp;
}
catch(java.io.IOException e)
{
return tmp;
} }
}String tmp = getFileContent("urfile");
tmp = "4444" + tmp;RandomAccessFile raf = new RandomAccessFile("urfile","rw");
raf.writeBytes(tmp);
* Get the specified file content
* If any exception found,return ""
* @param path the name of the file location
* @param isReadLine if true,add line break on each line
* @return
*/
public static String getFileContent(String path,boolean isReadLine)
{
String tmp = "";
java.io.RandomAccessFile raf;
try
{
File f = new File(path);
if( (!f.isFile()) && (!f.exists()))
{
System.out.println("Invalid file:" + path);
return tmp;
} raf = new java.io.RandomAccessFile(path,"r");
String s = "";
while((s = raf.readLine()) != null)
{
//System.out.println(tmp);
tmp += s;
if(isReadLine)
tmp += "\n";
}
raf.close();
return tmp;
}catch(java.io.FileNotFoundException e)
{
return tmp;
}
catch(java.io.IOException e)
{
return tmp;
} }
}String tmp = getFileContent("urfile");
tmp = "4444" + tmp;RandomAccessFile raf = new RandomAccessFile("urfile","rw");
raf.writeBytes(tmp);
再把要写的内容写入文件,再把源文件内容写回去吧
用RandomAccessFile可以移动文件指针,但好像只是覆盖而不能插入(你也可以试试).