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 <p><strong>输出:</strong></p> <p><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/000/172500344564251.png?x-oss-process=image/resize,p_40" class="lazy" alt="Java 中的文件写入器" ></p> <h3 id="结论">结论</h3> <p>在本教程中,我们了解了 FileWriter 的概念,如 FileWriter 的定义、如何声明 FileWriter、FileWriter 中的构造函数以及编程示例来演示 FileWriter 类的创建,使用 FileWriter 类创建文本文件,使用FileWriter 类将文本追加到由内容组成的现有文件中,使用 FileWriter 类逐行写入新文件,即一行一行地添加内容。</p> <h3 id="推荐文章">推荐文章</h3> <p>这是 Java 中 FileWriter 的指南。这里我们讨论如何声明 FileWriter、FileWriter 中的构造函数及其方法以及代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –</p> <ol> <li>Java 中的布局</li> <li>Java 编译器</li> <li>Java 中的合并排序</li> <li>Java 并行流</li> </ol></string></string></string>
以上是Java 中的文件写入器的详细内容。更多信息请关注PHP中文网其他相关文章!

JVM'SperformanceIsCompetitiveWithOtherRuntimes,operingabalanceOfspeed,安全性和生产性。1)JVMUSESJITCOMPILATIONFORDYNAMICOPTIMIZAIZATIONS.2)c提供NativePernativePerformanceButlanceButlactsjvm'ssafetyFeatures.3)

JavaachievesPlatFormIndependencEthroughTheJavavIrtualMachine(JVM),允许CodeTorunonAnyPlatFormWithAjvm.1)codeisscompiledIntobytecode,notmachine-specificodificcode.2)bytecodeisisteredbytheybytheybytheybythejvm,enablingcross-platerssectectectectectross-eenablingcrossectectectectectection.2)

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java实现“一次编写,到处运行”通过编译成字节码并在Java虚拟机(JVM)上运行。1)编写Java代码并编译成字节码。2)字节码在任何安装了JVM的平台上运行。3)使用Java原生接口(JNI)处理平台特定功能。尽管存在挑战,如JVM一致性和平台特定库的使用,但WORA大大提高了开发效率和部署灵活性。

JavaachievesPlatFormIndependencethroughTheJavavIrtualMachine(JVM),允许Codetorunondifferentoperatingsystemsswithoutmodification.thejvmcompilesjavacodeintoplatform-interploplatform-interpectentbybyteentbytybyteentbybytecode,whatittheninternterninterpretsandectectececutesoneonthepecificos,atrafficteyos,Afferctinginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginginging

JavaispoperfulduetoitsplatFormitiondence,对象与偏见,RichstandardLibrary,PerformanceCapabilities和StrongsecurityFeatures.1)Platform-dimplighandependectionceallowsenceallowsenceallowsenceallowsencationSapplicationStornanyDevicesupportingJava.2)

Java的顶级功能包括:1)面向对象编程,支持多态性,提升代码的灵活性和可维护性;2)异常处理机制,通过try-catch-finally块提高代码的鲁棒性;3)垃圾回收,简化内存管理;4)泛型,增强类型安全性;5)ambda表达式和函数式编程,使代码更简洁和表达性强;6)丰富的标准库,提供优化过的数据结构和算法。


热AI工具

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

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

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

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

热门文章

热工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

SublimeText3 Linux新版
SublimeText3 Linux最新版

VSCode Windows 64位 下载
微软推出的免费、功能强大的一款IDE编辑器

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