java write to file

package com.karakal.variety.utils;

import org.junit.Test;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import static org.junit.Assert.assertEquals;


public class JavaWriteToFile {


    public static void useBufferedWriter(String text, String fileName, boolean append) throws IOException {
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(fileName, append));
        bufferedWriter.write(text);
        bufferedWriter.close();
    }

    @Test
    public void useBufferedWriter() throws IOException {
        useBufferedWriter("hello", "text.txt", false);
        useBufferedWriter("\nword", "text.txt", true);
    }

    public static void usePrintWriter(String fileName) throws IOException {
        FileWriter fileWriter = new FileWriter(fileName);
        PrintWriter printWriter = new PrintWriter(fileWriter);

        printWriter.printf("hello  %s ", "world");
        printWriter.close();
    }

    @Test
    public void usePrintWriter() throws IOException {
        usePrintWriter("printWriter.txt");

    }

    public static void useFileOutPutStream(String text, String fileName) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(fileName);
        outputStream.write(text.getBytes());
        outputStream.close();
    }

    @Test
    public void useFileOutPutStream() throws IOException {
        useFileOutPutStream("ho", "outPutStream.txt");

    }


    public static void useDataOutPutStream(String text, String fileName) throws IOException {
        FileOutputStream fos = new FileOutputStream(fileName);
        DataOutputStream outStream = new DataOutputStream(new BufferedOutputStream(fos));
        outStream.writeUTF(text);
        outStream.close();

    }

    @Test
    public void useDataOutPutStream() throws IOException {
        useDataOutPutStream("中国", "dataOutPutStream.txt");
        String result;
        FileInputStream fis = new FileInputStream("dataOutPutStream.txt");
        DataInputStream reader = new DataInputStream(fis);
        result = reader.readUTF();
        reader.close();
        assertEquals("中国", result);
    }

    public static void useRandomAccessFile(String text, String fileName, Long position) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile(fileName, "rw");
        randomAccessFile.seek(position);
        RandomAccessFile read = new RandomAccessFile(fileName, "r");
        read.seek(position);
        String line = read.readLine();
        randomAccessFile.write((text + line).getBytes());
        randomAccessFile.close();

    }

    @Test
    public void useRandomAccessFile() throws IOException {
        useFileOutPutStream("ho", "outPutStream.txt");
        useRandomAccessFile("ell", "outPutStream.txt", 1L);

    }

    public static void useFiles(String text, String fileName) throws IOException {
        Path path = Paths.get(fileName);
        byte[] strToBytes = text.getBytes();

        Files.write(path, strToBytes);


    }

    @Test
    public void useFiles() throws IOException {
        String text = "hello";
        String fileName = "files.txt";
        useFiles(text, fileName);
        Path path = Paths.get(fileName);
        String read = Files.readAllLines(path).get(0);
        assertEquals(text, read);
    }


    public static void useFileChannel(String text, String fileName) throws IOException {
        RandomAccessFile stream = new RandomAccessFile(fileName, "rw");
        FileChannel channel = stream.getChannel();

        byte[] strBytes = text.getBytes();
        ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
        buffer.put(strBytes);
        buffer.flip();
        channel.write(buffer);
        stream.close();
        channel.close();

    }

    @Test
    public void fileChannel() throws IOException {
        useFileChannel("hello", "fileChannel.txt");
        RandomAccessFile reader = new RandomAccessFile("fileChannel.txt", "r");
        assertEquals("hello", reader.readLine());
        reader.close();
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值