PrintWriter类

PrintWriter是一种过滤流,也是一种处理流,即能对字节流和字符流进行处理。

1.查询API后,我们发现,会有八种构造方法。即:

  • PrintWriter(File file)

    Creates a new PrintWriter, without automatic line flushing, with the specified file.

    PrintWriter(File file, String csn)

    Creates a new PrintWriter, without automatic line flushing, with the specified file and charset.

    PrintWriter(OutputStream out)

    Creates a new PrintWriter, without automatic line flushing, from an existing OutputStream.

    PrintWriter(OutputStream out, boolean autoFlush)

    Creates a new PrintWriter from an existing OutputStream.

    PrintWriter(String fileName)

    Creates a new PrintWriter, without automatic line flushing, with the specified file name.

    PrintWriter(String fileName, String csn)

    Creates a new PrintWriter, without automatic line flushing, with the specified file name and charset.

    PrintWriter(Writer out)

    Creates a new PrintWriter, without automatic line flushing.

    PrintWriter(Writer out, boolean autoFlush)

    Creates a new PrintWriter.

2.实现了三种接口。

1) Closeable接口, 所以它有pw.close()方法来实现对PrintWriter的关闭。

2) Flushable接口,所以它有pw.flush()方法来实现人为的刷新。

3) Appendable接口,所以它有pw.append(char c)方法来向此输出流中追加指定字符,等价于print().

3.方法自寻查询api

4.举例:

 1 import java.io.IOException;
 2 import java.io.PrintWriter;
 3 import java.io.FileWriter;
 4 import java.io.File;
 5
 6 public class PrintWriterDemo {
 7
 8     public static void main(String[] args) {
 9         PrintWriter pw = null;
10         String name = "张松伟";
11         int age = 22;
12         float score = 32.5f;
13         char sex = ‘男‘;
14         try {
15             pw = new PrintWriter(new FileWriter(new File("e:\\file.txt")), true);
16             pw.printf("姓名:%s;年龄:%d;性别:%c;分数:%5.2f;", name, age, sex, score);
17             pw.println();
18             pw.println("多多指教");
19             pw.write(name.toCharArray());
20         } catch (IOException e) {
21             e.printStackTrace();
22         } finally {
23             pw.close();
24         }
25     }
26 }
时间: 2024-08-29 22:01:27

PrintWriter类的相关文章

学习PrintWriter类

转载CSDN zsw2zkl 的分享 Java.io包 1)首先先知道它的八种构造方法,但怎么记住这八种呢?我们都知道PrintWriter是一种过滤流,也叫处理流.也就是能对字节流和字符流进行处理,所以它会有: PrintWriter(OutputStream out)  根据现有的 OutputStream 创建不带自动行刷新的新 PrintWriter. PrintWriter(Writer out)  创建不带自动行刷新的新 PrintWriter. 这两种构造方法.由于PrintWri

IO包中的其他类总结

一.PrintStream和PrintWriter PrintStream 为其他输出流添加了功能,使它们能够方便地打印各种数据值表示形式. PrintStream 打印的所有字符都使用平台的默认字符编码转换为字节. 在需要写入字符而不是写入字节的情况下,应该使用 PrintWriter 类. 1 /** 2 * PrintStream 3 * 1.提供了打印方法,可以对多种类型值进行打印,并保持数据的原有格式 4 * 2.它不抛IOException 5 * 6 * 构造函数 接受三种类型的值

深入研究java.lang.Runtime类

深入研究java.lang.Runtime类 一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用.      一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为.

PrintWriter与Scanner

---恢复内容开始--- PrintWriter类 构造函数的第二个参数可省略 PrintWriter(File file, String csn) // 每次创建新文件,不能追加  csn 编码名 charSet name PrintWriter(OutputStream out, boolean autoFlush) OutputStream outStream = new FileOutputStream("E:/test.txt", true); //追加模式,无则创建,有则追

浅析Java.lang.Runtime类

一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用.      一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为.       当Applet和其他不被信任的代

IO流(三)其他流与File类

1:其他的流 1: DataOutputStream ;多了一些操作基本数据类型的功能 DataInputStream:读取文件. 用DataOutputStream流写进去的数据,就用DataInputStream流来读取. 1 import java.io.DataInputStream; 2 3 import java.io.DataOutputStream; 4 5 import java.io.FileInputStream; 6 7 import java.io.FileOutput

java 打印流PrintWriter基本操作

import java.io.*; public class PrintDemo { public static void main(String[] args) { PrintWriter writer=null; writer=new PrintWriter(System.out); String str=new String("Hello cjc!!!"); writer.print(str); writer.close(); try { writer=new PrintWrit

流之阅读器和书写器(PrintWriter)

PrintWriter类用于取代java 1.0的PrintStream类,它能正确地处理多字节字符集和国际化文本.Sun最初计划废弃PrintStream而支持PrintWriter,但当它意识到这样做会使太多现在的代码失效(尤其是依赖于System.out的代码),就放弃了这种想法.尽管如此,新编写的代码还是应当使用PrintWriter而不是PrintStream. 除了构造函数,PrintWriter类也有与PrintStream几乎相同的方法集.包括: public PrintWrit

深入研究java.lang.Runtime类(转)

一.概述      Runtime类封装了运行时的环境.每个 Java 应用程序都有一个 Runtime 类实例,使应用程序能够与其运行的环境相连接.      一般不能实例化一个Runtime对象,应用程序也不能创建自己的 Runtime 类实例,但可以通过 getRuntime 方法获取当前Runtime运行时对象的引用.      一旦得到了一个当前的Runtime对象的引用,就可以调用Runtime对象的方法去控制Java虚拟机的状态和行为.       当Applet和其他不被信任的代