java中的FileWriter用于创建文件,可以向创建的文件中写入字符;输出流类是 FileWriter 类的基类,因为它是从 FileWriter 类继承的,并且该类的构造函数所做的假设是,如果要由我们指定这些值,则允许默认字符编码和默认字节缓冲区大小。输出流编写器必须在文件输出流上构造。
Java FileWriter 类声明:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java.lang.Object Java.io.Writer Java.io.OutputStreamWriter Java.io.FileWriter
java中的FileWriter类由多个构造函数组成。他们是:
java中的FileWriter类由几个方法组成,分别是:
下面是用Java实现FileWriter的例子:
演示 FileWriter 类创建的 Java 程序。
代码:
import java.io.*; public class Read { public static void main(String args[])throws IOException { File file1 = new File("check.txt"); // A file is created file1.createNewFile(); // Object of FileWriter is created FileWriter writer1 = new FileWriter(file1); // Contents are written to the file writer1.write("Welcome to FileWriter"); writer1.flush(); writer1.close(); // Object of filereader is created FileReader read = new FileReader(file1); char [] a1 = new char[50]; read.read(a1); // array contents are read for(char ch : a1) System.out.print(ch); // characters are printed one by one read.close(); } }
输出:
使用 FileWriter 类创建文本文件的 Java 程序。
代码:
import java.io.*; public class Example { public static void main(String[] args) { //File constructor is initialized File file1 = new File("C:/Users/shivakumarsh/Desktop/Learning/source.txt"); try { boolean create = file1.createNewFile(); if (create) { System.out.println("File creation is succesful."); }else { System.out.println("There is already a file by this name."); } } catch (IOException e) { e.printStackTrace(); } } }
Java 程序演示将字符串附加到文件末尾。
代码:
import java.io.*; public class Example { public static void append(String fileName, String string) { try { // The file is opened in append mode BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName, true)); out1.write(string); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } } public static void main(String[] args) throws Exception { // A sample file is created with some text containing in it String fileName = "Shobha.txt"; try { BufferedWriter out1 = new BufferedWriter( new FileWriter(fileName)); out1.write("Learning appending\n"); out1.close(); } catch (IOException e) { System.out.println("occurance of exception" + e); } // The newly created file is appended with the string value passed here String string = "Learning is good"; append(fileName, string); // contents of the modified file are printed here try { BufferedReader in1 = new BufferedReader( new FileReader("Shobha.txt")); String mystr; while ((mystr = in1.readLine()) != null) { System.out.println(mystr); } } catch (IOException e) { System.out.println("Occurance of exception" + e); } } }
输出:
Java 程序使用 FileWriter 逐行写入文件。
代码:
import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.util.ArrayList; import java.util.List; public class List { public static void main(String[] args) throws Exception { ArrayList<String> list1 = new ArrayList<String>(); // The newly created file is appended with the string value passed here list1.add("Understanding an example in java"); list1.add("second line of the file"); list1.add("third line of the file"); writeFile("C:/Users/shivakumarsh/Desktop/Learning/source.txt", list1); System.out.println("file creation is successful"); } public static void write(String fileName, List<String> list1) throws Exception { FileWriter fwd = null; BufferedWriter bwd = null; try { fwd = new BufferedWriter(fwd); for (int j = 0; list1 != null && j < list.size(); j++) { bwd.write(list.get(j)); bwd.write("\n"); } } catch (Exception e1) { System.out.println("error occured:" + e1.getMessage()); throw e1; } finally { try { bwd.close(); } catch (Exception e1) { } try { fwd.close(); } catch (Exception e1) { } } } }
输出:
在本教程中,我们了解了 FileWriter 的概念,如 FileWriter 的定义、如何声明 FileWriter、FileWriter 中的构造函数以及编程示例来演示 FileWriter 类的创建,使用 FileWriter 类创建文本文件,使用FileWriter 类将文本追加到由内容组成的现有文件中,使用 FileWriter 类逐行写入新文件,即一行一行地添加内容。
这是 Java 中 FileWriter 的指南。这里我们讨论如何声明 FileWriter、FileWriter 中的构造函数及其方法以及代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –
以上是Java 中的文件写入器的详细内容。更多信息请关注PHP中文网其他相关文章!