请教IO问题
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1:21
请教IO问题
我学JAVA不久,今天自己写程序出了问题,大家帮我看看好吗.
我是想写一个双线程的,把一个txt文件,一边读取,一边输出到另一个文件中.
可是10页纸长度的1.txt输出到2.txt后只有1页的长度了,还报了错.
我对IO掌握的不好,高手顺便教教我.
我是想写一个双线程的,把一个txt文件,一边读取,一边输出到另一个文件中.
可是10页纸长度的1.txt输出到2.txt后只有1页的长度了,还报了错.
我对IO掌握的不好,高手顺便教教我.
上次由 liujingjing5 在 2007-04-02 23:28,总共编辑 1 次。
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1: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
代码: 全选
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
代码: 全选
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
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!
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
程序是自动结束的,
也生成了2.txt 只是内容没有完全复制
也生成了2.txt 只是内容没有完全复制
- laborer
- 帖子: 1016
- 注册时间: 2005-10-25 11:15
- 联系:
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1:21
- anticlockwise
- 帖子: 2394
- 注册时间: 2007-03-01 20:46
- 来自: 湖南长沙
- liujingjing5
- 帖子: 512
- 注册时间: 2006-10-12 1:21