R
Roger Redford
Dear experts,
I'm trying to learn java on my own. I picked up a
sample online, but it is not compiling right:
------------------------------------------------
import java.io.*;
public class FileInfo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
File f = new File(args);
if (f.exists()) {
System.out.println("getName: " + f.getName());
System.out.println("getPath: " + f.getPath());
System.out.println("getAbsolutePath: " + f.getAbsolutePath());
try {
System.out.println("getCanonicalPath: " + f.getCanonicalPath());
}
catch (IOException e) {
}
System.out.println("getParent: " + f.getParent());
if (f.canWrite()) System.out.println(f.getName() + " is writable.");
if (f.canRead()) System.out.println(f.getName() + " is readable.");
if (f.isFile()) {
System.out.println(f.getName() + " is a file.");
}
else if (f.isDirectory()) {
System.out.println(f.getName() + " is a directory.");
}
else {
System.out.println("What is this?");
}
if (f.isAbsolute()) {
System.out.println(f.getName() + " is an absolute path.");
}
else {
System.out.println(f.getName() + " is not an absolute path.");
}
try {
System.out.println("Last Modified" + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
System.out.println(f.getName() + " is " + f.length() + " bytes.");
}
catch (IOException e) {
}
}
else {
System.out.println("I'm sorry. I can't find the file " + args);
}
}
} /* main */
} /* class */
------------------------------------------------
Javac -classpath %CLASSPATH%;. FileInfo.java
FileInfo.java:44: exception java.io.IOException is never thrown in body of
corresponding try statement
catch (IOException e) {
^
------------------------------------------------
And yet, from what I have looked at, it looks the same as
other try/catch/finally.
I have fudged it this way, to get it compile:
------------------------------------------------
try {
System.out.println(args[0]);
System.out.println("Last Modified: " + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
}
finally {
System.out.println("finally");
}
}
/*
try {
System.out.println("Last Modified" + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
catch (IOException e) {
}
}
*/
}
else {
System.out.println("I'm sorry. I can't find the file " + args);
}
}
------------------------------------------------
But, how to get the original to work with /try/catch/finally?
Thanks a lot!
I'm trying to learn java on my own. I picked up a
sample online, but it is not compiling right:
------------------------------------------------
import java.io.*;
public class FileInfo {
public static void main(String[] args) {
for (int i = 0; i < args.length; i++) {
File f = new File(args);
if (f.exists()) {
System.out.println("getName: " + f.getName());
System.out.println("getPath: " + f.getPath());
System.out.println("getAbsolutePath: " + f.getAbsolutePath());
try {
System.out.println("getCanonicalPath: " + f.getCanonicalPath());
}
catch (IOException e) {
}
System.out.println("getParent: " + f.getParent());
if (f.canWrite()) System.out.println(f.getName() + " is writable.");
if (f.canRead()) System.out.println(f.getName() + " is readable.");
if (f.isFile()) {
System.out.println(f.getName() + " is a file.");
}
else if (f.isDirectory()) {
System.out.println(f.getName() + " is a directory.");
}
else {
System.out.println("What is this?");
}
if (f.isAbsolute()) {
System.out.println(f.getName() + " is an absolute path.");
}
else {
System.out.println(f.getName() + " is not an absolute path.");
}
try {
System.out.println("Last Modified" + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
System.out.println(f.getName() + " is " + f.length() + " bytes.");
}
catch (IOException e) {
}
}
else {
System.out.println("I'm sorry. I can't find the file " + args);
}
}
} /* main */
} /* class */
------------------------------------------------
Javac -classpath %CLASSPATH%;. FileInfo.java
FileInfo.java:44: exception java.io.IOException is never thrown in body of
corresponding try statement
catch (IOException e) {
^
------------------------------------------------
And yet, from what I have looked at, it looks the same as
other try/catch/finally.
I have fudged it this way, to get it compile:
------------------------------------------------
try {
System.out.println(args[0]);
System.out.println("Last Modified: " + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
}
finally {
System.out.println("finally");
}
}
/*
try {
System.out.println("Last Modified" + f.lastModified());
System.out.println(f.getName() + " is " + f.length() + " bytes.");
catch (IOException e) {
}
}
*/
}
else {
System.out.println("I'm sorry. I can't find the file " + args);
}
}
------------------------------------------------
But, how to get the original to work with /try/catch/finally?
Thanks a lot!