/**
* 读出写出
* @param oldFileName 源文件
* @param newFileName 新文件
* @throws IOException
*/
public static void testRead(String oldFileName,String newFileName) throws IOException{
FileOutputStream fos=new FileOutputStream(new File(newFileName));
RandomAccessFile raf=new RandomAccessFile(new File(oldFileName), "rw");
fos.write(raf.read(new byte[8]));
fos.flush();
fos.close();
raf.close();
}
public static void fileWrite() throws FileNotFoundException, IOException {
testRead("G:/森云/测试文件1。txt","G:/newFile.txt");
}
有简单的方法,给你个例子
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.Writer;
import java.util.Scanner;
public class Test {
public static void main(String args[]) {
String srcFile = "D:/a.txt";
String toFile = "D:/b.txt";
try {
String result = read(srcFile);
write(result, toFile);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String read(String srcFile) throws FileNotFoundException {
Scanner in = new Scanner(new File(srcFile));
String result = "";
while (in.hasNextLine()) {
result += in.nextLine() + "\r\n";
}
in.close();
return result;
}
private static void write(String result, String toFile) throws Exception {
Writer w = new FileWriter(new File(toFile));
w.write(result);
w.flush();
w.close();
}
}
RandomAccessFile
可读、可写的呀,另打开一个写入就好了