Voglio leggere dal file criteria.txt
, per tokenizzare e aggiungere alla fine dello stesso file i token. Il programma genera un'eccezione: No file found!
Non so dove sia il mio errore. Qualsiasi suggerimento mi aiuterebbe. Grazie in anticipo!Lettura e scrittura da e verso lo stesso file in Java
Ecco il mio codice:
import java.io.*;
import java.util.StringTokenizer;
public class Test
{
private FileReader fr;
private BufferedReader br;
private FileWriter fw;
private BufferedWriter bw;
private StringTokenizer strtok;
private String s;
//constructor
public Test()
{
try
{
fw=new FileWriter("criteria.txt", true);
bw=new BufferedWriter(fw);
try
{
fr=new FileReader("criteria.txt");
br=new BufferedReader(fr);
while((s=br.readLine())!=null)
{
strtok=new StringTokenizer(s," ");
while(strtok.hasMoreTokens())
{
bw.write("\n"+strtok.nextToken());
}
br.close();
}
}
catch(FileNotFoundException e)
{
System.out.println("File was not found!");
}
catch(IOException e)
{
System.out.println("No file found!");
}
bw.close();
}
catch(FileNotFoundException e)
{
System.out.println("Error1!");
}
catch(IOException e)
{
System.out.println("Error2!");
}
}
public static void main(String[] args)
{
Test t=new Test();
}
}
Se si utilizza Eclipse, il file deve trovarsi nella directory principale del progetto. –
È necessario stampare la traccia dello stack per trovare la riga di codice offendente. – Vulcan
controlla la directory dei file – Abubakkar