You are on page 1of 2

import java.io.

File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author lucat
*/
public class Laboratorio03 {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String path="C:\\Users\\lucat\\Desktop\\Luca\\Scuola\\Luca.txt";
File f=new File(path);
Scanner sc=new Scanner(System.in);
FileReader fr;
FileWriter fw;

ArrayList <String> lista=new ArrayList <String>();


String s="";
int contatore=0;
try {
fr=new FileReader(f);
try {
while(true){
int a=fr.read();
if(a==32){
lista.add(s);
s="";
}else if(a==-1){
lista.add(s);
break;
}else{
s=s+(char)a;
}
}
} catch (IOException ex) {
Logger.getLogger(Laboratorio03.class.getName()).log(Level.SEVERE,
null, ex);
}

} catch (FileNotFoundException ex) {


Logger.getLogger(Laboratorio03.class.getName()).log(Level.SEVERE, null,
ex);
}

String[]s1=new String[lista.size()];
for(int i=0;i<lista.size();i++){
s1[i]=lista.get(i);
}

System.out.println("Inserisci la parola da cercare");


String parola=sc.next();
System.out.println("Inserisci la parola sostitutiva");
String parola1=sc.next();
boolean b=true;
for(int i=0;i<lista.size();i++){
if(s1[i].equals(parola)){
b=false;
s1[i]=parola1;
System.out.println("La parola si trova al posto "+i);

}
}
if(b){
System.out.println("Non sono state trovate parole così");
}else{

System.out.println("Tutte le parole sono state sostituite");


}
try {
fw=new FileWriter(f);
for(int i=0;i<s1.length;i++){
fw.write(s1[i]+" " );
fw.flush();
}
} catch (IOException ex) {
Logger.getLogger(Laboratorio03.class.getName()).log(Level.SEVERE, null,
ex);
}

You might also like