Java IO流详解(三)

Scanner类
1 从键盘读取

public class ScannerTest {
    public static void main(String[] args ) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输出一个整数:");
        int i = input.nextInt();
        System.out.println("你输入的整数是:" + i);
    }
}

结果:
这里写图片描述

2 从字符串读取

public class ScannerTest2 {

    public static void main(String[] args ) {
        //这里的\r\n是换行符,Linux下其实只用\n即可
        Scanner input = new Scanner("hello\r\nworld\r\n");
        //循环读取,hasNext()方法和集合框架里面的一样使
        while(input.hasNext()) {
            //每次读取一行,别的读取方法见API,比较简单
            String s = input.nextLine();
            System.out.println(s);
        }  
    }
}

结果如图:
这里写图片描述

3 从文件读取

public class ScannerTest3 {
    public static void main(String[] args ) {

        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        File f = new File(path);
        Path path2 = f.toPath();
        Scanner input = null;
        try {
            //从文件构造Scanner对象,有可能产生异常
            input = new Scanner(path2);

            System.out.println(f.getAbsolutePath());
            System.out.print(input.hasNext());
            while(input.hasNext()) {
                String s = input.nextLine();
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
        }  
    }
}

结果图:
这里写图片描述
从上图可以看出编码格式不同,对于中文扫描输出是乱码。
还有一点需要注意:

public class ScannerTest3 {
    public static void main(String[] args ) {

        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        File f = new File(path);
        Scanner input = null;
        try {
            //从文件构造Scanner对象,有可能产生异常
            input = new Scanner(f);
            System.out.println(f.getAbsolutePath());
            System.out.print(input.hasNext());
            while(input.hasNext()) {
                String s = input.nextLine();
                System.out.println(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            input.close();
        }  
    }
}

在我进行测试的时候,使用如上的方式进行文件的读取操作,读取不到文件的内容。不知为何!!!??请大家谁知道的话,不吝赐教~~~!!

PrintWriter类
4 向文件写入内容

public class PrintWriterToFile {
    public static void main(String[] args) {
        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        //创建文件对象
        File file = new File(path);

        PrintWriter p = null;
        try {
            //此处构造函数还可以传其他对象,具体参考API文档
            p = new PrintWriter(file);

            //向文件写入一行,此外还有print()和printf()方法
            p.println("如果有一天我回到从前");
            p.println("回到最原始的我");
            p.println("你是否会觉得我不错");

            //刷新流
            p.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            p.close();
        }  
    }
}

结果图:
这里写图片描述

与PrintWriter类似的还有一个PrintStream类,此处以PrintWriter举例是因为文本文件具有人为可读性
而二进制文件(字节模式)则需要使用专门的程序来读取
可能有人会问:FileOutputStream、 FileWriter都能写文件,那么为何还需要PrintWriter和PrintStream类
如果细看API文档,可以知道前者单纯的字符写入流和字节写入流操作的方式大多用数组进行
对文件的细化处理非常不方便,而PrintWriter和PrintStream则很好的解决了这一问题,提供print()等方法
并且,PrintWriter和PrintStream对于不存在文件对象的情况下会直接创建,如果已有文件对象
它们则会把原有文件给覆盖掉,却没有增加方法
解决这问题也很简单,再看API文档
PrintWriter有一个构造方法PrintWriter(Writer out),也就是能够传入Writer对象
PrintStream有一个构造方法PrintStream(OutputStream out),也就是能传入OutputStream对象
因此,我们这样写就可以了
new PrintWriter(new FileWriter(file,true))
new PrintStream(new FileOutputStream(file,true))
既能增加数据,也能更高效的处理文件

5 实现PrintWriter的数据追加功能

public class PrintWriterAppendFile {
    public static void main(String[] args) {
        String path = "D:\\Program Files (x86)\\ADT\\workspace\\JavaIO\\demoTest.txt";

        //创建文件对象
        File file = new File(path);
        PrintWriter p = null;
        try {
            //利用FileWriter方式构建PrintWriter对象,实现追加
            p = new PrintWriter(new FileWriter(file,true));
            p.println("wqnmglb 这一句就是追加的 看到没");

            p.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //我们来小心翼翼的关闭流,好吧^_^
            p.close();
        }
    }
}

结果:
这里写图片描述

System类对IO的支持
6 System类中的写入

public class SystemOutTest {
    public static void main(String[] args) {
        //别忘了,OutputStream是所有字节写入流的父类
        OutputStream out = System.out;
        try {
            //写入数据,只能是数组,所以用getBytes()方法
            out.write("Hello,son of bitch!\r\n".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

结果:
这里写图片描述

7 System类中的读取

public class SystemInTest {
    public static void main(String[] args) {
        //别忘了InputStream是所有字节输入流的父类
        InputStream in = System.in;
        System.out.print("请输入文字: ");
        byte[] buf = new byte[1024];
        int len = 0;
        try {
            //将输入的数据保证到数组中,len记录输入的长度
            len = in.read(buf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        //用字符串的方式打印数组中的数据
        System.out.println("你的输入是: " + new String(buf,0,len));

    }
}

结果:
这里写图片描述
不过对于上面的情况,如果输入中文,则输出为乱码。请大家注意。原因在于读取的是字节的缘故。如果读取字符,则不存在乱码的情况。
其实,对于目前可能出现的乱码情况,大家大可不必纠结。因为在实际开发过程中,都会基本一定的环境当中。例如:进行android开发,android上面默认的字符编码是utf-8,你是知道的,所以即使出现了乱码的情况,使用字符格式转换Charset类就可以纠正。在例如:进行WEB开发,在WEB开发的过程中需要指定字符编码格式,或者GBK 或者UTF-8 不过推荐使用utf-8,因为平台移植性好。

8 利用BufferedReader实现对键盘的读取

public class BufferedReaderFromScanner {
    public static void main(String[] args) {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("请输入文本:");
        try {
            String str = bufferedReader.readLine();
            System.out.println("你输入的是:" + str);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            //关闭流,不耐烦的就直接抛
            bufferedReader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

结果:
这里写图片描述

如果有什么问题或者错误 请留言!谢谢~~~~【握手】

源码下载

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值