package lianxi; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.util.Scanner; public class GaimingDemo { public static void main(String[] args) { try { FileInputStream in = new FileInputStream("d:/text.txt") ; byte[ ] b = new byte[1024] ; String s = "" ; int i = -1 ; while((i=in.read(b))>0) { s += new String(b,0,i) ; } System.out.println("输入完成!"); System.out.println("读取到的内容为:"+s); in.close(); System.out.println("请输入修改后的名字:"); Scanner sc = new Scanner(System.in) ; String str = sc.nextLine( ) ; File file = new File("d:/text.txt"); file.renameTo(new File("d:/"+str)) ; FileOutputStream out = new FileOutputStream(file) ; byte[ ] by = str.getBytes() ; out.write(by); out.close(); System.out.println("输出完成!"); System.out.println("改名成功!"); } catch (Exception e) { e.printStackTrace(); } } }