请教IO问题

软件和网站开发以及相关技术探讨
回复
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

请教IO问题

#1

帖子 liujingjing5 » 2007-04-02 23:20

我学JAVA不久,今天自己写程序出了问题,大家帮我看看好吗.
我是想写一个双线程的,把一个txt文件,一边读取,一边输出到另一个文件中.
可是10页纸长度的1.txt输出到2.txt后只有1页的长度了,还报了错.
我对IO掌握的不好,高手顺便教教我.
上次由 liujingjing5 在 2007-04-02 23:28,总共编辑 1 次。
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

主函数

#2

帖子 liujingjing5 » 2007-04-02 23:21

代码: 全选

import java.io.*;
class test 
{
public static void main (String [] args)
    {
        try
        {
            Readertxt r = new Readertxt();//初始化对象
            Writertxt w = new Writertxt();
            r.setFile("c:\\1.txt");//设置文件位置
            w.setFile("c:\\2.txt");
            PipedOutputStream out = r.getOutputStream();//获得管道对象
            PipedInputStream in = w.getInputStream();
            out.connect(in);//连接管道
            Thread t1 = new Thread(r);//创建线程
            Thread t2 = new Thread(w);
            t1.start();//启动线程
            t2.start();
        }
        catch(Exception e)
        {
            System.out.println("main is something error!");
        }
    }
            
}
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

reader

#3

帖子 liujingjing5 » 2007-04-02 23:22

代码: 全选

import java.io.*;
class Readertxt implements Runnable
{
    int len;
    FileInputStream fr = null;//建立INPUT对象
    private PipedOutputStream out = new PipedOutputStream();//建立PIP对象
    public PipedOutputStream getOutputStream()//提供外界获得PIP对象方法
    {
      return out;  
    }
    public File fo = null;//建立FILE对象
    public void setFile(String a)//提供外界设置FILE路径方法
    {
        fo = new File(a);
    }
    public Readertxt() //构造函数以标记
    {
        System.out.println("readertxt is creating!");
    }
    public void run()
    {
        System.out.println("reader is runing!");
        
        try{fr = new FileInputStream(fo);}//赋值INPUT对象
        catch(Exception e){System.out.println("creat inputstream is error!");}
        byte []buf = new byte[1024];
        
        while(true)//循环读取
        {
            try
            {
                len = fr.read(buf);//INPUT读取数据到BUF[]
            }
            catch(Exception e){System.out.println("reader input is error!");}
            
            try
            {
                out.write(buf);//PIP将数据写入管道
            }
            catch(Exception e){System.out.println("reader pip is error!");}
            
            if (len < 1024)//最后一次读取一定不满1024,跳出循环
            {
                try{fr.close();out.close();}
                catch(Exception e){System.out.println("reader close is error!");}
                break;
            }
        }
    }
}
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

writer

#4

帖子 liujingjing5 » 2007-04-02 23:23

代码: 全选

import java.io.*;
class Writertxt implements Runnable
{
    int len;
    FileOutputStream fw = null;
    private PipedInputStream in = new PipedInputStream();
    public PipedInputStream getInputStream()
    {
        return in;
    }
    public File fn = null;
    public void setFile(String b)
    {
        fn = new File(b);
    }
    public Writertxt() 
    {
        System.out.println("writertxt is creating!");
    }
    public void run()
    {
        System.out.println("writer is runing!");
        
        try{fn.createNewFile();}
        catch(Exception e){e.getStackTrace();}
   
        try{fw = new FileOutputStream(fn);}
        catch(Exception e){System.out.println("creat outstream is error");}
        byte [] buf = new byte[1024];
        while(true)
        {
            try {
                len = in.read(buf);
                fw.write(buf);
            }
            catch (Exception e) {System.out.println("writer is error");}
            
            if (len < 1024);
            {
                try{in.close();fw.close();}
                catch(Exception e){System.out.println("write close is error!");}
                break;
            }
    }
    }
}
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

error

#5

帖子 liujingjing5 » 2007-04-02 23:25

readertxt is creating!
writertxt is creating!
reader is runing!
writer is runing!
reader pip is error!
reader pip is error!
reader pip is error!
reader pip is error!
reader pip is error!
reader pip is error!
reader pip is error!
reader pip is error!
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

error

#6

帖子 liujingjing5 » 2007-04-02 23:26

程序是自动结束的,
也生成了2.txt 只是内容没有完全复制
头像
laborer
帖子: 1016
注册时间: 2005-10-25 11:15
联系:

#7

帖子 laborer » 2007-04-02 23:49

代码: 全选

            if (len < 1024)//最后一次读取一定不满1024,跳出循环
不能这样判断是否结束的,虽然不一定会出错,你再仔细看一下java-doc。

代码: 全选

           if (len < 1024); 
多个分号。
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

#8

帖子 liujingjing5 » 2007-04-03 10:08

回楼上,果然是多了一个分号,输出的2.txt的长度基本够了.但是最后出现重复写的现象.
我查了DOC,但我还是不明白:
1,writer的那个分号导致第一轮管道流就关闭了,怎么会reader pip is error呢??
2,在reader中,我想不到什么方法能判断读取已结束,可以给点提示吗?
头像
anticlockwise
帖子: 2394
注册时间: 2007-03-01 20:46
来自: 湖南长沙

#9

帖子 anticlockwise » 2007-04-03 11:39

1. 你的Writer管道流虽然关闭,但是你的Reader管道流还在使用,当发现其中一个管道被关闭时,当然就会报错啦
2. 一般都是使用PipedInputStream.available()方法来判断是否已经读取完毕,所以才叫你看看JavaDoc的哦
头像
liujingjing5
帖子: 512
注册时间: 2006-10-12 1:21

#10

帖子 liujingjing5 » 2007-04-03 13:17

原来问题出来这,write()没有指定len,最后一次写入没有覆盖掉buf[len]以后的数据,所以最后出现了重复.
try
{
out.write(buf);//PIP将数据写入管道
}

不过我的程序写的很低级,试用了available()方法后,死机了,重起后写出的2.txt竟有100M.

谁写个正统的方法教教我吧,只要写输入输出部分的代码,我学习学习....
回复