Problem with IO objects...variable might not have been initialized?

I dont get why its wrong, i've initialized both objects and variables, i dont know why it gives me that error T__T.

Program is supposed to compare both text archives and see if they are equal in a byte per byte way.

import java.io.*;

publicclass TestIO

{

publicstaticvoid main(String[] args)

throws IOException{

int i = 0, j = 0;

FileInputStream data1;

FileInputStream data2;

try{

try

{

data1 =new FileInputStream(args[0]);

}catch(FileNotFoundException exc){

System.out.println(args[0] +" File Not Found!");

return;

}

try

{

data2 =new FileInputStream(args[1]);

}catch(FileNotFoundException exc){

System.out.println(args[1] +" File Not Found!");

return;

}

}catch(ArrayIndexOutOfBoundsException exc){

System.out.println("Usage: java TestIO File1.java File2.java");

}

try{

do{

i = data1.read();

j = data2.read();

if(i != j)break;

}while(i != -1 && j != -1);

}catch(IOException exc){

System.out.println("File Error!");

}

if (i != j)

{

System.out.println("Files Differ");

}

else

{

System.out.println("Files are the same");

}

data1.close();

data2.close();

}

}

Help is appreciated, thanks in advance ^_^ .

[3103 byte] By [Rix87a] at [2007-11-27 8:47:47]
# 1

import java.io.*;

public class FileDiff {

public static boolean diff(File file1, File file2) throws IOException {

BufferedInputStream in1 = new BufferedInputStream(new FileInputStream(file1));

try {

return diff(in1, file2);

} finally {

in1.close();

}

}

public static boolean diff(InputStream in1, File file2) throws IOException {

BufferedInputStream in2 = new BufferedInputStream(new FileInputStream(file2));

try {

return diff(in1, in2);

} finally {

in1.close();

}

}

public static boolean diff(InputStream in1, InputStream in2) throws IOException {

int b1, b2;

while ((b1 = in1.read()) != -1 & (b2 = in2.read()) != -1 ) {

if (b1 != b2)

return true;

}

return b1 != b2;

}

public static void main(String[] args) {

if (args.length != 2) {

System.err.println("Usage: java FileDiff file1 file2");

} else {

try {

System.out.println(diff(new File(args[0]), new File(args[1])));

} catch(IOException e) {

System.out.println(e.getMessage());

}

}

}

}

BigDaddyLoveHandlesa at 2007-7-12 20:53:29 > top of Java-index,Java Essentials,New To Java...
# 2

Yea, thanks for that code that i don't understand much due to my newbiness on java :D .

I was expecting more of like a simple fix on my code, since its almost all good, except for those "variable...bla-bla" annoying errors that i just can't figure out why it's wrong T_T .

But it's alright if it can't be solved the way i want it to be!

Anyways, thanks in advance for at least viewing the problem i'm posting ^_^ .

Rix87a at 2007-7-12 20:53:29 > top of Java-index,Java Essentials,New To Java...
# 3

> Yea, thanks for that code that i don't understand

> much due to my newbiness on java :D .

Here's a simple example.

Object o = null;

if(someCondition)

{

o = new Object();

}

System.out.println(o.toString());

In this code snippet, if someCondition is false, then o will not get initialized, which will cause a NPE in the last line. This is why your compiler is complaining.

CaptainMorgan08a at 2007-7-12 20:53:29 > top of Java-index,Java Essentials,New To Java...
# 4
To able that last comment to the original code, if the command line argument array has too few elements, you print the usage message:Usage: java TestIO File1.java File2.javabut! you continue on and try to read data anyway.
BigDaddyLoveHandlesa at 2007-7-12 20:53:29 > top of Java-index,Java Essentials,New To Java...
# 5
Thanks for the tips! it worked now ^_^, now i can advance on this book...**** Herbert Schildt :X
Rix87a at 2007-7-12 20:53:29 > top of Java-index,Java Essentials,New To Java...