编写IoDemo.java的Java应用程序,程序完成的功能是:首先读取text.txt文件内容,再通过键盘输入文件的名称为iodemo.txt,把text.txt的内容存入iodemo.txt

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();
        }
    }

}

 

posted @ 2016-09-27 20:02  丶疏影横斜  阅读(298)  评论(0编辑  收藏  举报