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();
}
}
java write to file
最新推荐文章于 2023-03-31 19:48:14 发布