本文共 11214 字,大约阅读时间需要 37 分钟。
java.io
package. Java FileWriter类是java.io
软件包的一部分。 java.io.OutputStreamWriter
class. FileWriter是java.io.OutputStreamWriter
类的子类。 write()
methods allow you to write character(s) or strings to a file. FileWriter用于写入字符文件。 它的write()
方法允许您将字符或字符串写入文件。 PrintWriter
, which provide better performance and higher-level, more flexible methods to write data. FileWriter通常由更高级别的Writer对象包装,例如或PrintWriter
,它们提供更好的性能以及更高级别,更灵活的数据写入方法。 FileWriter(File file)
: Creates a FileWriter
object using specified object. It throws an IOException
if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason. FileWriter(File file)
:使用指定的对象创建一个FileWriter
对象。 如果文件存在但它是目录而不是常规文件,或者不存在但无法创建,或者由于任何其他原因而无法打开,则抛出IOException
。 FileWriter(File file, boolean append)
: Creates a FileWriter object using specified File object. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. It throws an IOException if the file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason. FileWriter(File file, boolean append)
:使用指定的File对象创建一个FileWriter对象。 如果第二个参数为true,则字节将被写入文件的末尾而不是开头。 如果文件存在但它是目录而不是常规文件,或者不存在但无法创建,或者由于任何其他原因而无法打开,则抛出IOException 。 FileWriter(FileDescriptor fd)
: Creates a FileWriter object associated with specified file descriptor. FileWriter(FileDescriptor fd)
:创建一个与指定文件描述符关联的FileWriter对象。 FileWriter(String fileName)
: Creates a FileWriter object using specified fileName. It throws an IOException
if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason. FileWriter(String fileName)
:使用指定的fileName创建一个FileWriter对象。 如果命名文件存在但是目录而不是常规文件,或者不存在但无法创建,或者由于任何其他原因而无法打开,则抛出IOException
。 FileWriter(String fileName, boolean append)
: Creates a FileWriter object using specified fileName with a boolean indicating whether or not to append the data written. If the second argument is true, then the data will be written to the end of the file rather than the beginning. It throws an IOException if the named file exists but is a directory rather than a regular file or does not exist but cannot be created, or cannot be opened for any other reason. FileWriter(String fileName, boolean append)
:使用指定的fileName和一个布尔值创建FileWriter对象,该布尔值指示是否要附加写入的数据。 如果第二个参数为true,则数据将被写入文件的末尾而不是开头。 如果命名文件存在但是目录而不是常规文件,或者不存在但无法创建,或者由于任何其他原因而无法打开,则抛出IOException。 FileWriter inherits the method of java.io.OutputStreamWriter
and java.io.Writer
classes.
FileWriter继承了java.io.OutputStreamWriter
和java.io.Writer
类的java.io.Writer
。
Let’s have a look at the below methods with examples.
让我们用示例来看看下面的方法。
This method writes a single character specified by int c.
此方法写入一个由int c指定的字符。
package com.journaldev.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(); } } }}
FileWriter implements AutoCloseable
interface, hence we can use while using FileWriter class.
FileWriter实现了AutoCloseable
接口,因此在使用FileWriter类时我们可以 。
package com.journaldev.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(); } }}
Note: In the above program fileWriter.write(65)
will write A into file because the 65 is the decimal value for the character A, hence integer 65 will be converted into character A and same for the other.
注意 :在上面的程序中,因为65是字符A的十进制值,所以fileWriter.write(65)
会将A写入文件,因此整数65将被转换为字符A,其他字符也将转换为字符A。
This method writes a portion of String str
from int off
to int len
.
此方法将String str
的一部分从int off
写入int len
。
If the value of the len parameter is negative then no characters are written.
如果len参数的值为负,则不会写入任何字符。
package com.journaldev.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(); } }}
This method writes a portion of an array of characters specified by char[] cbuf
from int off
to int len
.
此方法将char[] cbuf
指定的char[] cbuf
数组的一部分从int off
写入int len
。
package com.journaldev.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(); } }}
This method writes the array of character specified by cbuf.
此方法写入cbuf指定的字符数组。
package com.journaldev.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(); } }}
This method writes a string value into file specified by str
.
此方法将字符串值写入str
指定的文件中。
package com.journaldev.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(); } }}
This method appends the specified character to this writer where c
is the 16-bit character to append.
此方法将指定的字符附加到此编写器,其中c
是要附加的16位字符。
package com.journaldev.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(); } }}
This method flushes the stream. When flush()
method is called it immediately writes the data to the output file.
此方法刷新流。 调用flush()
方法时,它将立即将数据写入输出文件。
package com.journaldev.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(); } }}
This method flush the stream before close it. Once the stream has been closed, invocation of write()
or flush()
method will cause an IOException
to be thrown. Closing a previously closed stream has no effect.
此方法在关闭流之前先对其进行冲洗。 一旦关闭流,调用write()
或flush()
方法将导致引发IOException
。 关闭先前关闭的流无效。
package com.journaldev.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(); } }}
Output:
输出:
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.journaldev.examples.FileWriterCloseExample.main(FileWriterCloseExample.java:20)
OutputStreamWriter
to accept strings. FileWriter可以处理Unicode字符串,而FileOutputStream将字节写入文件中并且不接受字符或字符串,因此需要由OutputStreamWriter
对其进行包装以接受字符串。 Also check for more about how to write file in java.
另请检查以获取有关如何在的更多信息。
That’s all for Java FileWriter, I hope nothing important got missed here.
Java FileWriter就这些了,我希望这里没有重要的事情。
Reference:
参考:
翻译自:
转载地址:http://smqzd.baihongyu.com/