Java 中的文件写入器
java中的FileWriter用于创建文件,可以向创建的文件中写入字符;输出流类是 FileWriter 类的基类,因为它是从 FileWriter 类继承的,并且该类的构造函数所做的假设是,如果要由我们指定这些值,则允许默认字符编码和默认字节缓冲区大小。输出流编写器必须在文件输出流上构造。
Java FileWriter 类声明:
开始您的免费软件开发课程
网络开发、编程语言、软件测试及其他
Java.lang.Object Java.io.Writer Java.io.OutputStreamWriter Java.io.FileWriter
Java 中 FileWriter 的构造函数
java中的FileWriter类由多个构造函数组成。他们是:
- FileWriter(File file):当给定一个文件对象时,使用该方法 FileWriter(File file) 构造一个 FileWriter 对象。
- FileWriter(File file, Boolean append): 当给定一个文件对象时,会构造一个 FileWriter 对象,布尔值指示是否使用该方法追加正在写入的数据FileWriter(文件文件,布尔追加)。
- FileWriter(FileDescriptor fd): 构造一个 FileWriter 对象,该对象与 FileWriter(FileDescriptor fd) 方法中指定为参数的文件描述符关联。
- FileWriter(String fileName): 当给出文件名时,使用此方法 FileWriter(String fileName) 创建一个 FileWriter 对象。
- FileWriter(String filename, Boolean append): 当给定文件名时,会创建一个 FileWriter 对象,Boolean 值指示是否使用此方法追加正在写入的数据FileWriter(字符串文件名,布尔追加).
Java 中 FileWriter 的方法
java中的FileWriter类由几个方法组成,分别是:
- Public void write(int c) throws IOException: 使用此方法写入单个字符; public void write(int c) 抛出 IOException。
- Public void write(char [] Stir) throws IOException: 使用此方法写入字符数组。 public void write(char [] Stir) 抛出 IOException。
- Public void write(string str) throws IOException: 使用此方法写入字符串。 public void write(string str) 抛出 IOException。
- Public void write(string str, int off, int len) throws IOException: 使用此方法写入字符串的一部分 Public void write(string str, int off, int len) throws IOException其中 off 表示必须开始写入字符的偏移量,len 表示要写入的字符数。
- Public voidlush() throws IOException: 使用此方法刷新流 Public voidlush() throws IOException.
- Public void close() throws IOException: 首先刷新流,然后使用此方法关闭 writer Public void close() throws IOException。
用 Java 实现 FileWriter 的示例
下面是用Java实现FileWriter的例子:
示例#1
演示 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(); } }
输出:
示例#2
使用 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(); } } }
示例 #3
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); } } }
输出:
示例#4
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 中的布局
- Java 编译器
- Java 中的合并排序
- Java 并行流
以上是Java 中的文件写入器的详细内容。更多信息请关注PHP中文网其他相关文章!

热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

记事本++7.3.1
好用且免费的代码编辑器

SublimeText3汉化版
中文版,非常好用

禅工作室 13.0.1
功能强大的PHP集成开发环境

Dreamweaver CS6
视觉化网页开发工具

SublimeText3 Mac版
神级代码编辑软件(SublimeText3)

PHP和Python各有优势,选择应基于项目需求。1.PHP适合web开发,语法简单,执行效率高。2.Python适用于数据科学和机器学习,语法简洁,库丰富。

PHP是一种广泛应用于服务器端的脚本语言,特别适合web开发。1.PHP可以嵌入HTML,处理HTTP请求和响应,支持多种数据库。2.PHP用于生成动态网页内容,处理表单数据,访问数据库等,具有强大的社区支持和开源资源。3.PHP是解释型语言,执行过程包括词法分析、语法分析、编译和执行。4.PHP可以与MySQL结合用于用户注册系统等高级应用。5.调试PHP时,可使用error_reporting()和var_dump()等函数。6.优化PHP代码可通过缓存机制、优化数据库查询和使用内置函数。7

Java 8引入了Stream API,提供了一种强大且表达力丰富的处理数据集合的方式。然而,使用Stream时,一个常见问题是:如何从forEach操作中中断或返回? 传统循环允许提前中断或返回,但Stream的forEach方法并不直接支持这种方式。本文将解释原因,并探讨在Stream处理系统中实现提前终止的替代方法。 延伸阅读: Java Stream API改进 理解Stream forEach forEach方法是一个终端操作,它对Stream中的每个元素执行一个操作。它的设计意图是处

PHP适合web开发,特别是在快速开发和处理动态内容方面表现出色,但不擅长数据科学和企业级应用。与Python相比,PHP在web开发中更具优势,但在数据科学领域不如Python;与Java相比,PHP在企业级应用中表现较差,但在web开发中更灵活;与JavaScript相比,PHP在后端开发中更简洁,但在前端开发中不如JavaScript。

PHP和Python各有优势,适合不同场景。1.PHP适用于web开发,提供内置web服务器和丰富函数库。2.Python适合数据科学和机器学习,语法简洁且有强大标准库。选择时应根据项目需求决定。

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP成为许多网站首选技术栈的原因包括其易用性、强大社区支持和广泛应用。1)易于学习和使用,适合初学者。2)拥有庞大的开发者社区,资源丰富。3)广泛应用于WordPress、Drupal等平台。4)与Web服务器紧密集成,简化开发部署。

PHP适用于Web开发和内容管理系统,Python适合数据科学、机器学习和自动化脚本。1.PHP在构建快速、可扩展的网站和应用程序方面表现出色,常用于WordPress等CMS。2.Python在数据科学和机器学习领域表现卓越,拥有丰富的库如NumPy和TensorFlow。
