Help for using PrintWriter and File object
A simple snipett of code to get the user to create a file and write data to it. I cant seem to use the parameter "filename" in the File class constructor...
System.out.println("Enter the name of the input file");
BufferedReader name=new BufferedReader(new InputStreamReader(System.in));
filename = name.readLine();
// create a new output stream and create file
File fileob=new File(filename);<- get an error "Local variable
-not yet initialized"
PrintWriter file=new PrintWriter(fileob);
[540 byte] By [
karanJa] at [2007-11-27 7:16:28]

Is it a run-time or compile-time error?
> compile timeNo compile time error for me. Are you sure you are declaring filename variable before using it? That was the only change I had to make for it to compile.
I am missing some try-catch
How about posting a small (<1 page) example program that forum members can try?
yes i have already declared filename before
here's a sample program
package examples;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class CreateFile {
String name;
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String str;
String filename;
System.out.println("Enter the name of the input file");
try{
BufferedReader name=new BufferedReader(newInputStreamReader(System.in));
try {
filename = name.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// create a new output stream and create file
File fileob=new File(filename);
PrintWriter file=new PrintWriter(fileob);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
{
}
//condition for quitting
while(!(str =in.readLine()).contentEquals("quit")) {
{
file.println(str);
}
{
}
}}
finally{}
}}
import java.io.*;
public class CreateFile {
public static void main(String[] args) throws IOException {
BufferedReader cin =new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the name of the input file");
String filename = cin.readLine();
copyToFile(cin, new File(filename));
}
private static void copyToFile(BufferedReader in, File file) throws IOException {
PrintWriter out =new PrintWriter(file);
try {
String line;
while((line =in.readLine()) != null && !line.equals("quit"))
out.println(line);
} finally {
out.close();
}
}
}
Its a variable scope problem. You are assigning the filename its value in a try block and then trying to access the filename outside the try-catch block. If your try block fails that filename variable may never be assigned a value and that is what the compiler is telling you.
try {
filename = name.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// create a new output stream and create file
File fileob=new File(filename);
You need to re-think your try-catch blocks.
mate filenmae has already been declarted here:
public static void main(String[] args) throws FileNotFoundException {
// TODO Auto-generated method stub
String str;
String filename; <--
so your assumption that the try-catch block is causing the problem is incorrect.
> mate filenmae has already been declarted here:
>
> public static void main(String[] args) throws
> FileNotFoundException {
> // TODO Auto-generated method stub
> String str;
> String filename; <--
>
> so your assumption that the try-catch block is
> causing the problem is incorrect.
Its not an assumption. I am not talking about declaration, I am talking about assigning the filename variable a value. The compiler is giving you an initialization error.
Just copy the line that is giving you the error into the try block and you will see what I mean.
If you assign a variable any value in a try block, there is no guarantee that it will be assigned because the try block may fail before the assignment happens. In that case your variable will just contain default/null value. Now if you go ahead and open a file with a null name, it will cause an error. The compiler has just saved you from a fatal exception by giving you a compile-time error.
If it is still not clear, let me know and we can talk about it more.
Thnx Hippolyte...this is what i have made after reading ur snippet
public class CreateFile {
public static void main(String[] args) throws IOException {
String str="";
System.out.println("Enter the name of the input file");
BufferedReader cin = new BufferedReader(
new InputStreamReader(System.in));
String filename = cin.readLine();
// create a new output stream and create file
PrintWriter file = new PrintWriter(filename);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
// condition for quitting
while (!(str = in.readLine()).equalsIgnoreCase("quit")) {
file.println(str);
}
cin.close();
in.close();
}
}
everything works fine except i dont see anything in the newly created file!!
do u find anything wrong?
also to help you understand try to compile this code:
int a = 5;
int b;
switch(a){
case 5:
b = 2;
}
int temp = b;
You will have a better appreciation of what compiler is doing for you afterwards.
> everything works fine except i dont see anything in the newly created file!!
> do u find anything wrong?
The most important stream to close is "file":
file.close();
Since file is buffered, you will lose the last few kbytes of data unless you flush
the buffer, which close does for you.
By the way, why have both in and cin -- aren't they both reading from System.in?
cheers!! yeah i didnt close the PrintWriter stream and it was causing all the trouble
yes you are right..i dont have to use two input streams-cin and in-just using them to test if using two streams generates any complications...so will use single stream in the completed program.
thnx Hipplyte!