热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Java中的Formatterout()方法,示例

Java中的Formatterout()方法,示例原文:h

Java 中的 Formatter out()方法,示例

原文:https://www . geesforgeks . org/formatter-out-method-in-Java-with-examples/

out() 方法是 java.util.Formatter 的内置方法,它返回格式化程序输出的目的地。

语法 :

public Appendable out()

参数:该功能不接受参数。

返回值:该函数返回输出的目的地。

异常:如果在函数调用之前格式化程序已经关闭,函数抛出格式化程序关闭异常

下面是上述功能的实现:

程序 1:

// Java program to implement
// the above function
import java.util.Formatter;
import java.util.Locale;
public class Main {
    public static void main(String[] args)
    {
        // Get the string Buffer
        StringBuffer buffer
            = new StringBuffer();
        // Object creation
        Formatter frmt
            = new Formatter(buffer,
                            Locale.CANADA);
        // Format a new string
        String name = "My name is Gopal Dave";
        frmt.format("What is your name? \n%s !",
                    name);
        // Print the Formatted string
        System.out.println(frmt);
        // Prints the destination of the output
        System.out.println("\nDestination: "
                           + frmt.out());
    }
}

输出:

What is your name?
My name is Gopal Dave !
Destination: What is your name?
My name is Gopal Dave !

程序二:

// Java program to implement
// the above function
import java.util.Formatter;
import java.util.Locale;
public class Main {
    public static void main(String[] args)
    {
        try {
            // Get the string Buffer
            StringBuffer buffer
                = new StringBuffer();
            // Object creation
            Formatter frmt
                = new Formatter(buffer,
                                Locale.CANADA);
            // Format a new string
            String name = "My name is Gopal Dave";
            frmt.format("What is your name? \n%s !",
                        name);
            // Print the Formatted string
            System.out.println(frmt);
            // Formatter closed
            frmt.close();
            // Prints the destination of the output
            System.out.println("\nDestination: "
                               + frmt.out());
        }
        catch (Exception e) {
            System.out.println("Exception is: "
                               + e);
        }
    }
}

输出:

What is your name?
My name is Gopal Dave !
Exception is: java.util.FormatterClosedException

参考:https://docs . Oracle . com/javase/10/docs/API/Java/util/formatter . html # out()


推荐阅读
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • Explore how Matterverse is redefining the metaverse experience, creating immersive and meaningful virtual environments that foster genuine connections and economic opportunities. ... [详细]
  • Java 中的 BigDecimal pow()方法,示例 ... [详细]
  • Java 类成员初始化顺序与数组创建
    本文探讨了Java中类成员的初始化顺序、静态引入、可变参数以及finalize方法的应用。通过具体的代码示例,详细解释了这些概念及其在实际编程中的使用。 ... [详细]
  • 本文介绍了Java并发库中的阻塞队列(BlockingQueue)及其典型应用场景。通过具体实例,展示了如何利用LinkedBlockingQueue实现线程间高效、安全的数据传递,并结合线程池和原子类优化性能。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
  • 本文探讨了如何在给定整数N的情况下,找到两个不同的整数a和b,使得它们的和最大,并且满足特定的数学条件。 ... [详细]
  • 本文介绍如何使用Objective-C结合dispatch库进行并发编程,以提高素数计数任务的效率。通过对比纯C代码与引入并发机制后的代码,展示dispatch库的强大功能。 ... [详细]
  • 在前两篇文章中,我们探讨了 ControllerDescriptor 和 ActionDescriptor 这两个描述对象,分别对应控制器和操作方法。本文将基于 MVC3 源码进一步分析 ParameterDescriptor,即用于描述 Action 方法参数的对象,并详细介绍其工作原理。 ... [详细]
  • Android 渐变圆环加载控件实现
    本文介绍了如何在 Android 中创建一个自定义的渐变圆环加载控件,该控件已在多个知名应用中使用。我们将详细探讨其工作原理和实现方法。 ... [详细]
  • MQTT技术周报:硬件连接与协议解析
    本周开发笔记重点介绍了在新项目中使用MQTT协议进行硬件连接的技术细节,涵盖其特性、原理及实现步骤。 ... [详细]
  • 本文详细介绍了如何构建一个高效的UI管理系统,集中处理UI页面的打开、关闭、层级管理和页面跳转等问题。通过UIManager统一管理外部切换逻辑,实现功能逻辑分散化和代码复用,支持多人协作开发。 ... [详细]
  • 本文详细介绍了Java中org.w3c.dom.Text类的splitText()方法,通过多个代码示例展示了其实际应用。该方法用于将文本节点在指定位置拆分为两个节点,并保持在文档树中。 ... [详细]
author-avatar
顺佳海外
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有