欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

3.7java中输入输出文件读取

程序员文章站 2022-03-01 18:06:50
...
import java.io.Console;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Scanner;

public class Inputoutput {
    public static void main(String[] args) throws  IOException {

   /*     Scanner in = new Scanner(System.in);
        System.out.println("what is your name");
        String name = in.nextLine();
        System.out.println("how old are you");
        int age =in.nextInt();

        System.out.println("名字"+name+",年龄"+age);*/

        //控制台密码输入
        Console cons = System.console();
        if (cons != null) {
            String username = cons.readLine("username");
            char[] passwd = cons.readPassword("pwd");

            System.out.println("用户:" + username + ",密码:" + passwd);
        }

//        格式化输出
        double x = 10000.0 / 3.0;
        System.out.println(x);
        System.out.printf("%8.2f",x);
        System.out.printf("%b",x);

//        文件读取

        Scanner in = new Scanner(Paths.get("D:\\javaStudy\\day02\\src\\myfile.txt"),"UTF-8");
        try{
            while (true){
                String s = in.nextLine();
                System.out.println(s);
            }
        }
        catch (Exception e){
            e.printStackTrace();

        }
    }
}

相关标签: java