DocumentIdentifier.java
package edu.odu.cs.cs350;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
/**
* class identifies the type of a document file
* and determines whether text files contain only ASCII characters.
*/
public class DocumentIdentifier {
/**
* Enumeration for supported document types.
*/
public enum DocumentType {
/**
* PDF document type
*/
PDF,
/**
* ASCII text document type
*/
ASCII_TEXT,
/**
* Unsupported document type
*/
UNSUPPORTED
}
/**
* Identifies the type of document based on its file extension.
*
* @param file the file to identify
* @return DocumentType.PDF if the file is a PDF,
* DocumentType.ASCII_TEXT if the file is a text file,
* or DocumentType.UNSUPPORTED otherwise
*/
public static DocumentType identify(File file) {
String name = file.getName().toLowerCase();
if (name.endsWith(".pdf")) {
return DocumentType.PDF;
} else if (name.endsWith(".txt")) {
return DocumentType.ASCII_TEXT;
} else {
return DocumentType.UNSUPPORTED;
}
}
/**
* Checks whether a given text file contains only ASCII characters.
*
* @param file the text file to check
* @return true if the file contains only ASCII characters (0–127), false otherwise
*/
public static boolean isTextDocument(File file) {
try (FileInputStream inputDocument = new FileInputStream(file)) {
int b;
while ((b = inputDocument.read()) != -1) {
if (b > 127) return false;
}
return true;
} catch (IOException e) {
return false;
}
}
/**
* Main method for identifying a document's type from the command line.
* Usage: java DocumentIdentifier <file>
*
* @param args the command-line arguments, where args[0] is the file path
*/
public static void main(String[] args) {
if (args.length == 0) {
System.out.println("Enter File: java DocumentIdentifier <file>");
return;
}
File file = new File(args[0]);
if (!file.exists()) {
System.out.println("File not found: " + file.getName());
return;
}
DocumentType type = identify(file);
switch (type) {
case PDF:
System.out.println(file.getName() + " : PDF Document");
break;
case ASCII_TEXT:
boolean ascii = isTextDocument(file);
if (ascii) {
System.out.println(file.getName() + " : ASCII Text Document");
} else {
System.out.println(file.getName() + " : Text file contains non-ASCII characters");
}
break;
default:
System.out.println(file.getName() + " : Unsupported file type");
}
}
}