Java中的Java.io.PrintWriter类| 2

作者: Arvin Chen 分类: Java 来源: Break易站(www.breakyizhan.com)
  •   Java I/O总结

    Java中的Java.io.PrintWriter类| 2

    更多方法:

    • PrintWriter printf(Locale l, String format, Object… args) :一种使用指定的格式字符串和参数将格式化字符串写入该写入程序的简便方法。
      语法: public PrintWriter printf(Locale l,
              String format,
              Object... args)
      参数:
       l  - 格式化时应用的区域设置。如果l为null,则不应用本地化。
      format - 格式字符串语法中描述的格式字符串
      args  - 格式字符串中格式说明符引用的参数。
      返回:this writer
       抛出:
      IllegalFormatException
      nullpointerException
      import java.io.*;
      import java.util.Locale;
      //Java program to demonstrate printf method
      public class PrintWriterDemo {
          public static void main(String[] args) {
              String s = "for";
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // printf this string
              pr.printf(Locale.CANADA, "Geeks%sGeeks", s);
      
              // flush the stream
              pr.flush();
      
          }
      }
      输出: 
      GeeksforGeeks
    • PrintWriter printf(String format,Object ... args):一种使用指定格式字符串和参数将格式化字符串写入该写入器的便捷方法。
      语法: public PrintWriter printf(String format,
              Object... args)
      参数:
      format - 格式字符串语法中描述的格式字符串
      args  - 格式字符串中格式说明符引用的参数。
      返回:this writer
       抛出:
      IllegalFormatException
      NullPointerException
      import java.io.*;
      //Java program to demonstrate printf(String format, Object... args) method
      public class PrintWriterDemo {
          public static void main(String[] args) {
              String s = "for";
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // printf this string
              pr.printf("Geeks%sGeeks", s);
      
              // flush the stream
              pr.flush();
      
          }
      }
      输出: 
      GeeksforGeeks
    • void println():通过写入行分隔符字符串来终止当前行。
      语法: public void println()
        import java.io.*;
      //Java program to demonstrate println() method
      public class PrintWriterDemo {
          public static void main(String[] args) {
              String s = "GeeksforGeeks";
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // printf this string
              pr.printf("%s", s);
      
              // flush the stream
              pr.flush();
      
          }
      }
      输出: 
      GeeksforGeeks
    • void println(boolean x):打印布尔值,然后终止该行。
      语法: public void println(boolean x)
      import java.io.*;
      //Java program to demonstrate println(boolean) method
      public class PrintWriterDemo {
          public static void main(String[] args) {
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print a boolean and change line
              pr.println(true);
              pr.print("New Line");
      
              // flush the stream
              pr.flush();
      
          }
      }
      输出:
      true
      New Line
    • void println(char x):打印一个字符,然后终止该行。
      语法: public void println(char x)
       import java.io.*;
      //Java program to demonstrate println(char x) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              char c = 'a';
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print a char and change line
              pr.println(c);
              pr.println('b');
      
              // flush the stream
              pr.flush();
      
          }
      }
      输出:
      a
      b
    • void println(char [] x):打印一组字符,然后终止该行。
      语法: public void println(char [] x)
      import java.io.*;
      //Java program to demonstrate println(char[] x) method
      public class PrintWriterDemo {
          public static void main(String[] args) {
              char[] c = {'a', 'b', 'c'};
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print an array and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
      
          }
      }
      
      输出:
      ABC
      
    • void println(double x):打印一个double,然后终止该行。
      语法: public void println(double x)
      import java.io.*;
      //Java program to demonstrate println(double x) method
      public class PrintWriterDemo {
          public static void main(String[] args) {
              double c = 176.348;
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print a double and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
          }
      }

      输出:

      176.348
    • void println(float x):打印一个浮点数然后终止这一行。
      语法: public void println(float x)
      //Java program to demonstrate println(float x) method
       import java.io.*;
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              float c = 5.76348f;
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print a float and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
      
          }
      }

      输出:

      5.76348
    • void println(int x):打印一个整数,然后终止该行。
      语法: public void println(boolean x)
      import java.io.*;
      //Java program to demonstrate println(int x) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              int c = 15;
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print an integer and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
      
          }
      }

      输出:

      15
      
    • void println(long x):打印一个长整数,然后终止该行。
      语法: public void println(long x)
              import java.io.*;
      //Java program to demonstrate println(long x) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              long c = 1252344125l;
              try {
                  // create PrintWriter object
                  PrintWriter pr= new PrintWriter(System.out);
      
                  // print a long and change line
                  pr.println(c);
      
                  // flush the stream
                  pr.flush();
      
              } catch (Exception ex) {
                  ex.printStackTrace();
              }
          }
      }

      输出:

      1252344125
    • void println(Object x):打印一个Object,然后终止该行。
      语法: public void println(Object x)
              import java.io.*;
      //Java program to demonstrate println(Object x) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              Object c = 10;
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print an object and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
          }
      }

      输出:

      10
    • void println(String x): 打印一个字符串,然后终止该行。
      语法: public void println(boolean x)
       import java.io.*;
      //Java program to demonstrate println(String x) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              String c = "GeeksforGeeks";
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // print a string and change line
              pr.println(c);
      
              // flush the stream
              pr.flush();
      
          }
      }

      输出:

      GeeksforGeeks
    • protected void setError(): 将流的错误状态设置为true。
      语法: public void println(String x)
      import java.io.*;
      //Java program to demonstrate setError() method
      public class PrintWriterDemo extends PrintWriter {
      
          public PrintWriterDemo(OutputStream out) {
              super(out);
          }
      
          public static void main(String[] args) {
              char c[] = {70, 71, 72, 73, 74, 75, 76};
      
              // create PrintWriter object
              PrintWriterDemo pr= new PrintWriterDemo(System.out);
      
              // write char 1-3
              pr.write(c, 1, 3);
      
              // flush the stream
              pr.flush();
      
              // set an internal error
              pr.setError();
          }
      }

      输出:

      GHI
      
    • void write(char [] buf,int off,int len): 将指定字符数组中的len char从偏移量开始写入此流。
      语法: public void write(char [] buf,
              int off,
              int len)
      覆盖:
      在类FilterOutputStream中写入
      参数:
      buf  - 一个字符数组
      off  - 开始采用char的偏移量 
      len  - 要写入的字符数
      import java.io.*;
      //Java program to demonstrate write() method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              char c[] = {70, 71, 72, 73, 74, 75, 76};
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // write char 1-3
              pr.write(c, 1, 3);
      
              // flush the stream
              pr.flush();
      
          }
      }

      输出:

      GHI
    • void write(int b): 将指定的char写入此流。
      语法: public void write(int b)
      覆盖:
      在类Writer中写入
      参数:
       b  - 要写入的字符
       import java.io.*;
      //Java program to demonstrate write(int b) method
      public class PrintWriterDemo {
      
          public static void main(String[] args) {
              int c = 70;
      
              // create PrintWriter object
              PrintWriter pr= new PrintWriter(System.out);
      
              // write char c which is character F in ASCII
              pr.write(c);
      
              // flush the stream
              pr.flush();
      
          }
      }

      输出:

      F
    • void write(String s):写入一个字符串。
      语法: public void write(String s)
      参数:
      s  - 要写入的字符串
      
      import java.io.*;
      public class PrintWriterDemo {
      
         public static void main(String[] args) {
            String s = "Hello";
            try {
      
               // create a new writer
               PrintWriter pw = new PrintWriter(System.out);
      
               // write strings
               pw.write(s);
               pw.write(" World");
      
               // flush the writer
               pw.flush();
      
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         }
      }
      
      Hello World
    • void write(String s,int off,int len):写入一部分字符串。
      语法: public void write(String s,
               int off,
               int len)
      参数:
      s  - 一个字符串
      off  - 开始写字符的偏移量
      len  - 要写入的字符数
      
      import java.io.*;
      
      public class PrintWriterDemo {
      
         public static void main(String[] args) {
            String s = "Hello World";
            try {
      
               // create a new writer
               PrintWriter pw = new PrintWriter(System.out);
      
               // write substrings
               pw.write(s, 0, 5);
               pw.write(s, 6, 5);
      
               // flush the writer
               pw.flush();
      
            } catch (Exception ex) {
               ex.printStackTrace();
            }
         }
      }
      
      Hello World
  •   Java I/O总结
  •   本文标题:Java中的Java.io.PrintWriter类| 2 - Break易站
    转载请保留页面地址:https://www.breakyizhan.com/java/4357.html
      微信返利机器人
      免费:淘宝,京东,拼多多优惠券
      腾讯,爱奇艺,优酷的VIP视频免费解析,免费看
      即刻扫描二维码,添加微信机器人!

    发表笔记

    电子邮件地址不会被公开。 必填项已用*标注