求段关于java代码, 关于怎么样同时读取2个文件,从第一个文件a开始读, 每次读取一定量的数据---byte[] b=new byte[200],把这数据放到一个packet里面,接着再把这个packet放到一个queue里面,然后继续去读第二个文件b,读取同样量的数据,再把这数据放到一个packet里面,接着再把这个packet放到一个queue里面,接着回到第一个文件,重复同样的操作,直到queue满了,再在不同的地方创建两个文件(c,d),把文件a的数据放到c里面,再把文件b的数据放到d里面,然后queue空了,继续之前的操作,读取文件,等queue满了以后再把数据写入c,d中………………
需要创建一个class--packet
不可以用线程和网络!!!
急求!!!
在此先谢谢各位大大了!!
需要创建一个class--packet
不可以用线程和网络!!!
急求!!!
在此先谢谢各位大大了!!
解决方案 »
- 一个小问题
- 急急急!!!!明天要交员工管理系统,今天还没调试出来!!!在线等,急啊!
- DataOutputStream将当于Linux的Socket编程中的哪一个级别?
- 高分求助:C#中console.ReadKey()的功能在JAVA中该怎么实现
- 初学java,问一个关于applet的程序
- 8处错误没有找到
- 单个byte的BCD解码函数。。大家瞧啊
- 如何在tomcat环境,在自己建的目录下,调用servlet
- JAVA程序可不可以编译连接成类似于.exe这样的文件,双击它就能执行?
- 有哪位网友知道JpadPro 4.2 Build 611的注册码。拜托给一个,感谢不尽!
- 怎么让WizardDialog能最小化到任务栏,而且不能使用最大化按钮
- 【高分求解】诡异问题:JComboBox可以下拉,但没有下拉箭头
http://www.hinn.cn/2008/03/jave_file_reader_writer.html
下面是我已有的代码import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.File;
import javax.swing.JFileChooser;public class readfile{
public static void main(String[] args)throws Exception{
for(int i=1; i<=3; i++)
{
BufferedInputStream in=null;
BufferedOutputStream out=null;
JFileChooser chooser = new JFileChooser();
File f = new File(new File("filename.txt").getCanonicalPath());
chooser.setSelectedFile(f);
chooser.showOpenDialog(null);
File curFile = chooser.getSelectedFile();
try{
in=new BufferedInputStream(new FileInputStream(curFile));
String filename = curFile.getName();
String[] str = filename.split("\\.");
String filetype = str[str.length - 1];
out=new BufferedOutputStream(new FileOutputStream("i"+i+"."+filetype));
byte[] b=new byte[200];
int len;
while((len=in.read(b))!=-1){
out.write(b,0,len);
}
out.flush();
}
catch(Exception e){
e.printStackTrace();
}
finally{
if (in!=null){
try{
in.close();
}
catch(Exception e){}
}
if (out!=null){
try{
out.close();
}
catch(Exception e){}
}
}
}
}
}
Quene可以共有一个。
写文件的可以只有一个。
int len;
//这里是每次读b这么长一段 然后就自动while loop自动循环直到文件A读完 再读B
//而我需要的是同时读 还不能用多线程呢
while((len=in.read(b))!=-1)
while(i1.read(buf,0,200) != -1){ //读取文件1
//组装你的packet,存入queue
break;
}
while(i1.read(buf,0,200) != -1){ //读取文件2
//组装你的packet,存入queue
break;
}
}
怎么去write就不说了吧
//组装你的packet,存入queue
break;
}
* @(#)multiplexing.java
*
*
* @author
* @version 1.00 2009/12/18
*/import java.io.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.File;
import javax.swing.JFileChooser;
//main
public class multiplexing{
public static void main(String[] args)throws Exception{
BufferedInputStream in1=null;
BufferedOutputStream out1=null;
BufferedInputStream in2=null;
BufferedOutputStream out2=null;
BufferedInputStream in3=null;
BufferedOutputStream out3=null;
Queue<packet> queue = new Queue<packet>();
packet p = new packet();
//interface for select file
//select first file
try{
//-----------------------------------------------------
//check the request packet type
if(p.typefile == 1)
{
//open all file
JFileChooser chooser1 = new JFileChooser();
File f1 = new File(new File("").getCanonicalPath());
chooser1.setSelectedFile(f1);
chooser1.showOpenDialog(null);
File curFile1 = chooser1.getSelectedFile();
//select second file
JFileChooser chooser2 = new JFileChooser();
File f2 = new File(new File("").getCanonicalPath());
chooser2.setSelectedFile(f2);
chooser2.showOpenDialog(null);
File curFile2 = chooser2.getSelectedFile();
//select third file
JFileChooser chooser3 = new JFileChooser();
File f3 = new File(new File("").getCanonicalPath());
chooser3.setSelectedFile(f3);
chooser3.showOpenDialog(null);
File curFile3 = chooser3.getSelectedFile();
//}
in1=new BufferedInputStream(new FileInputStream(curFile1));
in2=new BufferedInputStream(new FileInputStream(curFile2));
in3=new BufferedInputStream(new FileInputStream(curFile3)); String filename1 = curFile1.getName();
String[] str1 = filename1.split("\\.");
String filetype1 = str1[str1.length - 1];
String fullfilename1 = "copyOf"+filename1+"."+filetype1;
p.IDnumber = filename1;
out1=new BufferedOutputStream(new FileOutputStream(fullfilename1));
String filename2 = curFile2.getName();
String[] str2 = filename2.split("\\.");
String filetype2 = str2[str2.length - 1];
String fullfilename2 = "copyOf"+filename2+"."+filetype2;
p.IDnumber = filename2;
out2=new BufferedOutputStream(new FileOutputStream(fullfilename2));
String filename3 = curFile3.getName();
String[] str3 = filename3.split("\\.");
String filetype3 = str3[str3.length - 1];
String fullfilename3 = "copyOf"+filename3+"."+filetype3;
p.IDnumber = filename3;
out3=new BufferedOutputStream(new FileOutputStream(fullfilename3));
//-----------------------------------------------------
//change the request packet type
p.typefile = 2;
}
//-----------------------------------------------------
//check the request packet type
if(p.typefile == 2)
{
byte[] b=new byte[p.getdataSize()];
boolean eof = false;
while ((p.datafield=in1.read(b))!=-1)
{
queue.enqueue(p);
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
//System.out.println(c.IDnumber);
out1.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
while ((p.datafield=in2.read(b))!=-1)
{
queue.enqueue(p);
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
//System.out.println(c.IDnumber);
out2.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
while ((p.datafield=in3.read(b))!=-1)
{
queue.enqueue(p);
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
//System.out.println(c.IDnumber);
out3.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
out1.flush();
out2.flush();
out3.flush();
//System.out.println(p.IDnumber);
}
}
catch(Exception e){
e.printStackTrace();
}
finally{
if (in1!=null){
try{
p.typefile = 3;
in1.close();
}
catch(Exception e){}
}
if (out1!=null){
try{
p.typefile = 3;
out1.close();
}
catch(Exception e){}
}
if (in2!=null){
try{
p.typefile = 3;
in2.close();
}
catch(Exception e){}
}
if (out2!=null){
try{
p.typefile = 3;
out2.close();
}
catch(Exception e){}
}
if (in3!=null){
try{
p.typefile = 3;
in3.close();
}
catch(Exception e){}
}
if (out3!=null){
try{
p.typefile = 3;
out3.close();
}
catch(Exception e){}
}
}
// }
}
}
class packet
{
//
int typefile;
String IDnumber;
byte dataSize;
int datafield;
public packet()
{
typefile = 1;
IDnumber = "";
dataSize = 80;
datafield = 0;
}
public void settype(int t)
{
typefile = t;
}
public int gettype()
{
return typefile;
}
public byte getdataSize()
{
return dataSize;
}
public void setID(String filename)
{
IDnumber = filename;
}
public String getID()
{
return IDnumber;
}
public void setdatafield(int d)
{
datafield = d;
}
public int getdatafield()
{
return datafield;
}
}
while ((p.datafield=in1.read(b))!=-1) //读进来
{
queue.enqueue(p); //放入队列
if(queue.size()>=20) //size()==1,不执行
{
eof = true;
}
else if(!eof) //eof之前false,执行这里
{
packet c = queue.dequeue(); //出队列
//System.out.println(c.IDnumber);
out1.write(b,0,c.getdatafield()); //写文件
if(queue.isEmpty())
{
eof = false; //完了,又变false
}
}
break; //while结束
}不知道说的对不对,感觉就是逻辑有问题
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.RandomAccessFile;
import java.io.Reader;
import java.io.File;
import javax.swing.JFileChooser;
//main
public class multiplexing{
public static void main(String[] args)throws Exception{
BufferedInputStream in1=null;
BufferedOutputStream out1=null;
BufferedInputStream in2=null;
BufferedOutputStream out2=null;
BufferedInputStream in3=null;
BufferedOutputStream out3=null;
Queue<packet> queue = new Queue<packet>();
packet p = new packet();
//interface for select file
//select first file
try{
//-----------------------------------------------------
//check the request packet type
if(p.typefile == 1)
{
//open all file
JFileChooser chooser1 = new JFileChooser();
File f1 = new File(new File("").getCanonicalPath());
chooser1.setSelectedFile(f1);
chooser1.showOpenDialog(null);
File curFile1 = chooser1.getSelectedFile();
//select second file
JFileChooser chooser2 = new JFileChooser();
File f2 = new File(new File("").getCanonicalPath());
chooser2.setSelectedFile(f2);
chooser2.showOpenDialog(null);
File curFile2 = chooser2.getSelectedFile();
//select third file
JFileChooser chooser3 = new JFileChooser();
File f3 = new File(new File("").getCanonicalPath());
chooser3.setSelectedFile(f3);
chooser3.showOpenDialog(null);
File curFile3 = chooser3.getSelectedFile();
//}
in1=new BufferedInputStream(new FileInputStream(curFile1));
in2=new BufferedInputStream(new FileInputStream(curFile2));
in3=new BufferedInputStream(new FileInputStream(curFile3));
String filename1 = curFile1.getName();
String[] str1 = filename1.split("\\.");
String filetype1 = str1[str1.length - 1];
String fullfilename1 = "copyOf"+filename1+"."+filetype1;
p.IDnumber = filename1;
out1=new BufferedOutputStream(new FileOutputStream(fullfilename1));
String filename2 = curFile2.getName();
String[] str2 = filename2.split("\\.");
String filetype2 = str2[str2.length - 1];
String fullfilename2 = "copyOf"+filename2+"."+filetype2;
p.IDnumber = filename2;
out2=new BufferedOutputStream(new FileOutputStream(fullfilename2));
String filename3 = curFile3.getName();
String[] str3 = filename3.split("\\.");
String filetype3 = str3[str3.length - 1];
String fullfilename3 = "copyOf"+filename3+"."+filetype3;
p.IDnumber = filename3;
out3=new BufferedOutputStream(new FileOutputStream(fullfilename3));
//-----------------------------------------------------
//change the request packet type
p.typefile = 2;
}
//-----------------------------------------------------
//check the request packet type
//if(p.typefile == 2)
//{
while(p.typefile == 2)
{
byte[] b=new byte[p.getdataSize()];
boolean eof = false;
while ((p.datafield=in1.read(b))!=-1)
{
queue.enqueue(p);
//set the maximum size for queue
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
//System.out.println(c.IDnumber);
out1.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
while ((p.datafield=in2.read(b))!=-1)
{
queue.enqueue(p);
//set the maximum size for queue
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
out2.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
while ((p.datafield=in3.read(b))!=-1)
{
queue.enqueue(p);
//set the maximum size for queue
if(queue.size()>=20)
{
eof = true;
}
else if(!eof)
{
packet c = queue.dequeue();
//System.out.println(c.IDnumber);
out3.write(b,0,c.getdatafield());
if(queue.isEmpty())
{
eof = false;
}
}
break;
}
out1.flush();
out2.flush();
out3.flush();
}
p.typefile = 3;
//}
}
catch(Exception e){
e.printStackTrace();
}
finally{
if(p.typefile == 3)
if (in1!=null){
try{
in1.close();
}
catch(Exception e){}
}
if (out1!=null){
try{
out1.close();
}
catch(Exception e){}
}
if (in2!=null){
try{
in2.close();
}
catch(Exception e){}
}
if (out2!=null){
try{
out2.close();
}
catch(Exception e){}
}
if (in3!=null){
try{
in3.close();
}
catch(Exception e){}
}
if (out3!=null){
try{
out3.close();
}
catch(Exception e){}
}
}
// }
}
public static boolean compareFile(String file1, String file2) {
return compareFile(new File(file1), new File(file2));
}
public static boolean compareFile(File file1, File file2) {
BufferedInputStream in4 = null, in5 = null;
try {
in4 = new BufferedInputStream(new FileInputStream(
file1));
in5 = new BufferedInputStream(new FileInputStream(
file2));
int i;
while ( (i = in4.read()) != -1) {
if (i != in5.read()) {
return false;
}
}
if (in5.read() != -1) {
return false;
}
return true;
}
catch (FileNotFoundException ex) {
System.out.println("File not found!");
}
catch (IOException ex) {
System.out.println("IOException!");
}
finally {
try {
in4.close();
in5.close();
}
catch (IOException ex1) {
System.out.println("IOException when closing!");
}
}
return false;
}}
class packet
{
//
int typefile;
String IDnumber;
byte dataSize;
int datafield;
public packet()
{
typefile = 1;
IDnumber = "";
dataSize = 80;
datafield = 0;
}
public void settype(int t)
{
typefile = t;
}
public int gettype()
{
return typefile;
}
public byte getdataSize()
{
return dataSize;
}
public void setID(String filename)
{
IDnumber = filename;
}
public String getID()
{
return IDnumber;
}
public void setdatafield(int d)
{
datafield = d;
}
public int getdatafield()
{
return datafield;
}
}
总归谢谢大家的帮忙哦!~!