codice giusto
codice:
/* Copy a text file.
To use this program, specify the name
of the source file and the destination file.
For example, to copy a file called FIRST.TXT
to a file called SECOND.TXT, use the following
command line.
java CopyFile FIRST.TXT SECOND.TXT
*/
import java.io.*;
class Esempio {
public static void main(String args[]) throws IOException
{
int i;
FileInputStream fin = null;
FileOutputStream fout = null;
// First, confirm that both files has been specified.
if(args.length != 2) {
System.out.println("Usage: CopyFile from to");
return;
}
// Copy a File.
try {
// Attempt to open the files.
fin = new FileInputStream(args[0]);
fout = new FileOutputStream(args[1]);
do {
i = fin.read();
if(i != -1) fout.write(i);
} while(i != -1);
} catch(IOException e) {
System.out.println("I/O Error: " + e);
} finally {
try {
if(fin != null) fin.close();
} catch(IOException e2) {
System.out.println("Error Closing Input File");
}
try {
if(fout != null) fout.close();
} catch(IOException e2) {
System.out.println("Error Closing Output File");
}
}
}
}
codice sbagliato
codice:
/* Copy a text file.
_ _To use this program, specify the name
_ _of the source file and the destination file.
_ _For example, to copy a file called FIRST.TXT
_ _to a file called SECOND.TXT, use the following
_ _command line.
_ _java CopyFile FIRST.TXT SECOND.TXT
*/
import java.io.*;
class Esempio {
_ public static void main(String args[]) throws IOException_
_ {
_ _ int i;
_ _ FileInputStream fin = null;
_ _ FileOutputStream fout = null;
_ _ // First, confirm that both files has been specified.
_ _ if(args.length != 2) {
_ _ _ System.out.println("Usage: CopyFile from to");
_ _ _ return;
_ _ }
_ _ // Copy a File.
_ _ try {
_ _ _ // Attempt to open the files.
_ _ _ fin = new FileInputStream(args[0]);
_ _ _ fout = new FileOutputStream(args[1]);
_ _ _ do {
_ _ _ _ i = fin.read();
_ _ _ _ if(i != -1) fout.write(i);
_ _ _ } while(i != -1);
_ _ } catch(IOException e) {
_ _ _ System.out.println("I/O Error: " + e);
_ _ } finally {
_ _ _ try {
_ _ _ _ if(fin != null) fin.close();
_ _ _ } catch(IOException e2) {
_ _ _ _ System.out.println("Error Closing Input File");
_ _ _ }
_ _ _ try {
_ _ _ _ if(fout != null) fout.close();
_ _ _ } catch(IOException e2) {
_ _ _ _ System.out.println("Error Closing Output File");
_ _ _ }
_ _ }
_ }
}