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]
# 1
Is it a run-time or compile-time error?
DarumAa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 2
compile time
karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 3
> 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.
DarumAa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 4
I am missing some try-catch
S_i_m_ua at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 5
How about posting a small (<1 page) example program that forum members can try?
Hippolytea at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 6
yes i have already declared filename before
karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 7
Is the problem solved?
Hippolytea at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 8
still working on it mate
karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 9

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{}

}}

karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 10

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();

}

}

}

Hippolytea at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 11

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.

DarumAa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 12

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.

karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 13

> 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.

DarumAa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 14

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?

karanJa at 2007-7-12 19:05:29 > top of Java-index,Java Essentials,New To Java...
# 15

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.

DarumAa at 2007-7-21 22:14:49 > top of Java-index,Java Essentials,New To Java...
# 16

> 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?

Hippolytea at 2007-7-21 22:14:49 > top of Java-index,Java Essentials,New To Java...
# 17

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!

karanJa at 2007-7-21 22:14:49 > top of Java-index,Java Essentials,New To Java...
# 18
Good luck!
Hippolytea at 2007-7-21 22:14:49 > top of Java-index,Java Essentials,New To Java...