Ver Mensaje Individual
  #1 (permalink)  
Antiguo 30/09/2011, 02:33
xictor
 
Fecha de Ingreso: septiembre-2011
Mensajes: 25
Antigüedad: 12 años, 7 meses
Puntos: 3
Cambiar una palabra por otra de un txt y guardarlo en otro txt

Buenas a todos, soy nuevo en java y estoy intentando hacer un programa que:
1. me abra un archivo de txt,
2. lo recorra
3. cambie una palabra por otra
4. el resultado lo guarde en un nuevo txt

Necesito ayuda porque he leído mucho sobre los stringtokenizier y los streamsbuffered... he probado de todo y lo máximo que he sacado es esto, que no me funciona.

Código PHP:
import java.io.*;
import java.util.StringTokenizer;

public class 
prueba {
    public 
void prueba(){}
        
    
    public 
void PropiedadesDelFichero(){
        
File f = new File("C:/Documents and Settings/viclopez/Desktop/pruebaa.txt");

if (
f.canRead())
   
System.out.println("El fichero existe y se puede leer");

if (
f.canWrite())
    
System.out.println("El fichero existe y se puede escribir en él");

if (
f.canExecute())
    
System.out.println("El fichero existe y se puede ejecutar");

    }
    
  public static 
void SustituirMorraña() throws FileNotFoundExceptionIOException{
      
      
try { 
        
FileReader fr = new FileReader"C:/Documents and Settings/viclopez/Desktop/pruebaa.txt" );
     
BufferedReader entrada = new BufferedReaderfr ); 

     
String linea
      
//recorro las lineas del txt hasta que sea null
      
while((linea entrada.readLine())!= null){  
      
StringTokenizer st = new StringTokenizer(linea);  
      
//recorro todos los token
      
while (st.hasMoreTokens()) {  
            
String token=st.nextToken(); 
             
System.out.println("a ver que sale " token);
             
//lo reemplazo por lo que me interesa
           
String nuevotexto token.replaceAll("hola","");
           
//SequenceInputStream sis = new SequenceInputStream(nuevotexto);
                
System.out.println("a ver:"nuevotexto);
           
           try {
       
// st2 = new StringTokenizer(nuevotexto);
       //System.out.println("a ver si sale el st2:" + st2);
       
       
FileWriter fw = new FileWriter("C:/Documents and Settings/viclopez/Desktop/prueba4.txt"); 
       
BufferedWriter bw = new BufferedWriter(fw);
       
       
PrintWriter pw = new PrintWriter(bw);
       
        
pw.println(nuevotexto);
        
System.out.println("NUEVO: " pw);
        
pw.close();
        
        }
catch(
java.io.IOException ioex) {
    
  
System.out.println("se presento el error: "+ioex.toString());
}
       
            
           
                
                
//falta guardar "texto" un archivo de texto
            
      
}            
     } 
}       catch (
IOException e) { 
        
System.out.println("Error " e); 
}  
        }
  public static 
void main (String [] argsthrows FileNotFoundExceptionIOException{
      
SustituirMorraña();
    }


Por favor ponedme el programa que funcione, he leído mucho y es que no me sale =(

Gracias