`
loveelwin
  • 浏览: 3605 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

PrintWriter vs FileWriter in Java

 
阅读更多

FileWriter and PrintWriter are the character representation of IO. That means it can be used to write characters. Internally FileWriter would use the default character set of the underlying OS and convert the characters to bytes and write it to the disk.


PrintWriter & FileWriter .
Similarities
a) Both extend from Writer.
b) Both are character representation classes, that means they work with characters and convert them to bytes using default charset.
Differences
a) FileWriter throws IOException in case of any IO failure, this is a checked exception.
b) None of the PrintWriter methods throws IOException , instead they set a boolean flag which can be obtained using checkError().
c) PrintWrite r invokes flush after every byte of data is written , automatically. In case of FileWriter , invoker as to take care of invoking flush.

Difference between PrintStream and OutputStream : Similar to above explanation, just replace character with byte.

Method of PrintWriter class:

a)PrintWriter(Writer out);

b)PrintWriter(Writer out,boolean autoFlush);//if autoFlush is true,then method println will flush the buffer;

c)PrintWriter(OutputStream out);

d)PrintWriter(OutputStream out,boolean autoFlush);

e)PrintWriter(String filename);

f)PrintWriter(File file);     //the same effective as Print(Writer filename);

g)print();

h)println();  //after print,add a line break in this line.("\r\n" for Windows and "\n" for Linux);

i) checkError();

 

Method of FileWriter class:

a)close();

b)flush();

c)write();

d)FileWriter(String fileName);

e)FileWriter(File file, boolean append);   //if argument "append" is true,then bytes will be written to the end of the file rather than the beginning

f)FileWriter(File file);

g)FileWriter(File file,boolean append);

h)FileWriter(FileDescriptor fd);

 

 

associate:

FileWriter out = new FileWriter("test.txt");

equals:

PrintWriter out = new PrintWriter(new FileWriter("test.txt"));

 

 

分享到:
评论

相关推荐

    Java中文件的输入与输出实例.pdf

    PrintWriter fileOut = new PrintWriter(new FileWriter("E:\\JavaExample\\7-5\\bbb.txt")); // 写入到文件的第一行 fileOut.println("下面是文件 bbb.txt 中的内容"); while ((s = fileIn.readLine()) != ...

    Java中文件的输入与输出.pdf

    PrintWriter fileOut = new PrintWriter(new FileWriter("E:\\JavaExample\\7-5\\bbb.txt")); // 写入到文件的第一行 fileOut.println("下面是文件bbb.txt中的内容"); while ((s = fileIn.readLine()) != null...

    java 将由键盘中录入的信息保存到文件中

    总结来说,Java通过`java.io`包中的类实现了键盘输入数据到文件的保存,主要涉及到`Scanner`和`FileWriter`类的使用。了解并熟练掌握这些基本操作对于Java开发者来说是非常重要的。在实际应用中,可以根据需求进行...

    java I/0实现

    PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out"))); while ((s = s2.split("\n")) != null) out1.println(s); out1.close(); ``` 3. 从字符串中逐个读取字符: ```java ...

    java中文件操作大全

    使用`java.io.PrintWriter`或`java.io.BufferedWriter`可以向文件写入内容。下面的示例展示了如何使用PrintWriter写入文本。 ```java import java.io.PrintWriter; import java.io.FileWriter; import java.io....

    javaIO与文件(ppt文档).ppt

    在Java中,文本文件的读写可以使用PrintWriter、FileWriter、BufferedWriter等类,它们通常会根据系统默认编码或指定编码来处理字符。二进制文件则通常使用DataInputStream和DataOutputStream来读写原始字节。 6. *...

    关于io的一些代码

    import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; public class copyFile { public static void copyFile(String srcFile, String tagFile) { BufferedReader in=...

    java简单的员工管理系统

    try (PrintWriter writer = new PrintWriter(new FileWriter(filePath, true))) { writer.println(employee.toString()); } catch (IOException e) { System.out.println("保存失败:" + e.getMessage()); } }...

    java 产生猜随机数字

    PrintWriter writer = new PrintWriter(new FileWriter("随机数.txt", true)); writer.println("用户猜测:" + userGuess); if (userGuess == randomNumber) { writer.println("程序生成的随机数是:" + random...

    JAVA 应用编程150例

    5. **输入输出流**:Java I/O系统包括读写文件、网络通信等,例如FileReader、FileWriter、Scanner、PrintWriter等类的使用。理解如何进行文件操作,是许多实际应用的基础。 6. **多线程**:Java提供了内置的多线程...

    Java IO 与 装饰模式

    Java IO体系包含多个层次,如InputStream/OutputStream(字节流基类)、Reader/Writer(字符流基类),以及它们的子类,如FileInputStream/FileOutputStream、FileReader/FileWriter等。 2. 装饰模式的应用: 在...

    java读写文件的集中方式

    PrintWriter writer = new PrintWriter(new FileWriter("output.txt"))) { String line; while ((line = reader.readLine()) != null) { writer.println(line); } } catch (IOException e) { e....

    java 常见问题及答案

    对于文件的输入输出,可以使用`BufferedReader`配合`FileReader`进行读取,用`PrintWriter`配合`FileWriter`进行写入。对于非ASCII字符(如中文),直接使用`System.in`可能导致编码问题,因此推荐使用`...

    Java 输入与输出流操作

    - **FileReader** 和 **FileWriter** 专用于文件的字符读写。 - **BufferedReader** 和 **BufferedWriter** 提供字符缓冲功能,提升性能。 - **PrintWriter** 可以方便地进行格式化输出。 4. **转换流**: - **...

    java写一个日志程序

    在这里,`InputStreamReader`与`System.in`(标准输入流)配合,使我们能够读取键盘输入的字符。 - `FileWriter`:用于向文件写入文本数据。它可以创建或追加到已存在的文件。 - `PrintWriter`:一个强大的文本...

    Java 使用流保存数据 简单的人员信息管理系统

    为了实现用户界面,可以使用Java的控制台输入输出(如`System.in`和`System.out.println()`)或图形用户界面(GUI,如Swing或JavaFX)进行交互。控制台方式简洁,适合初学者;GUI方式更直观,但需要更多的代码和对...

    Java输入输出流

    Java预定义了三个标准流:`System.in`(标准输入)、`System.out`(标准输出)和`System.err`(错误输出)。它们通常与键盘、显示器和错误日志关联。 ### 打包与解包流(GZIPOutputStream, GZIPInputStream) 用于...

    java_IO操作_(读写、追加、删除、移动、复制、修改)

    例如,使用`FileOutputStream`按字节写入,`PrintWriter`按字符写入,或`BufferedWriter`按行写入。 - **追加模式**:在现有文件末尾添加内容,而不是覆盖原有内容,需要在打开文件流时设置`"a"`模式。 - **覆盖...

    java测试题以及题库

    writer = new PrintWriter(new BufferedWriter(new FileWriter("out"))); ``` 154题:此题考察如何从Console对象读取一行文本。`java.io.Console`类提供了与控制台交互的方法。A选项`String s = c.readLine();` 是...

    java is print.zip_IS_broaddk3

    8. **IO流**:除了标准输出,Java还提供了丰富的I/O流类库,允许我们向文件、网络、内存等其他目标进行输出,`FileWriter`、`PrintWriter`等类就是为此设计的。 9. **日志记录**:在实际开发中,通常会用到日志框架...

Global site tag (gtag.js) - Google Analytics