E possivel manipular mais de um arquivo conconrrentemente

3 respostas
F

galera tenho um problema, eu leio varias listas ao mesmo tempo e comparo seus elementos, e depois tento salvar em arquivo, o problema e quando ele tenta salvar no segundo arquivo aberto ele nao consegue, so salva no 1 arquivo todos os dados.
pergunta, E possivel manipular varios arquivos abertos de uma vez so???

Falow!!!

3 Respostas

dkotvan

Sim. É possível. De que forma você está gravando nos arquivos? Um trecho do código ajudaria.

P

Sim, já fiz instâncias simultâneas em mais de um arquivo.
E funcionou, o qu vc está utiliazando para manipular esses arquivos? IO?

F

Aqui o codgio

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Vector;

/*
 * Created on May 26, 2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */

/**
 * @author felipe
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Arquivo_mais {

	public static void main(String[] args) throws IOException {
	
		FileReader reader2002 = new FileReader("C:\Documents and Settings\beto\Desktop\2002.txt");//empresa
		BufferedReader leitor2002 = new BufferedReader(reader2002);
		
		FileReader reader2003 = new FileReader("C:\Documents and Settings\beto\Desktop\2003.txt");//empresa
		BufferedReader leitor2003 = new BufferedReader(reader2003);
		
		FileReader reader2004 = new FileReader("C:\Documents and Settings\beto\Desktop\2004.txt");//empresa
		BufferedReader leitor2004 = new BufferedReader(reader2004);
		
		FileReader reader2005 = new FileReader("C:\Documents and Settings\beto\Desktop\2005.txt");//empresa
		BufferedReader leitor2005 = new BufferedReader(reader2005);
		
		FileReader reader2006 = new FileReader("C:\Documents and Settings\beto\Desktop\2006.txt");//empresa
		BufferedReader leitor2006 = new BufferedReader(reader2006);
		
		FileWriter writer = new FileWriter("C:\arquivo_teste_mais.txt");
		PrintWriter saida = new PrintWriter(writer,true);
				
		FileWriter writer1 = new FileWriter("C:\arquivo_teste_mais1.txt");
		PrintWriter saida1 = new PrintWriter(writer,true);
		
		
		FileWriter writer2 = new FileWriter("C:\arquivo_teste_mais2.txt");
		PrintWriter saida2 = new PrintWriter(writer,true);
		
		FileWriter writer3 = new FileWriter("C:\arquivo_teste_mais3.txt");
		PrintWriter saida3 = new PrintWriter(writer,true);
		
		FileWriter writer4 = new FileWriter("C:\arquivo_teste_mais4.txt");
		PrintWriter saida4 = new PrintWriter(writer,true);
		
		Vector v2002 = new Vector();
		Vector v2003 = new Vector();
		Vector v2004 = new Vector();
		Vector v2005 = new Vector();
		Vector v2006 = new Vector();
		
		String linha;
		int num;
		String cnpj;
		
		while((linha = leitor2002.readLine())!=null) {
		  	cnpj=linha.substring(0,14);
		  	v2002.add(cnpj.trim());
		}
		
		while((linha = leitor2003.readLine())!=null) {
		  	cnpj=linha.substring(0,14);
		  	v2003.add(cnpj.trim());
		}
		while((linha = leitor2004.readLine())!=null) {
		  	cnpj=linha.substring(0,14);
		  	v2004.add(cnpj.trim());
		}
		while((linha = leitor2005.readLine())!=null) {
		  	cnpj=linha.substring(0,14);
		  	v2005.add(cnpj.trim());
		}
		while((linha = leitor2006.readLine())!=null) {
		  	cnpj=linha.substring(0,14);
		  	v2006.add(cnpj.trim());
		}
		
		
		Vector l1 = new Vector();
		Vector l2 = new Vector();
		Vector l3 = new Vector();
		Vector l4 = new Vector();
		Vector l5 = new Vector();
		Vector l6 = new Vector();
		Vector l7 = new Vector();
		Vector l8 = new Vector();
		
		
		
		String aux=null;
		System.out.println("antes do for");
		for(int i=0;i<v2002.size();i++){
			aux=(String) v2002.get(i);
			if(v2003.contains(aux)&& v2004.contains(aux)&& v2005.contains(aux)&& v2006.contains(aux)){
				l1.add(aux); // todos os anos
				saida.println(aux);
			}
			else if(v2003.contains(aux)){
				l2.add(aux); // 2002 e 2003
				System.out.println(aux.toString()+" --> 2002 2003");
				//saida1.println(aux);
			}
			if(v2004.contains(aux)){
				if(l2.contains(aux)){ // 2002 2003 2004
					l6.add(aux);
					System.out.println(aux.toString()+" --> 2002 2003 2004");
				}
				else
					l3.add(aux);	// 2002 2004	
			}
			if(v2005.contains(aux)){
				if(l6.contains(aux)){ // 2002 2003 2004 2005
					l7.add(aux);
					System.out.println(aux.toString()+" --> 2002 2003 2004 2005");
				}
				else
					l4.add(aux); // 2002 2005
			}
			if(v2006.contains(aux)){
				if(l7.contains(aux))
				   l8.add(aux); // todos tb
				else
					l5.add(aux); //2002 2006
			}			
					
		}
	
		
	}
}

Se alguem souber???

Criado 29 de maio de 2006
Ultima resposta 29 de mai. de 2006
Respostas 3
Participantes 3