Java笔记—— 格式化的输入和输出

时间:2023-02-25 14:32:03

精确输出

可以用8个字符的宽度和小数点后了两个字符的精度打印x。

double x = 10000.0 /3.0;
System.out.printf("%8.2f\n",x);// 输出结果: 3333.33

控制输出的各种标志

可以控制输出的各种标志(具体详情 见《java核心技术卷1 p59》)

System.out.printf("%,.2f\n",10000.0/3.0); //输出 3,333.33
System.out.printf("%+.2f\n",x);//输出 +3333.33
System.out.printf("%+d\n",888);//+888

创建一个格式化的字符串,而不打印输出

int age = 20;
String name = "yesyjl";
String message = String.format("Hello,%s.Next year,you will be %d",name,age );//不显示

密码的输入(输入不可见)

密码的输入(根据系统,有的系统这个不允许 包:import java.io.Console

Console cons = System.console();
String username = cons.readLine("User name: ");
char[] passwd = cons.readPassword("Password");

日期与时间的格式化选项

这个需要包:import java.util.Date

System.out.printf("%tc\n",new Date()); //输出 星期二 九月 12 11:50:37 CST 2017
System.out.printf("%tF\n",new Date()); //输出 2017-09-12(IOS 8601 日期)
System.out.printf("%tT\n",new Date()); //输出 11:52:27 (24小时时间)

文件输入输出

“文件的输入输出”这个意思应该是在程序中输入这个文件,或者输出这个文件,而不是对这个文件的处理。刚刚开始没有用throws子句进行标记,所以出现了为止的错误。
对于txt文件的读写,文件的输入和输出,需要如果用一个不存在的文件构造一个Scnner,或者用一个不能被创建的文件名构造一个
PrintWriter,那么久会发生异常。Java编译器认为这些异常比“被零除”异常更严重。所以为了防止可能出现的情况。这需要在main方法中用throws子句标记如下所示

public static void main(String[] args) throws IOException
{
Scanner in = new Scanner(Paths.get("myfile.txt"),"UTF-8");
}
  • 1.注意在用throws时,会用到包 import java.io.IOException;
  • 2.注意在用到 Paths.get时,会用到包 import java.nio.file.Paths;
Scanner in = new Scanner(Paths.get("f:\\12\\myfile.txt"),"UTF-8");//文件的输入
PrintWriter out = new PrintWriter("f:\\12\\myfile.txt","UTF-8");//文件的输出

所有代码


package www.yjlblog.cn;
/*
* 格式化输入和输出*/


import java.util.Date;
import java.util.Scanner;

//import java.io.Console;


public class imput
{
public static void main(String[] args)
{
double x = 10000.0 /3.0;
//可以用8个字符的宽度和小数点后了两个字符的精度打印x。
System.out.printf("%8.2f\n",x);// 输出结果: 3333.33

//可以控制输出的各种标志(具体详情 见《java核心技术卷1 p59》)
System.out.printf("%,.2f\n",10000.0/3.0); //输出 3,333.33
System.out.printf("%+.2f\n",x);//输出 +3333.33
System.out.printf("%+d\n",888);//+888

//可以使用静态的String.format 方法创建一个格式化的字符串,而不打印输出
int age = 20;
String name = "yesyjl";
String message = String.format("Hello,%s.Next year,you will be %d",name,age );//不显示

//密码的输入(根据系统,有的系统这个不允许 包:import java.io.Console;)

Console cons = System.console();
String username = cons.readLine("User name: ");
char[] passwd = cons.readPassword("Password");


//日期与时间的格式化选项(import java.util.Date;)
System.out.printf("%tc\n",new Date()); //输出 星期二 九月 12 11:50:37 CST 2017
System.out.printf("%tF\n",new Date()); //输出 2017-09-12(IOS 8601 日期)
System.out.printf("%tT\n",new Date()); //输出 11:52:27 (24小时时间)

//文件的输入输出
//对于txt 文件的读写
//文件的输入和输出,需要如果用一个不存在的文件构造一个Scnner,或者用一个不能被创建的文件名构造一个
//PrintWriter,那么久会发生异常。Java编译器认为这些异常比“被零除”异常更严重。所以为了防止可能出现的情况
//这需要在main方法中用throws 子句标记如下所示
/*
*public static void main(String[] args) throws IOException
*{
* Scanner in = new Scanner(Paths.get("myfile.txt"),"UTF-8");
*} */
//1.注意在用throws 时,会用到包 import java.io.IOException;
//2.注意在用到 Paths.get 时,会用到包 import java.nio.file.Paths;
//public static void main(String[] args) throws IOException
Scanner in = new Scanner(Paths.get("f:\\12\\myfile.txt"),"UTF-8");
PrintWriter out = new PrintWriter("f:\\12\\myfile.txt","UTF-8");






}
}

未完待续....