编写TextRw.java的Java应用程序,程序完成的功能是:首先向TextRw.txt中写入自己的学号和姓名,读取TextRw.txt中信息并将其显示在屏幕上。
package homework007; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Text001 { public static void main(String[] args) throws Exception { try { //写入 String fileaddress = "e:/TextRw.txt"; FileOutputStream out = new FileOutputStream(fileaddress); String str = "学号:2012412440 姓名:张伟"; byte[] b = str.getBytes(); out.write(b); System.out.println("写入成功"); out.close(); //读取 FileInputStream in = new FileInputStream(fileaddress); int i =0; String str1 = ""; byte[] b1 = new byte[1024]; while((i=in.read(b1))>0) { str1+=new String(b1,0,i); } System.out.println(str1); } catch (FileNotFoundException e) { e.printStackTrace(); } } }