IO流对文件的读取操作

/*1. 在当前项目的根目录下有一个名为“info.txt”的文件,里面存放的内容如下(可手动创建录入,不需要使用IO流):

2. 利用IO流的知识读取info.txt文件的内容, 在控制台上打印大写字符出现的次数。
格式如下:*/

info.txt文件的内容:

Life Is Short
You Need AmaZing

代码实现:

 1 import java.io.BufferedReader;
 2 import java.io.FileNotFoundException;
 3 import java.io.FileReader;
 4 import java.io.IOException;
 5 
 6 public class Test {
 7 
 8     public static void main(String[] args) throws IOException {
 9         BufferedReader br = new BufferedReader(new FileReader("info.txt")); 
10         String line = null;
11         int count = 0;
12         while((line = br.readLine()) != null){
13             //System.out.println(line);
14             char[] ch = line.toCharArray();
15             for (int i = 0; i < ch.length; i++) {
16                 if(ch[i] >= 'A' &&ch[i] <='Z'){
17                     count++;
18                 }
19             }
20         }
21         br.close();
22         System.out.println("大写字符出现的次数为:"+count);
23     }
24 
25 }
原文地址:https://www.cnblogs.com/YangGC/p/8511567.html