I'm writing a program to read two files supplied as arguments in the command line, convert them from list to char[][], and display their contents to the user.
I'm trying to practice exception-handling and javadocs, so I'm not interested in using a library. Also, this will be part of a larger program, hence the conversions happening, but I just want to focus on the I/O part for now. All comments are welcome!
public class Utilities {
/**
* This class must not be instantiated.
*/
private Utilities() {}
/**
* Reads a file and returns a <code>List<String></code>.
*
* @param file path to the file
* @return a <code>List<String></code> containing every line from the file
* @throws AccessDeniedException if access rights are insufficient
* @throws EOFException if the file is empty
* @throws IOException I/O exception
* @throws IllegalArgumentException (unchecked) if the path string cannot be converted
* to a <code>Path</code>
* @throws NoRegularFileException if the file is not a regular file
* @throws NoSuchFileException if the file does not exist
* @throws SecurityException (unchecked) In the case of the default provider,
* and a security manager is installed, the <code>checkRead</code> is invoked
* to check read access to the file.
*/
public static List<String> readFile(String file)
throws NoSuchFileException, EOFException, IOException {
Path p = Paths.get(file);
if (!Files.exists(p)) {
throw new NoSuchFileException(file);
} else if (!Files.isRegularFile(p)) {
throw new NoRegularFileException(file);
} else if (!Files.isReadable(p)) {
throw new AccessDeniedException(file);
} else if (Files.size(p) == 0) {
throw new EOFException(file);
}
return Files.readAllLines(p);
}
/**
* Converts a <code>List</code> of any type to a <code>char[][]</code>.
*
* @param <T> the type of the element
* @param l a <code>List</code>
* @return a <code>char[][]</code>. If the <code>List</code> is <code>null</code>,
* returns <code>null</code>. If an element of the <code>List</code> is
* <code>null</code>, the corresponding array is set to <code>null</code>.
*/
public static <T> char[][] listToCharArray(List<T> l) {
if (l == null) {
return null;
}
char[][] arr = new char[l.size()][];
{
int i = 0;
for (T t : l) {
if (t == null) {
arr[i++] = null;
} else {
arr[i++] = t.toString().toCharArray();
}
}
}
return arr;
}
/**
* Displays a <code>char[][]</code>.
*
* @param arr a <code>char[][]</code>
*/
public static void display(char[][] arr) {
for (char[] line : arr) {
for (char c : line) {
System.out.print(c);
}
System.out.println();
}
}
}
class NoRegularFileException extends FileSystemException {
public NoRegularFileException(String file) {
super(file);
}
}
public class MyProgram {
public static void main(String[] args) {
try {
if (args.length != 2) {
System.err.println("The proper use is: java MyProgram file1.txt file2.txt");
return;
}
List<List<String>> files = new ArrayList<>();
for (String s : args) {
try {
files.add(Utilities.readFile(s));
} catch (NoSuchFileException e) {
System.err.printf("File %s does not exist%n", e.getMessage());
return;
} catch (NoRegularFileException e) {
System.err.printf("File %s is not a regular file%n", e.getMessage());
return;
} catch (AccessDeniedException e) {
System.err.printf(
"Access rights are insufficient to read file %s%n", e.getMessage()
);
return;
} catch (EOFException e) {
System.err.printf("File %s is empty%n", e.getMessage());
return;
}
}
char[][] file1 = Utilities.listToCharArray(files.get(0));
char[][] file2 = Utilities.listToCharArray(files.get(1));
Utilities.display(file1);
Utilities.display(file2);
} catch (Exception e) {
System.err.printf("Error: %s%n", e.getMessage());
}
}
}
NoRegularFileException
? I can't find that class in the Java API. \$\endgroup\$