Salve a tutti
Premetto che sto studiando java da pochissimo, ma purtroppo ho l'esigenza di
fare dele cose abbastanza "complesse" per uno che ha cominciato da poco (maledetta tesi -.-').
In ogni caso, Vi spiego qual'è il mio problema sperando di essere abbastanza chiaro....e sperando
che possiate aiutarmi.
Obiettivo nr.1 : devo fare in modo che un utente possa selezionare un file .txt dal proprio pc e che il
contenuto di questo file venga bufferizzato in una variabile e visualizzato.
Per la selezione del file uso la classe JFileChooser e in questo modo riesco ad avere tutte info
che mi interessano del file, compreso il path:
//************************************************** **************************
// display information about file user specifies
public void analyzePath()
{
// create File object based on user input
File name = getFile();
if ( name.exists() ) // if name exists, output information about it
{
// display file (or directory) information
outputArea.setText( String.format(
"%s%s\n%s\n%s\n%s\n%s%s\n%s%s\n%s%s\n%s%s\n%s% s",
name.getName(), " exists",
( name.isFile() ? "is a file" : "is not a file" ),
( name.isDirectory() ? "is a directory" :
"is not a directory" ),
( name.isAbsolute() ? "is absolute path" :
"is not absolute path" ), "Last modified: ",
name.lastModified(), "Length: ", name.length(),
"Path: ", name.getPath(), "Absolute path: ",
name.getAbsolutePath(), "Parent: ", name.getParent() ) );
if ( name.isDirectory() ) // output directory listing
{
String directory[] = name.list();
outputArea.append( "\n\nDirectory contents:\n" );
for ( String directoryName : directory )
outputArea.append( directoryName + "\n" );
} // end else
} // end outer if
else // not file or directory, output error message
{
JOptionPane.showMessageDialog( this, name +
" does not exist.", "ERROR", JOptionPane.ERROR_MESSAGE );
} // end else
} // end method analyzePath
//************************************************** ****************************
Per visualizzare un txt,ipoteticamente potrei usare una classe simile a questa:
//************************************************** ****************************
public class leggi {
public static void main(String [] args) throws IOException {
// incapsula in BufferedReader un file aperto in lettura
BufferedReader filebuf =
new BufferedReader(new FileReader("copyread.txt"));
/* equivale alla coppia di istruzioni:
*
* FileReader filein = new FileReader("copyread.txt");
* BufferedReader filebuf = new BufferedReader(filein);
*/
String nextStr;
nextStr = filebuf.readLine(); // legge una riga del file
while (nextStr != null){
System.out.println(nextStr); // visualizza la riga
nextStr = filebuf.readLine(); // legge la prossima riga
}
filebuf.close(); // chiude il file
}
}
//************************************************** ***************************
il problema è che non riesco a passare il path del file che ho selezionato a Filereader.
Come faccio??!?! ometto di scrivere tutte le vie che ho tentato.
Tnks per l'aiuto!!!!

Rispondi quotando