⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 formatwriter.java

📁 Java Classic Examples是我买的两本书:《JAVA经典实例》和《java入门经典源代码》里边附送光盘里带的源码
💻 JAVA
字号:
import java.io.*;

public class FormatWriter extends PrintWriter
{
  private int width = 10;      // Field width required for output

  // Basic constructor for a default field width
  public FormatWriter(Writer output)
  {
    super(output);                // Call PrintStream constructor
  }

  // Constructor with a specified field width
  public FormatWriter(Writer output, int width)
  {
    super(output);                // Call PrintStream constructor
    this.width = width;           // Store the field width
  }

  // Constructor with autoflush option
  public FormatWriter(Writer output, boolean autoflush)
  {
    super(output, autoflush);     // Call PrintStream constructor
  }

  // Constructor with a specified field width and autoflush option 
  public FormatWriter(Writer output, boolean autoflush, int width)
  {
    super(output, autoflush);     // Call PrintStream constructor
    this.width = width;           // Store the field width
  }

  // Helper method for output
  private void output(String str)
  {
    int blanks = width - str.length();    // Number of blanks needed

    // If the length is less than the width, add blanks to the start
    for(int i = 0; i < blanks; i++)
      super.print(' ');                   // Output a space
    super.print(str);                     // Use base method for output
  }

  // Output type long formatted in a given width
  public void print(long value)
  {
    output(String.valueOf(value));         // Pad to width and output
  }

  // Output type double formatted in a given width
  public void print(double value)
  {
    output(String.valueOf(value));         // Pad to width and output
  }

  // Output type long formatted in a given width plus a newline
  public void println(long value)
  {
    this.print(value);                     // Call current method
    super.println();                       // Call base method
  }

  // Output type double formatted in a given width plus a newline
  public void println(double value)
  {
    this.print(value);                    // Call current method
    super.println();                      // Call base method
  }

  public void setWidth(int width)
  {
    this.width = width>0 ? width : 1;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -