import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*
* 支持随机访问;
* 根据mode,既可以是输入流,也可以是输出流;
*/
public class RandomAccessFileDemo
{
public static void main(String[] args) {
method1();
method2();
method3();
}
public static void method1()
{
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("d:\\a\\a.txt"), "r");
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
byte[] b = new byte[20];
int len;
while((len = raf1.read(b)) != -1)
{
raf2.write(b, 0 ,len);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(raf1 != null)
{
try
{
raf1.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
if(raf2 != null)
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void method2()
{
RandomAccessFile raf2 = null;
try
{
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
raf2.seek(raf2.length()); //调到文件末尾;
raf2.write("sss".getBytes());//在末尾处添加内容;
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
//在文件中插入内容;
public static void method3()
{
RandomAccessFile raf2 = null;
try
{
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
raf2.seek(4);
byte[] b = new byte[9];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf2.read(b)) != -1)
{
sb.append(new String(b, 0, len));
}
raf2.seek(4);
raf2.write("sss".getBytes());
raf2.write(sb.toString().getBytes());
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
/*
* 支持随机访问;
* 根据mode,既可以是输入流,也可以是输出流;
*/
public class RandomAccessFileDemo
{
public static void main(String[] args) {
method1();
method2();
method3();
}
public static void method1()
{
RandomAccessFile raf1 = null;
RandomAccessFile raf2 = null;
try {
raf1 = new RandomAccessFile(new File("d:\\a\\a.txt"), "r");
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
byte[] b = new byte[20];
int len;
while((len = raf1.read(b)) != -1)
{
raf2.write(b, 0 ,len);
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
if(raf1 != null)
{
try
{
raf1.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
if(raf2 != null)
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}
public static void method2()
{
RandomAccessFile raf2 = null;
try
{
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
raf2.seek(raf2.length()); //调到文件末尾;
raf2.write("sss".getBytes());//在末尾处添加内容;
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
//在文件中插入内容;
public static void method3()
{
RandomAccessFile raf2 = null;
try
{
raf2 = new RandomAccessFile(new File("f:\\a\\a.txt"), "rw");
raf2.seek(4);
byte[] b = new byte[9];
int len;
StringBuffer sb = new StringBuffer();
while((len = raf2.read(b)) != -1)
{
sb.append(new String(b, 0, len));
}
raf2.seek(4);
raf2.write("sss".getBytes());
raf2.write(sb.toString().getBytes());
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}finally
{
try
{
raf2.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
}
}