Question: **** IN JAVA **** Write a java class called CheckFile, that takes in a command line argument filename, and prints whether the file is a
**** IN JAVA ****
Write a java class called CheckFile, that takes in a command line argument filename, and prints whether the file is a text file or a binary file.
Check the file contents for a null (0) byte value, and if you find that value, assume it is binary.
If you don't find that value, assume it is text. Look for the first instance of the newline character (' '). If it is preceded by a carriage return (' '), then it is a Windows text file. Otherwise it is a Unix text file. Print which kind of text file it is.
CheckFile.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.*;
import java.util.*;
/**
*
*/
public class CheckFile {
public static void main(String[] args) {
if (args.length != 1){
System.out.println("usage: java CheckFile filename");
System.exit(1);
File file = new File (args[0]);
if (!file.exists()){
System.err.println("File not found");
System.exit(2);
}
try{
DataInputStream fin = new DataInputStream(new FileInputStream(file));
int b;
do {
b = fin.read();
if (b == 0){
System.out.println("Binary file.");
}
} while (b != 0);
int c;
c = fin.readByte();
} catch (IOException e) {
System.out.println("ah");
}
}
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
