写入文件和获取文件内容

来源:互联网 发布:市国土资源局待遇 知乎 编辑:程序博客网 时间:2024/06/06 15:38

代码如下:

import java.io.FileNotFoundException;import java.io.IOException;import java.io.RandomAccessFile;public class Test1 {public static void main(String[] args) throws IOException {/** * 写一个字符串到文件里面去 */RandomAccessFile raf = new RandomAccessFile("demo.txt","rw");String input = "helloWorld! 张三";byte[] arr = input.getBytes(); raf.write(arr);//写入文件raf.close();/** * 取出demo.txt中的内容 */RandomAccessFile raf2 = new RandomAccessFile("demo.txt","r");byte[] bu = new byte[(int) raf2.length()];raf2.read(bu);System.out.println(new String(bu).trim());raf2.close();}}
测试结果如下:

helloWorld! 张三

0 0
原创粉丝点击