Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2293488
  • 博文数量: 321
  • 博客积分: 3440
  • 博客等级: 中校
  • 技术积分: 2992
  • 用 户 组: 普通用户
  • 注册时间: 2007-05-24 09:08
个人简介

我就在这里

文章分类

全部博文(321)

文章存档

2015年(9)

2014年(84)

2013年(101)

2012年(25)

2011年(29)

2010年(21)

2009年(6)

2008年(23)

2007年(23)

分类: Java

2013-04-09 12:06:49


  1. <span style="font-size:16px;">package demo.others;

  2. import java.io.FileNotFoundException;
  3. import java.io.PrintStream;
  4. import java.util.Formatter;

  5. /**
  6.  * Formatter类用于格式化
  7.  *
  8.  * @author Touch
  9.  *
  10.  */
  11. public class FormatterDemo {
  12.     public static void main(String[] args) {
  13.         int i = 1;
  14.         double d = 2.2352353456345;
  15.         // 1.两种最简单的格式化输出,类似c语言中的printf函数
  16.         System.out.format("%-3d%-5.3f\n", i, d);
  17.         System.out.printf("%-3d%-5.3f\n", i, d);
  18.         // Formatter类的使用
  19.         // 2.格式化输出到控制台
  20.         Formatter f = new Formatter(System.out);
  21.         f.format("%-3d%-8.2f%-10s\n", i, d, "touch");
  22.         // 3.格式化输出到文件
  23.         Formatter ff = null;
  24.         try {
  25.             ff = new Formatter(new PrintStream("file/formater.txt"));
  26.         } catch (FileNotFoundException e) {
  27.             e.printStackTrace();
  28.         }
  29.         ff.format("%-3d%-8.2f%-10s\n", i, d, "touch");
  30.         // 4.String.format().同c语言中sprintf()
  31.         System.out.println(String.format("(%d%.2f%s)", i, d, "touch"));
  32.     }
  33. }
  34. </span>

转自:http://blog.csdn.net/touch_2011/article/details/6860574

阅读(913) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~