package com.javabase.p1;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import javax.xml.ws.WebFault;
//RandomAccessFile 文件读写使用简单方便
public class TestFile {
/**
*
* @param path 文件名 全路径
* @param str 写入的字符串
* @throws IOException
*/
public static void do_write_file(String path,String str) throws IOException {
RandomAccessFile rf = new RandomAccessFile(path, "rw");
rf.setLength(0);
rf.seek(0);
rf.writeUTF(str);
rf.close();
System.out.println("写入完成");
}
/**
*
* @param path 文件名 全路径
* @throws IOException
*/
public static void do_read_file(String path) throws IOException {
RandomAccessFile rf = new RandomAccessFile(path, "rw");
rf.seek(0);
String s = rf.readUTF();
rf.close();
System.out.println(s);
}
public static void main(String[] args) {
String path = "c:/63631.txt";
String s = "我想去海南看大海!吃鱿鱼丝!";
try {
do_write_file(path, s);
do_read_file(path);
} catch (Exception e) {
}
}
}
java的RandomAccessFile文件读写使用简单方便
最新推荐文章于 2021-07-27 12:37:51 发布