基于字符流读写文件

 1 //写文件
 2 package homework;
 3 
 4 import java.io.BufferedWriter;
 5 import java.io.File;
 6 import java.io.FileWriter;
 7 import java.io.IOException;
 8 import java.util.Scanner;
 9 
10 public class Wenjian1 {
11 
12     public static void main(String[] args) throws IOException {
13         Scanner in=new Scanner(System.in);
14         String pathname="d:/student1.txt";
15         File f=new File(pathname);
16         FileWriter fw=null;
17         BufferedWriter bw=null;
18         fw=new FileWriter(f);
19         bw=new BufferedWriter(fw);
20         bw.write("姓名\t数学\t物理\t化学\t总成绩");
21         bw.newLine();
22         String xm;
23         int cj1,cj2,cj3,zcj;
24         int yes=1;
25         while(yes==1) {
26             System.out.println("请依次输入 姓名 数学 物理 化学:");
27             xm=in.next();
28             cj1=in.nextInt();
29             cj2=in.nextInt();
30             cj3=in.nextInt();
31             zcj=cj1+cj2+cj3;
32             bw.write(xm+"\t"+cj1+"\t"+cj2+"\t"+cj3+"\t"+zcj);
33             bw.newLine();
34             System.out.println("继续添加学生吗?(1/0)");
35             yes=in.nextInt();
36         }
37         bw.close();
38         fw.close();
39         in.close();
40         System.out.println("运行结束");
41     }
42 
43 }
 1 //读文件,将一个文本文件输出在屏幕上,在输出时将小写字母转换成大写字母
 2 package homework;
 3 
 4 import java.io.BufferedReader;
 5 import java.io.File;
 6 import java.io.FileNotFoundException;
 7 import java.io.FileReader;
 8 import java.io.IOException;
 9 
10 public class Wenjian2 {
11 
12     public static void main(String[] args) throws IOException {
13         File f = new File("d:/dssjava/abc.txt");
14         if (!f.exists()) {
15             System.out.println("文件不存在");
16             System.exit(0);
17         }
18         FileReader fr = new FileReader(f);
19         BufferedReader br = new BufferedReader(fr);
20         String msg = br.readLine();
21         while (msg != null) {
22             String abc = msg.toUpperCase();
23             System.out.println(abc);
24             msg = br.readLine();
25         }
26         br.close();
27         fr.close();
28     }
29 
30 }

 

posted @ 2020-12-18 01:04  丁帅帅dss  阅读(89)  评论(0)    收藏  举报