使用FileWriter和FileReader在Java中处理文件

Java FileWriter和FileReader类用于从文本文件(它们是 字符流 课程)。建议这样做 如果必须读写任何文本信息,则使用FileInputStream和FileOutputStream类,因为它们是字节流类。

null

字符输出流 FileWriter可用于创建文件并将字符写入其中。

  • 此类继承自OutputStream类。
  • 此类的构造函数假定默认字符编码和默认字节缓冲区大小是可接受的。要自己指定这些值,请在FileOutputStream上构造OutputStreamWriter。
  • FileWriter用于编写字符流。若要写入原始字节流,请考虑使用FielOutPoSt流。
  • 如果输出文件不存在,FileWriter将创建输出文件。

施工人员:

  • 文件编写器(文件文件)– 在给定文件对象的情况下构造FileWriter对象。
  • FileWriter(文件,布尔附加)– 在给定文件对象的情况下构造FileWriter对象。
  • 文件编写器(文件描述符fd)—— 构造与文件描述符关联的FileWriter对象。
  • FileWriter(字符串文件名)– 在给定文件名的情况下构造FileWriter对象。
  • FileWriter(字符串文件名,布尔附加)– 构造一个FileWriter对象,给定一个文件名,该文件名带有一个布尔值,指示是否追加写入的数据。

方法:

  • 公共无效写入(int c)引发IOException—— 只写一个字符。
  • public void write(char[]stir)引发IOException—— 写入一个字符数组。
  • public void write(字符串str)引发IOException– 写一个字符串。
  • public void write(字符串str, int off, int len)抛出IOException—— 写入字符串的一部分。这里off是开始写入字符的偏移量,len是要写入的字符数。
  • public void flush()引发异常 冲走小溪
  • public void close()引发异常 首先刷新流,然后关闭写入程序。

读取和写入是逐字符进行的,这会增加I/O操作的数量,并影响系统的性能。 缓冲写入程序 可以与FileWriter一起使用,以提高执行速度。 下面的程序描述了如何使用FileWriter创建文本文件

JAVA

// Creating a text File using FileWriter
import java.io.FileWriter;
import java.io.IOException;
class CreateFile
{
public static void main(String[] args) throws IOException
{
// Accept a string
String str = "File Handling in Java using "+
" FileWriter and FileReader";
// attach a file to FileWriter
FileWriter fw= new FileWriter("output.txt");
// read character wise from string and write
// into FileWriter
for ( int i = 0 ; i < str.length(); i++)
fw.write(str.charAt(i));
System.out.println("Writing successful");
//close the file
fw.close();
}
}


字符流

FileReader用于从“文本”文件中读取字符形式的数据。

  • 此类继承自InputStreamReader类。
  • 此类的构造函数假定默认字符编码和默认字节缓冲区大小是合适的。要自己指定这些值,请在FileInputStream上构造一个InputStreamReader。
  • FileReader用于读取字符流。若要读取原始字节流,请考虑使用FieldPixSt流。

施工人员:

  • 文件阅读器(文件) 根据要读取的文件创建文件读取器
  • 文件阅读器(文件描述符fd)—— 根据要读取的FileDescriptor,创建一个新的FileReader
  • 文件阅读器(字符串文件名)– 根据要读取的文件的名称,创建一个新的文件读取器

方法:

  • public int read()引发IOException– 读一个字符。此方法将一直阻止,直到字符可用、发生I/O错误或到达流的结尾。
  • 公共整数读取(char[]cbuff)引发IOException—— 将字符读入数组。此方法将一直阻止,直到某些输入可用、发生I/O错误或到达流的末尾。
  • 公共摘要int read(char[]buff,int off,int len)抛出IOException—— 将字符读入数组的一部分。此方法将一直阻止,直到某些输入可用、发生I/O错误或到达流的末尾。 参数: cbuf–目标缓冲区 off–开始存储字符的偏移量 len–要读取的最大字符数
  • public void close()引发异常 关闭阅读器。
  • 公共长跳转(长n)抛出IOException—— 跳过角色。此方法将一直阻止,直到某些字符可用、发生I/O错误或到达流的结尾。 参数: n–要跳过的字符数

下面的程序描述了如何使用FileReader读取“文本”文件

JAVA

// Reading data from a file using FileReader
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
class ReadFile
{
public static void main(String[] args) throws IOException
{
// variable declaration
int ch;
// check if File exists or not
FileReader fr= null ;
try
{
fr = new FileReader("text");
}
catch (FileNotFoundException fe)
{
System.out.println("File not found");
}
// read from FileReader till the end of file
while ((ch=fr.read())!=- 1 )
System.out.print(( char )ch);
// close the file
fr.close();
}
}


本文由 尼森特·夏尔马 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享