java多文件开发,Java多种写文件方式

写文件在开发小工具时常用到,比如爬取某些网站的信息,数据量不是很大,保存到本地即可。当然如果会一些额外的技能,比如多线程,网络之类的,小工具会更加有意思。

这里看下Java不同的写文件方式:

BufferedWriter

PrintWriter

FileOutputStream

DataOutputStream

RandomAccessFile

FileChannel

Files

BufferedWriter

把类中定义的方法信息,写入文件

static String fileName = "/Users/aihe/tmp/writeFileDemo.txt";

static void writeFileWithBufferedWriter() throws IOException {

Method[] methods = WriteFileDemo.class.getDeclaredMethods();

String str = Arrays.toString(methods);

BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));

writer.write(str);

writer.close();

}

17968e976bda16faf412a421f731e39e.png image-20190326081928256

追加信息到已经存在的文件:

static void appendFileWithBufferedWriter() throws IOException {

// FileWriter的第二个参数代表是否追加

BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));

writer.append("追加信息");

writer.close();

}

d5d006c6dcb1271029b0a0d7ff902dd0.png image-20190326082146211

PrintWriter

PrintWriter可以输出格式化的信息到文件中。

static void writingFileWithPrintWriter()

throws IOException {

Method[] methods = WriteFileDemo.class.getDeclaredMethods();

String str = Arrays.toString(methods);

// 可以使用FileWriter,BufferedWriter

FileWriter fileWriter = new FileWriter(fileName);

PrintWriter printWriter = new PrintWriter(fileWriter);

printWriter.printf("当前类的方法信息: %s \n方法的个数:%d \n", str, methods.length);

printWriter.close();

}

062641088faaebfa3345f18bba0e0a8a.png image-20190326082536781

FileOutputStream

用来写入二进制数据到文件中,需要将String转换为bytes。

static void writingFileWithFileOutputStream()

throws IOException {

Method[] methods = WriteFileDemo.class.getDeclaredMethods();

String str = Arrays.toString(methods);

FileOutputStream outputStream = new FileOutputStream(fileName);

// 需要将String转换为bytes

byte[] strToBytes = str.getBytes();

outputStream.write(strToBytes);

outputStream.close();

}

f0f9b358394d562ca42be3b35e139345.png image-20190326083040667

DataOutputStream

写法如上

static void writingFileWithDataOutputStream()

throws IOException {

Method[] methods = WriteFileDemo.class.getDeclaredMethods();

String str = Arrays.toString(methods);

FileOutputStream fos = new FileOutputStream(fileName);

DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));

outStream.writeUTF(str);

outStream.close();

// verify the results

String result;

FileInputStream fis = new FileInputStream(fileName);

DataInputStream reader = new DataInputStream(fis);

result = reader.readUTF();

reader.close();

System.out.println(result.equals(str));

}

RandomAccessFile

想要写入或者编辑一个已经存在的文件,而不是写入一个全新的文件或者单纯的追加,那么我们可以使用RandomAccessFile。这个类可以让我们写入特定的位置,如下:

写入中文的时候使用writeUTF方法,不然可能会乱码

static void writeToPositionWithRAF(String filename, long position)

throws IOException {

RandomAccessFile writer = new RandomAccessFile(filename, "rw");

writer.seek(position);

//写入中文的时候防止乱码

writer.writeUTF("新内容");

writer.close();

}

69e0767cbd9a1f54f2e8cf130476ade0.png image-20190326084711766

FileChannel

在处理大文件的时候,FileChannel会比标准的io更快。

static void writeWithFileChannel() throws IOException {

RandomAccessFile stream = new RandomAccessFile(fileName, "rw");

FileChannel channel = stream.getChannel();

String value = WriteFileDemo.class.getSimpleName();

byte[] strBytes = value.getBytes();

ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);

buffer.put(strBytes);

buffer.flip();

channel.write(buffer);

stream.close();

channel.close();

}

921e529bdd2b513f0e85307a7160eea9.png image-20190326085329071

Files

Files是Java7引入的工具类,通过它,我们可以创建,移动,删除,复制文件。目录也是一种特殊的文件,对目录也适用。当然也可以用于读写文件

static void writeWithFiles()

throws IOException {

String str = "Hello";

Path path = Paths.get(fileName);

byte[] strToBytes = str.getBytes();

Files.write(path, strToBytes);

String read = Files.readAllLines(path).get(0);

System.out.println(str.equals(read));

}

ed5488355d40bde8d012adda718be676.png image-20190326085644551

最后

操作文件的时候记得要关闭文件流,也可以使用java7的try-with-resource语法。

BufferedWriter 提供高效的读写字符,字符串,数组。

PrintWriter 写入格式化文字

FileOutputStream 写入二进制流

DataOutputStream 写primary类型

RandomAccessFile 随机读写文件,在指定的位置编辑文件

FileChannel 写入大文件

作者:Real_man

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值