首页 > Java > java教程 > 正文

Java 中的文件写入器

PHPz
发布: 2024-08-30 15:37:17
原创
417 人浏览过

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();
}
}
登录后复制

输出:

Java 中的文件写入器

示例#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();
}
}
}
登录后复制

Java 中的文件写入器

示例 #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);
}
}
}
登录后复制

输出:

Java 中的文件写入器

示例#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) {
}
}
}
}
登录后复制

输出:

Java 中的文件写入器

结论

在本教程中,我们了解了 FileWriter 的概念,如 FileWriter 的定义、如何声明 FileWriter、FileWriter 中的构造函数以及编程示例来演示 FileWriter 类的创建,使用 FileWriter 类创建文本文件,使用FileWriter 类将文本追加到由内容组成的现有文件中,使用 FileWriter 类逐行写入新文件,即一行一行地添加内容。

推荐文章

这是 Java 中 FileWriter 的指南。这里我们讨论如何声明 FileWriter、FileWriter 中的构造函数及其方法以及代码实现。您还可以浏览我们其他推荐的文章以了解更多信息 –

  1. Java 中的布局
  2. Java 编译器
  3. Java 中的合并排序
  4. Java 并行流

以上是Java 中的文件写入器的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:php
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!