Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Where is my Input Stream Getting closed? program:(NOTE: please check the description for the complete program )

import java.io.*;

import java.util.*;

public class FILE_CGI {

 

String file_name;

 

 

public FILE_CGI(String file_name)

{

this.file_name = file_name+".txt";

}

 

public static boolean  deleteFile(String fname)

{

boolean exists = new File(fname).delete();

 

return exists;

 

}

 

public static void test(String filename)

{

System.out.println("the following File "+filename+" is"+filename+(deleteFile(filename)?"is Deleted":"does not Exists"));

}

 

public static void create_file(String fname)

{

try {

if(new File(fname).createNewFile())

{

System.out.println(fname+" file is created");

}

else

{

System.out.println("Error Occured while creating file");

}

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

}

 

public void readFile(String filename)

{

FileInputStream fis = null;

 

try {

fis = new FileInputStream(filename);

 

byte[] buffer = new byte[];

 

int total=0;

int reads=0;

 

while((reads = fis.read(buffer))!= -1)

{

System.out.println(new String(buffer));

total += reads;

}

 

System.out.println("Total "+total+" bytes are read");

 

}catch(FileNotFoundException e)

{

System.out.println(e);

}

catch(IOException e)

{

System.out.println(e);

}

finally

{

try

{

if(fis!=null)

{

fis.close();

}

}catch(IOException e)

{

System.out.println(e);

}

}

 

}

 

public void writeFile(String fname)

{

 

BufferedWriter bw = null;

try {

FileWriter fw = new FileWriter(fname);

 

bw = new BufferedWriter(fw);

 

System.out.println("Enter the text you want to write in "+fname+" file");

 

BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

//HERE STREAM AUTOMATICALLY GETS CLOSED

String str= br.readLine();

//how to read a string that contains new line paragraphs 'enter' etc

bw.write(str);

bw.newLine();

bw.write("End of File");

br.close();

 

}catch(IOException e)

{

e.printStackTrace();

}

finally {

try {

if(bw!=null)

{

bw.close();

 

}

}catch(IOException e)

{

e.printStackTrace();

}

}

}

 

public static void main(String[] args) {

// TODO Auto-generated method stub

String choice = null ;

Scanner sc = new Scanner(System.in);

System.out.println("Enter file name:");

String fn = sc.nextLine();

FILE_CGI fc = new FILE_CGI(fn);

create_file(fc.file_name);

fc.writeFile(fc.file_name);

System.out.println("Reading the file "+fc.file_name+" .");

fc.readFile(fc.file_name);

//STREAM IS CLOSED EXCEPTION IS THROWN ERROR DELETING THE FILE

System.out.println("Want to delete "+fc.file_name+" file");

try {

choice = sc.next();

}catch(Exception e)

{

System.out.println(e);

}

 

if(choice=="yes")

{

test(fc.file_name);

}

else

{

System.out.println("EXIT");

}

sc.close();

}

 

}

user-image
Question added by Abdul Rahman Mohammed
Date Posted: 2017/07/13

i want to know why i could'nt enter string in the following lines 

Note: Occurence of Exception is in Main() method following statements.

/STREAM IS CLOSED EXCEPTION IS THROWN ERROR DELETING THE FILE

System.out.println("Want to delete "+fc.file_name+" file");

try {

choice = sc.next();

}catch(Exception e)

{

System.out.println(e);

}

 

if(choice=="yes")

{

test(fc.file_name);

}

else

{

System.out.println("EXIT");

}

sc.close();

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.