java.io
Class RandomAccessFilejava.lang.Object
extended byjava.io.RandomAccessFileAll Implemented Interfaces:
DataInput, DataOutputpublic class RandomAccessFile
extends Object
implements DataOutput, DataInputInstances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.Since:
JDK1.0
Class RandomAccessFilejava.lang.Object
extended byjava.io.RandomAccessFileAll Implemented Interfaces:
DataInput, DataOutputpublic class RandomAccessFile
extends Object
implements DataOutput, DataInputInstances of this class support both reading and writing to a random access file. A random access file behaves like a large array of bytes stored in the file system. There is a kind of cursor, or index into the implied array, called the file pointer; input operations read bytes starting at the file pointer and advance the file pointer past the bytes read. If the random access file is created in read/write mode, then output operations are also available; output operations write bytes starting at the file pointer and advance the file pointer past the bytes written. Output operations that write past the current end of the implied array cause the array to be extended. The file pointer can be read by the getFilePointer method and set by the seek method.It is generally true of all the reading routines in this class that if end-of-file is reached before the desired number of bytes has been read, an EOFException (which is a kind of IOException) is thrown. If any byte cannot be read for any reason other than end-of-file, an IOException other than EOFException is thrown. In particular, an IOException may be thrown if the stream has been closed.Since:
JDK1.0
public static void main(String args[]) {
int line = 5; // 这里指定的是修改第几行
try {
RandomAccessFile Raf = new RandomAccessFile("c:/test.txt", "rw");
for (int i = 1; i < line; i++) {
Raf.readLine();
}
Raf.writeBytes("111"); // 写入修改字符
}
catch (Exception e) {
}
}
}
{
public static void main(String args[])
{
String conStr="---test---",temp;
int rows=3;
try{
File file = new File("c:/test.txt");
FileInputStream fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
StringBuffer buf = new StringBuffer();
for(int j=1;j < rows && (temp=br.readLine())!= null;j++){
buf = buf.append(temp);
buf = buf.append( System.getProperty("line.separator"));
}
buf = buf.append(conStr);
br.readLine();
while ((temp=br.readLine())!= null)
{
buf = buf.append( System.getProperty("line.separator"));
buf = buf.append(temp);
}
br.close();
FileOutputStream fos = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(fos);
pw.write(buf.toString().toCharArray());
pw.flush();
pw.close();
}catch (IOException e)
{
System.out.println(e);
} }
}给我个回应 ,谢谢
我的空间不支持或者碰到这个就发生异常,不明白为什么???????? RandomAccessFile file = new RandomAccessFile(file,"rw");难道我少了什么吗
RandomAccessFile file = new RandomAccessFile(file,"rw");
对象file做为文件名来源的字符串,同时又作为随机文件对象的实例,怎么可能没问题?
---------
我暂时是用这方法 ,效率很高
你完全后有没有用close关掉文件连接,如果没有关写的内容是不会保存的
在运行完毕后用RandomAccessFile的close()方法,如果是在写的中途可以用flush()方法来完成文件保存