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"));
分享到:
相关推荐
PrintWriter fileOut = new PrintWriter(new FileWriter("E:\\JavaExample\\7-5\\bbb.txt")); // 写入到文件的第一行 fileOut.println("下面是文件 bbb.txt 中的内容"); while ((s = fileIn.readLine()) != ...
PrintWriter fileOut = new PrintWriter(new FileWriter("E:\\JavaExample\\7-5\\bbb.txt")); // 写入到文件的第一行 fileOut.println("下面是文件bbb.txt中的内容"); while ((s = fileIn.readLine()) != null...
总结来说,Java通过`java.io`包中的类实现了键盘输入数据到文件的保存,主要涉及到`Scanner`和`FileWriter`类的使用。了解并熟练掌握这些基本操作对于Java开发者来说是非常重要的。在实际应用中,可以根据需求进行...
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter("out"))); ``` **解析**: 1. **读取流**: - 使用`FileReader`创建一个文件读取器。 - 将`FileReader`实例传递给`BufferedReader`的...
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("F:\\nepalon\\TestIO.out"))); int lineCount = 1; while ((s = in4.readLine()) != null) out1.println(lineCount++ + "" + s); out1....
PrintWriter writer = new PrintWriter(new FileWriter("myfile1.txt")); String line; System.out.println("Enter student information (type 'bye' to finish):"); while (!(line = reader.readLine())....
- 写入完成后,需要先关闭`PrintWriter`,再关闭`FileWriter`。 ```java out.close(); fw.close(); ``` #### 三、总结与比较 1. **适用范围**: - `InputStream`/`OutputStream`适用于处理任何类型的文件...
- 在Java中,标准输入输出是通过`java.lang.System`类实现的,其中`System.in`用于获取标准输入,而`System.out`用于输出数据。 - `System.in`是一个`InputStream`对象,可以用来读取用户从键盘输入的数据。 #### ...
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out"))); while ((s = s2.split("\n")) != null) out1.println(s); out1.close(); ``` 3. 从字符串中逐个读取字符: ```java ...
使用`java.io.PrintWriter`或`java.io.BufferedWriter`可以向文件写入内容。下面的示例展示了如何使用PrintWriter写入文本。 ```java import java.io.PrintWriter; import java.io.FileWriter; import java.io....
在Java中,文本文件的读写可以使用PrintWriter、FileWriter、BufferedWriter等类,它们通常会根据系统默认编码或指定编码来处理字符。二进制文件则通常使用DataInputStream和DataOutputStream来读写原始字节。 6. *...
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=...
try (PrintWriter writer = new PrintWriter(new FileWriter(filePath, true))) { writer.println(employee.toString()); } catch (IOException e) { System.out.println("保存失败:" + e.getMessage()); } }...
PrintWriter writer = new PrintWriter(new FileWriter("随机数.txt", true)); writer.println("用户猜测:" + userGuess); if (userGuess == randomNumber) { writer.println("程序生成的随机数是:" + random...
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("IODemo.out"))); int lineCount = 1; while ((s = in2.readLine()) != null) { out1.println(lineCount + ": " + s); lineCount++; }...
5. **输入输出流**:Java I/O系统包括读写文件、网络通信等,例如FileReader、FileWriter、Scanner、PrintWriter等类的使用。理解如何进行文件操作,是许多实际应用的基础。 6. **多线程**:Java提供了内置的多线程...
PrintWriter out1 = new PrintWriter(new BufferedWriter(new FileWriter("F:\\nepalon\\TestIO.out"))); int lineCount = 1; while ((s = in4.readLine()) != null) out1.println(lineCount++ + "" + s); out1....
- 数据流是一系列连续的数据单元,可以是来自键盘的标准输入(System.in),标准输出(System.out)或标准错误输出(System.err)。 - 在Unix系统中,用Control-D表示数据流的结束,在Windows系统中,用Control-Z...
Java IO体系包含多个层次,如InputStream/OutputStream(字节流基类)、Reader/Writer(字符流基类),以及它们的子类,如FileInputStream/FileOutputStream、FileReader/FileWriter等。 2. 装饰模式的应用: 在...
### JAVA基础阶段总结 ...- **字符输出流**:如 FileWriter、PrintWriter 等。 通过这些基础知识的学习,开发者可以更好地理解和掌握 JAVA 编程的核心概念和技术要点,为进一步深入学习打下坚实的基础。