1 package shurushuchu; 2 import java.io.*; 3 public class TestRW { 4 5 public static void main(String[] args) { 6 /*1.编写TextRw.java的Java应用程序,程序完成的功能是: 7 * 首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。*/ 8 try { 9 File f = new File("D:/TestDW.txt"); 10 if (!(f.exists())) 11 { 12 13 f.createNewFile(); 14 System.out.println("文件创建成功"); 15 } 16 FileOutputStream fs=new FileOutputStream(f,true); 17 String s=" 姓名"; 18 String ss=" 学号"; 19 byte[] b=s.getBytes(); 20 byte[] b1=ss.getBytes(); 21 fs.write(b); 22 fs.write(b1); 23 fs.close(); 24 System.out.println("写入文件成功"); 25 FileInputStream fi=new FileInputStream(f); 26 byte[] d=new byte[1024]; 27 String st=" 姓名"; 28 String st1=" 学号"; 29 int i=0; 30 while((i=fi.read(d))>0) 31 { 32 String s1=new String(d, 0, i); 33 st+=s1; 34 35 } 36 System.out.println("输入的内容为:"+st); 37 int i1=0; 38 while((i1=fi.read(d))>0) 39 { 40 String ss1=new String(d, 0,i1); 41 st1+=ss1; 42 } 43 System.out.println("输入的内容为:"+st1); 44 fi.close(); 45 } 46 catch (Exception e) 47 { 48 // TODO 自动生成的 catch 块 49 e.printStackTrace(); 50 } 51 } 52 53 54 }