您的位置:首页 > 编程语言 > Java开发

java的RandomAccessFile文件读写使用简单方便

2019-05-14 20:43 423 查看
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) {

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: