Δημοσιεύτηκε: 16 Απρ 2012, 15:40
εγώ το έκανα έτσι
- Μορφοποιημένος Κώδικας: Επιλογή όλων
-
package musiclibrary;
import java.io.File;
import java.io.FileNotFoundException;
import java.lang.SecurityException;
import java.util.*;
public class Save_Load_Library {
private Formatter output;
private Scanner input;
public void WriteFile( String file, String[] array ) {
try
{
output = new Formatter(file);
}
catch ( SecurityException securityException )
{
System.err.println( "You dont have the permission to open the file." );
}
catch ( FileNotFoundException filenotFoundException )
{
System.err.println( "File not found." );
}
for ( int i = 0; i < array.length; i++ ) {
output.format("%s \n", array[i]);
}
output.close();
}
public ArrayList<String> readFile( String path ) {
ArrayList< String > rarray = new ArrayList();
try
{
input = new Scanner( new File( path ) );
}
catch ( FileNotFoundException fileNotFoundException )
{
System.err.println( "Error opening file." );
}
try
{
while ( input.hasNext() ) {
rarray.add( input.nextLine() );
}
}
catch ( NoSuchElementException elementException )
{
System.err.println( "File improperly formed." );
input.close();
}
catch ( IllegalStateException stateException )
{
System.err.println( "Error reading from file." );
}
catch ( NullPointerException nullPointerException )
{
System.err.println( "Failed." );
}
return rarray;
}
public void closeFile() {
if ( output != null )
output.close();
}
}