举报投诉联系我们 手机版 热门标签 名动网
您的位置:名动网 > java打印模版 Java 打印样式格式

java打印模版 Java 打印样式格式

2023-04-19 06:20 Java教程

java打印模版 Java 打印样式格式

java打印模版

Java打印模版是一种用于打印文档的模板,它可以帮助开发人员快速创建出色的打印文档。它可以帮助开发人员在不需要重复编写代码的情况下,快速创建出色的打印文档。

Java打印模版有很多优点,其中最重要的是它能够节省时间和精力。使用Java打印模版,开发人员不需要重复编写代码,而是可以直接使用已有的模板来创建出色的打印文档。此外,Java打印模版还能够帮助开发人员避免出错,因为它们已经包含了正确的格式和语法。

public class PrintTemplate {

    public static void main(String[] args) {

        // 创建一个PrintTemplate对象
        PrintTemplate printTemplate = new PrintTemplate();

        // 设置标题、字体大小、字体颜色、背景颜色、行间距、列间距
        printTemplate.setTitle("My Document"); 
        printTemplate.setFontSize(12); 
        printTemplate.setFontColor("#000000"); 
        printTemplate.setBackgroundColor("#FFFFFF"); 
        printTemplate.setLineSpacing(1); 
        printTemplate.setColumnSpacing(1); 

        // 添加文本内容并打印文档 
        String content = "This is my document."; 
        printTemplate.addContent(content); 
        printTemplate.printDocument(); 

    }  										   }   

Java 打印样式格式

Java格式 - Java 打印样式格式


java.util.Formatter 类支持printf样式格式化。

printf样式格式化是C编程语言的良好支持。

以下代码在Java中使用C的Printf样式格式。

import java.util.Date;

public class Main {
  public static void main(String[] args) {
    // Formatting strings
    System.out.printf("%1$s, %2$s,  and  %3$s %n", "ABC", "DEF", "XYZ");
    System.out.printf("%3$s, %2$s,  and  %1$s %n", "ABC", "DEF", "XYZ");

    // Formatting numbers
    System.out.printf("%1$4d, %2$4d, %3$4d %n", 1, 10, 100);
    System.out.printf("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000);
    System.out.printf("%1$-4d, %2$-4d,  %3$-4d %n", 1, 10, 100);
    System.out.printf("%1$-4d, %2$-4d,  %3$-4d %n", 10, 100, 1000);

    // Formatting date and time
    Date dt = new Date();
    System.out.printf("Today is  %tD  %n", dt);
    System.out.printf("Today is  %tF  %n", dt);
    System.out.printf("Today is  %tc  %n", dt);
  }
}

上面的代码生成以下结果。

格式化类

我们可以使用System.out.println()和System.out.print()方法在标准输出上打印文本。

System.out是java.io.PrintStream类的一个实例,其中有println()和print()实例方法。

PrintStream类还有 format() printf()它可以支持printf样式的格式化。

String类的静态format()也支持printf样式的格式化。

PrintStream类的format()/printf()方法和String类的format()静态方法的格式化行为是相同的。

PrintStream类中的format()/printf()方法将格式化的输出写入输出流,而String类的format()方法返回格式化的输出。

PrintStream类的format()/printf()方法和String类的format()静态方法取决于Formatter类。

格式化程序用于格式化文本,可以写入以下目的地:

  • 附加(例如StringBuffer,StringBuilder,Writer等)
  • 文件
  • 输出流
  • 打印流

以下代码显示如何使用Formatter类来格式化数据并将结果存储在StringBuilder类中。

import java.util.Date;
import java.util.Formatter;

public class Main {
  public static void main(String[] args) {
    StringBuilder sb = new StringBuilder();
    Formatter fm = new Formatter(sb);

    // Formatting strings
    fm.format("%1$s, %2$s,  and  %3$s %n", "A", "B", "C");
    fm.format("%3$s, %2$s,  and  %1$s %n", "D", "E", "F");

    // Formatting numbers
    fm.format("%1$4d, %2$4d, %3$4d %n", 1, 10, 100);
    fm.format("%1$4d, %2$4d, %3$4d %n", 10, 100, 1000);
    fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 1, 10, 100);
    fm.format("%1$-4d, %2$-4d,  %3$-4d %n", 10, 100, 1000);

    // Formatting date and time
    Date dt = new Date();
    fm.format("Today  is %tD  %n", dt);
    fm.format("Today  is %tF %n", dt);
    fm.format("Today  is %tc %n", dt);

    // Display the entire formatted string
    System.out.println(sb.toString());

  }
}

上面的代码生成以下结果。

格式化为文件

要将所有格式化的文本写入文件,请使用以下代码。

我们必须处理FileNotFoundException,如果指定的文件不存在,它可能会从Formatter类的构造函数抛出。

我们必须调用它的close()方法来关闭输出文件。

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Formatter;

public class Main {
  public static void main(String[] args) {
    File file = new File("xyz.txt");
    Formatter fm = null;
    try {
      // Create a Formatter that will write the output the file
      fm = new Formatter(file);

      // Formatting strings
      fm.format("%1$s, %2$s,  and  %3$s %n", "A", "B", "C");
      fm.format("%3$s, %2$s,  and  %1$s %n", "A", "B", "C");

      // Format more text here...
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } finally {
      if (fm != null) {
        fm.close();
      }
    }
  }
}

formatatter()方法

Formatatter的format()方法是重载的。其声明如下。

Formatter format(String format, Object... args)
Formatter format(Locale l, String format, Object...  args)

format()方法的第一个版本使用默认语言环境进行格式化。第二个版本允许您指定语言环境。

PrintStream类的format()/printf()方法和String类的format()方法支持format()方法的这两个版本。

阅读全文
以上是名动网为你收集整理的java打印模版 Java 打印样式格式全部内容。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。
相关文章
© 2024 名动网 mdwl.vip 版权所有 联系我们