毕业论文论文范文课程设计实践报告法律论文英语论文教学论文医学论文农学论文艺术论文行政论文管理论文计算机安全
您现在的位置: 毕业论文 >> 报告 >> 正文

使用FileInputStream和FileOutputStream类将当前目录中的Write1.txt文件复制成Write2.txt文件

更新时间:2007-10-4:  来源:毕业论文

JAVA2程序设计实用教程之Java的面向对象特征上机报告|上机实验实习报告|计算机上机实习报告|使用FileInputStream和FileOutputStream类将当前目录中的Write1.txt文件复制成Write2.txt文件|如果去掉sleep语句(同时去掉catch(InterruptedException e)语句),则运行结果将会是怎样

算法:import java.io.*;

public class Write1

{

    public static void main(String args[])

    {

        try

        {

            System.out.print("Input: ");

            int count,n=512;

            byte buffer[] = new byte[n];

            count = System.in.read(buffer);        //读取标准输入流

            FileOutputStream  wf1 = new FileOutputStream("Write1.txt");

            FileOutputStream  wf2 = new FileOutputStream("Write2.txt");

            FileInputStream rf=new FileInputStream("Write1.txt");                                     

            wf1.write(buffer,0,count);              //写入输出流

            wf1.close();    //关闭输出流

            System.out.println("Save to Write1.txt!");

            rf.read(buffer,0,count);

            wf2.write(buffer,0,count);

            wf2.close();

            rf.close();

            System.out.println("copy to Write2.txt!");

           

        }

        catch (IOException ioe)

        {

            System.out.println(ioe);

        }

        catch (Exception e)

        {

            System.out.println(e);

        }

    }

}

结果:若图片无法显示请联系站长QQ752018766

如果去掉sleep语句(同时去掉catch(InterruptedException e)语句),则运行结果将会是怎样?

算法:import java.io.*;

public class Pipedstream

{

    public static void main (String args[])

    {

        PipedInputStream in = new PipedInputStream();

        PipedOutputStream out = new PipedOutputStream();

        try

        {

             in.connect(out);

        }

        catch(IOException ioe)   {  }

        Send s1 = new Send(out,1);

        Send s2 = new Send(out,2);

        Receive r1 = new Receive(in);

        Receive r2 = new Receive(in);

        s1.start();  s2.start();

        r1.start();   r2.start();

    }

}

class Send extends Thread                //发送线程

{

    PipedOutputStream out;

    static int count=0;                  //记录线程个数

    int k=0;

    public Send(PipedOutputStream out,int k)

    {

        this.out= out;

        this.k= k;

        this.count++;                    //线程个数加1

    }

    public void run( )

    {

        System.out.print("\r\nSend"+this.k+":   "+this.getName()+"  ");

        int i=k;

        try

        {

            while (i<10)

            {

                out.write(i);

                i+=2;

                //sleep(1);

            }

            if (Send.count==1)           //只剩一个线程时

            {

                out.close();             //关闭输入管道流

                System.out.println("  out closed!");

            }   

            else

                this.count--;            //线程个数减1

        }

        //catch(InterruptedException e)   {  }

        catch(IOException e)   { }

    }

}

class Receive extends Thread             //接收线程

{

    PipedInputStream in;

    public Receive(PipedInputStream in)

    {

        this.in = in;

    }

    public void run( )

    {

        System.out.print("\r\nReceive: "+this.getName()+"  ");

        try

        {

            int i = in.read();

            while (i!=-1)                //输入流未结束时

            {

                System.out.print(i+"  ");

                i = in.read();

                //sleep(1);

            }

            in.close();                  //关闭输入管道流

        }

       // catch(InterruptedException e)  { }

        catch(IOException e)

        {

            System.out.println(e);

        }

    }

}

结果:若图片无法显示请联系站长QQ752018766

使用FileInputStream和FileOutputStream类将当前目录中的Write1.txt文件复制成Write2.txt文件下载如图片无法显示或论文不完整,请联系qq752018766
设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©lwfree.cn 六维论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。