📜  Java Java类设置 2

📅  最后修改于: 2022-05-13 01:54:43.489000             🧑  作者: Mango

Java Java类设置 2

Java Java类设置 1
更多方法:

  • PrintWriter printf(Locale l, String format, Object... args) :一种使用指定格式字符串和参数将格式化字符串写入此编写器的便捷方法。
    Syntax :public PrintWriter printf(Locale l,
            String format,
            Object... args)
    Parameters:
    l - The locale to apply during formatting. If l is null then no localization is applied.
    format - A format string as described in Format string syntax
    args - Arguments referenced by the format specifiers in the format string.
    Returns: This writer
    Throws:
    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();
      
        }
    }
    
    Output:
    GeeksforGeeks
  • PrintWriter printf(String format, Object... args) :使用指定的格式字符串和参数将格式化字符串写入此编写器的便捷方法。
    Syntax :public PrintWriter printf(String format,
            Object... args)
    Parameters:
    format - A format string as described in Format string syntax
    args - Arguments referenced by the format specifiers in the format string.
    Returns: This writer
    Throws:
    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();
      
        }
    }
    
    Output:
    GeeksforGeeks
  • void println():通过写入行分隔符字符串来终止当前行。
    Syntax :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();
      
        }
    }
    
    Output:
    GeeksforGeeks
  • void println(boolean x):打印一个布尔值,然后终止该行。
    Syntax :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();
      
        }
    }
    
    Output:
    true
    New Line
  • void println(char x):打印一个字符,然后终止该行。
    Syntax :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();
      
        }
    }
    
    Output:
    a
    b
  • void println(char[] x):打印一个字符数组,然后终止该行。
    Syntax :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();
      
      
        }
    }
    
    Output:
    abc
    
  • void println(double x):打印一个双精度然后终止该行。
    Syntax :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):打印一个浮点数,然后终止该行。
    Syntax :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):打印一个整数,然后终止该行。
    Syntax :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):打印一个长整数,然后终止该行。
    Syntax :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) :打印一个对象,然后终止该行。
    Syntax :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) :打印一个字符串,然后终止该行。
    Syntax :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。
    Syntax :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) :从偏移量 off 开始的指定 char 数组写入 len char 到此流。
    Syntax :public void write(char [] buf,
            int off,
            int len)
    Overrides:
    write in class FilterOutputStream
    Parameters:
    buf - A char array
    off - Offset from which to start taking char 
    len - Number of char to write
    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) :将指定的字符写入此流。
    Syntax :public void write(int b)
    Overrides:
    write in class Writer
    Parameters:
     b - The char to be written
    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) :写入一个字符串。
    Syntax :public void write(String s)
    Parameters:
    s - String to be written
    
    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) :写入字符串的一部分。
    Syntax :public void write(String s,
             int off,
             int len)
    Parameters:
    s - A String
    off - Offset from which to start writing characters
    len - Number of characters to write
    
    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();
          }
       }
    }
    
    HelloWorld