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

  • Post author:
  • Post category:java


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) {

}

}

}



版权声明:本文为weixin_36212334原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。