Java FileWriter 示例
Java的FileWriter
- Java FileWriter class is a part of java.io package.
- FileWriter is a sub class of java.io.OutputStreamWriter class.
- FileWriter is meant for writing streams of characters.
- FileWriter is used to write to character files. Its write() methods allow you to write character(s) or strings to a file.
- FileWriters are usually wrapped by higher-level Writer objects, such as BufferedWriter or PrintWriter, which provide better performance and higher-level, more flexible methods to write data.
FileWriter 构造函数
- FileWriter(File file): 使用指定的File对象创建一个FileWriter对象。如果文件存在但是是一个目录而不是一个普通文件,或者文件不存在但是无法创建,或者无法以任何其他原因打开文件,则抛出一个IOException。
FileWriter(File file, boolean append): 使用指定的File对象创建一个FileWriter对象。如果第二个参数为true,则会将字节写入文件的末尾而不是开头。如果文件存在但是是一个目录而不是一个普通文件,或者文件不存在但是无法创建,或者无法以任何其他原因打开文件,则抛出一个IOException。
FileWriter(FileDescriptor fd): 创建一个与指定文件描述符关联的FileWriter对象。
FileWriter(String fileName): 使用指定的文件名创建一个FileWriter对象。如果文件存在但是是一个目录而不是一个普通文件,或者文件不存在但是无法创建,或者无法以任何其他原因打开文件,则抛出一个IOException。
FileWriter(String fileName, boolean append): 使用指定的文件名创建一个FileWriter对象,并使用一个布尔值指示是否追加写入的数据。如果第二个参数为true,则数据将被写入文件的末尾而不是开头。如果文件存在但是是一个目录而不是一个普通文件,或者文件不存在但是无法创建,或者无法以任何其他原因打开文件,则抛出一个IOException。
Java FileWriter示例
Java FileWriter示例展示了如何使用FileWriter类进行文件写入。
FileWriter继承了java.io.OutputStreamWriter和java.io.Writer类的方法。我们来看一下下面的一些方法和示例。
写 int c
这个方法写入一个由整数c指定的单个字符。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
import java.io.IOException;
/**
* Java write file using FileWriter write method
*
* @author pankaj
*
*/
public class FileWriterWriteIntExample {
public static void main(String[] args) {
FileWriter fileWriter = null;
try {
fileWriter = new FileWriter("D:/data/file.txt");
//inherited method from java.io.OutputStreamWriter
fileWriter.write(65);
fileWriter.write(66);
fileWriter.write(67);
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if (fileWriter != null) {
fileWriter.flush();
fileWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write method using try with resource
*
* @author pankaj
*
*/
public class FileWriterWriteIntTryWithResource {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(65);
fileWriter.write(66);
fileWriter.write(67);
} catch (Exception e) {
e.printStackTrace();
}
}
}
注意:上述程序中,fileWriter.write(65)会将字符A写入文件,因为65是字符A的十进制值,所以整数65会被转换成字符A,其他字符同理。
写入 String str, int off, int len
这种方法从整数off开始,将字符串str的一部分写入到整数len中。
- str: String to be written
- off: Offset from which to start reading characters
- len: Number of characters to be written
如果len参数的值为负数,则不会写入任何字符。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(String s, int off, int len) method
*
* @author pankaj
*
*/
public class FileWriterWriteStringExample {
public static void main(String[] args) {
String data = "This is FileWriter Example.";
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(data, 8, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
将字符数组cbuf的从off位置开始的长度为len的部分写入。
该方法将字符数组char[] cbuf中从off位置开始,长度为len的一部分写入。
- cbuf: A character array
- off: Offset from which to start reading characters
- len : Number of characters to write
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(char[] cbuf, int off, int len) method
*
* @author pankaj
*
*/
public class FileWriterWriteCharArray {
public static void main(String[] args) {
char[] data = "This is FileWriter Example.".toCharArray();
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.OutputStreamWriter
fileWriter.write(data, 8, 10);
} catch (Exception e) {
e.printStackTrace();
}
}
}
请将字符数组cbuf写入。
这个方法将由cbuf指定的字符数组写入。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(char[] cbuf) method
*
* @author pankaj
*
*/
public class FileWriterWriteCharArrayExample {
public static void main(String[] args) {
char[] data = "This is FileWriter Example.".toCharArray();
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write(data);
} catch (Exception e) {
e.printStackTrace();
}
}
}
写 String str
这种方法将一个字符串值写入由str指定的文件中。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter write(String str) method
*
* @author pankaj
*
*/
public class FileWriterWriteString {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
} catch (Exception e) {
e.printStackTrace();
}
}
}
添加字符c
将指定字符追加到此写入器中,其中c是要追加的16位字符。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file using FileWriter append(char c) method
*
* @author pankaj
*
*/
public class FileWriterAppendCharacter {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
fileWriter.append('C');
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行flush()
这个方法清空了流。当调用flush()方法时,它立即将数据写入输出文件。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file with FileWriter flush() method
*
* @author pankaj
*
*/
public class FileWriterFlushExample {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
//inherited method from java.io.OutputStreamWriter
fileWriter.flush();
fileWriter.write(" Tutorials");
fileWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
}
close()
在关闭流之前,该方法会刷新流。一旦流被关闭,调用write()或flush()方法将导致抛出IOException。关闭已关闭的流不会产生任何效果。
package com.Olivia.io.filewriter;
import java.io.FileWriter;
/**
* Java write file with FileWriter close() method
*
* @author pankaj
*
*/
public class FileWriterCloseExample {
public static void main(String[] args) {
try(FileWriter fileWriter = new FileWriter("D:/data/file.txt")) {
//inherited method from java.io.Writer
fileWriter.write("JournalDev");
//inherited method from java.io.OutputStreamWriter
fileWriter.close();;
fileWriter.write(" Tutorials");
} catch (Exception e) {
e.printStackTrace();
}
}
}
输出:
java.io.IOException: Stream closed
at sun.nio.cs.StreamEncoder.ensureOpen(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at sun.nio.cs.StreamEncoder.write(Unknown Source)
at java.io.OutputStreamWriter.write(Unknown Source)
at java.io.Writer.write(Unknown Source)
at com.Olivia.examples.FileWriterCloseExample.main(FileWriterCloseExample.java:20)
FileWriter 和 FileOutputStream 的比较
- FileWriter is meant for writing streams of characters while FileOutputStream is used for writing streams of raw bytes.
- FileWriter deal with 16-bit characters while FileOutputStream deals with 8-bit bytes.
- FileWriter can handle Unicode strings while FileOutputStream writes bytes to a file and do not accepts characters or strings hence it needs to wrapped up by OutputStreamWriter to accept strings.
还要检查Java写文件以了解如何在Java中编写文件。Java FileWriter就是这些,希望没有漏掉任何重要的内容。
你可以从我们的GitHub存储库下载所有示例代码。
参考: API文档