import java.io.*;


class file3
{
	public static void main(String args[]) throws IOException
	{
		FileInputStream f1;
		int i;
		FileOutputStream f2;

		try
		{
			f1= new FileInputStream(args[0]);

		}catch(Exception e)
		{

		}

		try
		{
			f2= new FileOutputStream(args[1]);

		}catch(Exception e)
		{

		}
try{
		do
		{
			i=f1.read();
			if(i!=-1)
			f2.write(i);

		}while(i!=-1);
}catch(Exception e){};


		do
		{
			i=f1.read();
			System.out.print((char)i);

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

it is giving error that "f1 must not be initialized" and same f2 can not be ...please solve this error. when i run from CMD then it says file not found. it is not even geeting this file. i have done everything.it is not catching that there is file of this name

can you copy paste the exact error message? what parameters are you passing in the command line?

If line 14 throws an exception then you will get to line42 with f1 not initialised. Similarly for f2
ps NEVER do this:

catch(Exception e)
{
 
}

when you get any error of any kind in the try block you will see no error message of any kind, and will have no idea what happened. Display the error message in EVERY catch block, like this:

catch(Exception e)
{
   e.printStackTrace();
}

If line 14 throws an exception then you will get to line42 with f1 not initialised. Similarly for f2
ps NEVER do this:

catch(Exception e)
{
 
}

when you get any error of any kind in the try block you will see no error message of any kind, and will have no idea what happened. Display the error message in EVERY catch block, like this:

catch(Exception e)
{
   e.printStackTrace();
}

but it is saying that file3.java do't exist :'(

can you copy paste the exact error message? what parameters are you passing in the command line?

it is saying file3.java doesn't exist

that's an entire different problem than you described in your first post.
and, it's not really a copy paste of the error message you're getting.

besides, based on what you posted, we can't verify the name you've given your java file, so it might be possible that file3.java doesn't exist.

that's an entire different problem than you described in your first post.
and, it's not really a copy paste of the error message you're getting.

besides, based on what you posted, we can't verify the name you've given your java file, so it might be possible that file3.java doesn't exist.

C:\Users\gourav\java_pr\files3.java:32: error: variable f1 might not have been initialized
			i=f1.read();
			  ^
C:\Users\gourav\java_pr\files3.java:34: error: variable f2 might not have been initialized
			f2.write(i);
			^
C:\Users\gourav\java_pr\files3.java:45: error: variable f1 might not have been initialized
			i=f1.read();
			  ^
3 errors

Tool completed with exit code 1

this is error

Did you read my post where I explained that error. Did you change your program like I said?

Did you read my post where I explained that error. Did you change your program like I said?

yes sir! i have done that as u said :'( but in vain......

f1= new FileInputStream(args[0]);
If that call fails what do you want to do? Right now you carry on and try to read from f1 anyway. Obviously that's not going to work. Maybe you should consider exiting your program if you can't create the FileInputStream.

f1= new FileInputStream(args[0]);
If that call fails what do you want to do? Right now you carry on and try to read from f1 anyway. Obviously that's not going to work. Maybe you should consider exiting your program if you can't create the FileInputStream.

it is code from herbert scheildt. why is it so ? why call will fail ? i have rewrite full code u said. even after that it is saying same error i have posted above. f1 might not be initialized again and again.

Did you read my post where I explained that error. Did you change your program like I said?

but i have made many programs using inputstreams there is no problem. but it is the only one. :'( hey! one question! i have given stacktrace() then y it is not printing error ? it must print that. rather it is not even compiling this file. this must be run time error rather than compiling. right ?

It tries to open a file using a file name that is supplied as a parameter when the program is run.
The file name may be wrong. The file path may be wrong. You may not have read permission for the file. Any of these will make the call fail and throw an Exception.

then post the code as you have it now, otherwise, how do you expect us to know what's wrong with it?

e.printStackTrace()
prints complete details of any run-time error (Exception) that happens inside the try block when the program is executing.

then post the code as you have it now, otherwise, how do you expect us to know what's wrong with it?

import java.io.*;

class X
{
	public static void main(String args[]) throws IOException
	{
		FileInputStream f1;
		FileInputStream f3;
		FileOutputStream f2;
		int i;

		try
		{
			f1= new FileInputStream(args[0]);

		}catch(Exception e)
		{
			e.printStackTrace();
		}
		try
				{
					f3= new FileInputStream(args[1]);

				}catch(Exception e)
				{
					e.printStackTrace();
		}
		try
		{
			f2=new FileOutputStream(args[1]);

		}catch(Exception e)
		{
			e.printStackTrace();
		}

		do{
			i=f1.read();
			if(i!=-1)
			f2.write(i);
		}while(i!=-1);


		do{
			i=f3.read();
			System.out.print((char)i);

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

this is the code which has error.

C:\Users\gourav\Desktop\X.java:38: error: variable f1 might not have been initialized
			i=f1.read();
			  ^
C:\Users\gourav\Desktop\X.java:40: error: variable f2 might not have been initialized
			f2.write(i);
			^
C:\Users\gourav\Desktop\X.java:45: error: variable f3 might not have been initialized
			i=f3.read();
			  ^
3 errors

Tool completed with exit code 1

this is the error which is coming when i compile . now tell !

It tries to open a file using a file name that is supplied as a parameter when the program is run.
The file name may be wrong. The file path may be wrong. You may not have read permission for the file. Any of these will make the call fail and throw an Exception.

hey! but i will write the files name when it will compile.right ? but it is not compling sir!! java X file1.txt file2.txt i can give only when it will compile. but sir! it is not compiling. how can i give file name b4 that. u can compile if u want and see

I've already explained those errors, and pointed you towards a solution. Stop asking the question over and over again, start processing the answers.

And I don't need to compile your code because its absolutely obvious what it tries to do and what's wrong with it. One last time:
f1= new FileInputStream(args[0]);
If that call fails what do you want to do? Right now you carry on and try to read from f1 anyway. Obviously that's not going to work. Maybe you should consider exiting your program if you can't create the FileInputStream.

e.printStackTrace()
prints complete details of any run-time error (Exception) that happens inside the try block when the program is executing.

ya! right but y it is not printing error? bcz it is compiling ? that's y ?

maybe a problem with your scope. your initialization is within your try block.
your read and write are also run if the code in your try block fails, which means, when your initialization fails.

try creating one try-catch block and put all of your code within the try block, with an e.printStackTrace() in the catch block.

I've already explained those errors, and pointed you towards a solution. Stop asking the question over and over again, start processing the answers.

And I don't need to compile your code because its absolutely obvious what it tries to do and what's wrong with it. One last time:
f1= new FileInputStream(args[0]);
If that call fails what do you want to do? Right now you carry on and try to read from f1 anyway. Obviously that's not going to work. Maybe you should consider exiting your program if you can't create the FileInputStream.

sorry for making u irritated! but i m asking questions only when i implement what u said. :'( i m not asking randomly.

maybe a problem with your scope. your initialization is within your try block.
your read and write are also run if the code in your try block fails, which means, when your initialization fails.

try creating one try-catch block and put all of your code within the try block, with an e.printStackTrace() in the catch block.

yupieeeeeeeee!! problem solved!! really really thanks. ya ! that was the problem!! wow! i m too too too too too too impressed. thanks. thanks both of u . u and this website is damn fabulous. HATS OFF

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.