package com.anran.filereader;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
/**
* @author anran
* @version 创建时间:2017年9月9日 上午9:09:44 类说明 :
*/
public class WriteFile {
public static void main(String[] args) throws IOException {
String path = "C:\\Users\\Administrator\\Desktop\\ecplise快捷键.txt";
String targetPath = "C:\\Users\\Administrator\\Desktop\\text.txt";
// fileWriteByInputStream(path, targetPath);
// fileWriteByFileInputStream(path, targetPath);
// fileWriteByBufferedReader(path, targetPath);
// fileWriteByBufferedWriter(targetPath, "AAA安然AAA");
// fileWriteByFileWriter(targetPath, "CCCC安然CCCCC");
fileWriteByRandomAccessFile(targetPath, "EEEE安然EEEEE");
}
// 会覆盖原有内容(相当于文件copy)
public static void fileWriteByInputStream(String source, String target)
throws IOException {
try (InputStream in = new FileInputStream(source)) {
try (OutputStream out = new FileOutputStream(target)) {
byte[] buffer = new byte[4096];
int bytesToRead;
while ((bytesToRead = in.read(buffer)) != -1) {
out.write(buffer, 0, bytesToRead);
}
}
}
}
// 会覆盖原有内容(相当于文件copy)
public static void fileWriteByFileInputStream(String source, String target)
throws IOException {
try (FileInputStream in = new FileInputStream(source)) {
try (FileOutputStream out = new FileOutputStream(target)) {
FileChannel inChannel = in.getChannel();
FileChannel outChannel = out.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(4096);
while (inChannel.read(buffer) != -1) {
buffer.flip();
outChannel.write(buffer);
buffer.clear();
}
}
}
}
// 会覆盖原有内容(相当于文件copy)
public static void fileWriteByBufferedReader(String source, String target)
throws IOException {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(
new FileInputStream(source), "UTF-8"))) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(
target))) {
String str = null;
while (null != (str = reader.readLine())) {
writer.write(str);
writer.write("\n");
}
}
}
}
// 文件中追加内容
public static void fileWriteByBufferedWriter(String file, String conent)
throws FileNotFoundException, IOException {
// FileOutputStream构造器第二个参数设置为true是追加
try (BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(file, true)))) {
out.write(conent);
}
}
// 文件中追加内容
public static void fileWriteByFileWriter(String fileName, String content)
throws IOException {
// 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件
try (FileWriter writer = new FileWriter(fileName, true)) {
writer.write(content);
}
}
public static void fileWriteByRandomAccessFile(String fileName,
String content) throws IOException {
// 打开一个随机访问文件流,按读写方式
try (RandomAccessFile randomFile = new RandomAccessFile(fileName, "rw")) {
// 文件长度,字节数
long fileLength = randomFile.length();
// 将写文件指针移到文件尾。
randomFile.seek(fileLength);
randomFile.write(content.getBytes());
}
}
}
java文件写入
最新推荐文章于 2025-05-27 10:30:23 发布