I'm a freshwoman at Systems Information, new to Java and programming and was assigned this task by my professor. My job is to read characters from a text file (file details in the bottom), recognizing each char as an integer. I should extend a class that will make bitwise operations to each char as to change them. For example, 'a' value is 97. Then it will be changed into something else, for example, 112, which is the number for 'p'. Then I have to print this modified character in a new file. I should send it by Thursday but I’m trying to make it cleaner and more efficient in the meanwhile. I’m working on handling “file not found” and “file can’t be read” with try/catch as we just learned it and maybe the professor would prefer that these exceptions would be handled that way and I’ll see if I can keep line breaks (-1).
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Codigo extends Codificador {
@Override
void codifica(String[] args) {
File filein = new File(args[0]); // File to be read and encoded
File fileout = new File(args[1]); // File that will receive encoded characters
FileInputStream fis = null;
FileOutputStream fos = null;
if (!filein.exists()) { // Tests if filein exists in this folder
System.out.println(args[0] + " não encontrado."); // Returns message "File* not found" if needed.
return;
}
if (!(filein.isFile() && filein.canRead())) { // Tests if filein can be read
System.out.println(filein.getName() + " não pode ser lido."); // Returns "File* can't be read" if needed.
return;
}
try {
fis = new FileInputStream(filein);
fos = new FileOutputStream(fileout);
int c; // Character read from filein
int r; // Character that will store encoded character c
while ((c = fis.read()) != -1) { // Each character from filein is read and stored in c
// While the program doesn't fine a line break (-1)
r = ((c & 0x0010) >> 4) | ((c & 0x0001) << 4) | // Bitwise operations are realized to encode c
((c & 0x1000) >> 3) | ((c & 0x0200) << 3) | // This encoded character is stored in r
(c & 0xEDEE);
System.out.print((char)r); // Checks if was encoded as expected
fos.write(r); // Writes encoded character in fileout
}
} catch (IOException e) {
System.err.println("Caught IOException: " + e.getMessage());
}
finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
} // Closes FileInputStream resources
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
} // Closes FileOutputStream resources
}
}
}
}
This is the class that it extends:
public abstract class Codificador {
/** Recebe o nome do arquivo de entrada em args[0] e o nome do arquivo de
saída em args[1]. Pressupõe que os arquivos estão em UTF-8. */
abstract void codifica (String[] args);
/** codifica o caracter armazenado em c, troca os bits de lugar e
retorna o valor.
@param c: caracter a codificar
@return : caracter em código
*/
int codifica (int c) {
int r;
r = ((c & 0x0010) >> 4) | ((c & 0x0001) << 4) |
((c & 0x1000) >> 3) | ((c & 0x0200) << 3) |
(c & 0xEDEE);
return r;
}
}
And here is a class to test it :
public class TestaCodigo {
public static void main (String[] args) {
Codigo cod = new Codigo ();
cod.codifica (args);
}
}
To test my code I should type
java TestaCodigo inputfile.txt outputfile.txt
Then, by running
java TestaCodigo outputfile.txt newfile.txt
it should create newfile.txt which is a copy of inputfile.txt
These last ones were made by my professor, so I can't make any changes to them.
Any suggestions on code and/or documentation?
#codifica(int c)
that is supposed to transform the character \$\endgroup\$throws
declarations when overriding a method from an abstractclass
afaik, thus you have to handle theIOException
\$\endgroup\$throw new AnotherException(e)
is the best handling you can do.System.err.println
is the third worst (just after ignoring and usingSystem.out
). \$\endgroup\$IOError
is impossible. Propably should beIllegalArgumentException
\$\endgroup\$