JavaSE(三十一)-IO流--PrintWriter类
  eZw8kcl3fQWu 2023年11月19日 11 0



文章目录

  • 1.常用的构造方法
  • 2.常用方法
  • 3.PrintWriter和BufferedWriter的使用区别



java.io.PrintWriter是java中很常见的一个类,该类可用来创建一个文件并向文本文件写入数据。可以理解为java中的文件输出,java中的文件输入则是java.io.File。

1.常用的构造方法

1)构造方法参数为String类型的对象,值应为文件全路径。若文件不存在,则会先创建文件。

public PrintWriter(String fileName) throws FileNotFoundException {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName))),
          false);
 }

代码示例

举例:若aaa.txt不存在,则先创建aaa.txt,再向aaa.txt中写入“Hello World”。若aaa.txt存在,则直接向aaa.txt中写入“Hello World”(每次写入的内容都会覆盖原来的内容)。

public static void main(String[] args)  {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter("aaa.txt");
            pw.print("Hello World");
            pw.print("Hello World");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }finally {
            pw.close();
        }
    }

2)构造方法参数为File类型的对象,值应为File。

public PrintWriter(File file) throws FileNotFoundException {
     this(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file))),
          false);
 }
public static void main(String[] args) {
        File file = new File("ccc.txt");
        System.out.println(file.exists());//输出为false,因为本地没有ccc.txt
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(file);//先创建ccc.txt(若存在,则不会创建)
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        pw.print("Hello World");
        pw.print("Hello World");
        pw.print("Hello World");
        pw.close();
    }

3)构造方法参数为FileWriter

public PrintWriter (Writer out) {
       this(out, false);
   }

示例:

public static void main(String[] args) {
    File file = new File("ccc.txt");
    System.out.println(file.exists());//输出为false,因为本地没有ccc.txt
    PrintWriter pw = null;
    try {
        pw = new PrintWriter(new FileWriter(file,true));//先创建ccc.txt(若存在,则不会创建)
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    pw.println("Hello World");
    pw.println("Hello World");
    pw.println("Hello World");
    pw.close();
}

2.常用方法

(1)print(String str):向文件写入一个字符串。
(2)print(char[] ch):向文件写入一个字符数组。
(3)print(char c):向文件写入一个字符。
(4)print(int i):向文件写入一个int型值。
(5)print(long l):向文件写入一个long型值。
(6)print(float f):向文件写入一个float型值。
(7)print(double d):向文件写入一个double型值。
(8)print(boolean b):向文件写入一个boolean型值。

3.PrintWriter和BufferedWriter的使用区别

BufferedWriter:将文本写入字符输出流,缓冲各个字符从而提供单个字符,数组和字符串的高效写入。通过write()方法可以将获取到的字符输出,然后通过newLine()进行换行操作。BufferedWriter中的字符流必须通过调用flush方法才能将其刷出去。并且BufferedWriter只能对字符流进行操作。如果要对字节流操作,则使用BufferedInputStream。

PrintWriter:向文本输出流打印对象的格式化表示形式(Prints formatted representations of objects to a text-output stream)。PrintWriter相对于BufferedWriter的好处在于,如果PrintWriter开启了自动刷新,那么当PrintWriter调用println,输出流中的数据就会自动刷新出去。PrintWriter不但能接收字符流,也能接收字节流。

Socket编程中,尽量用PrintWriter取代BufferedWriter,下面是PrintWriter的优点:

1. PrintWriter的print、println方法可以接受任意类型的参数,而BufferedWriter的write方法只能接受字符、字符数组和字符串;
2. PrintWriter的println方法自动添加换行,BufferedWriter需要显示调用newLine方法;
3. PrintWriter的方法不会抛异常,BufferedWriter方法需要处理异常;
4. PrintWriter构造方法可指定参数,实现自动刷新缓存(autoflush);
5. PrintWriter的构造方法更广。

小结:
在使用BufferedReader中的readLine方法接收BufferedWriter中的字符流时,由于readLine是在读取到换行符的时候才将整行字符返回,所以BufferedWriter方法在录入一段字符后要使用newLine方法进行一次换行操作,然后再把字符流刷出去。而PrintWriter由于可以开启自动刷新,并且其中的println方法自带换行操作。所以代码实现起来要比BufferedWriter简单一些。


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月19日 0

暂无评论

推荐阅读
  E929ZvlRxyUs   2023年12月23日   18   0   0 前端url前端URL
eZw8kcl3fQWu