473,394 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Help with I/O closing the Reader

122 100+
Hi everyone.
I need help I'm trying to close the reader and It wil not let me.
Can some one tell me why and possible show me.



Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args) throws Exception {
  2.  
  3.         Scanner kbd = new Scanner(System.in);
  4.         int choice;
  5.         System.out.println("Make a Section: ");
  6.         System.out.println("1. Read Text File ");
  7.         System.out.println("2. Copy Text File ");
  8.         System.out.println("3. Exit ");
  9.         System.out.print("\nPlease press Enter afer each response");
  10.         System.out.println("Enter your choose please: ");
  11.         choice = kbd.nextInt();
  12.         kbd.nextLine();
  13.         if (choice == 1) { // if 1 is select go to makePerson
  14.  
  15.  
  16.             String reader;
  17.             String name = null;
  18.             kbd = new Scanner (new File(name));
  19.  
  20. try{
  21.  
  22.             System.out.print( "Enter the filename you want to open  " );
  23.             name = kbd.nextLine();
  24.  
  25.              while (kbd.hasNext())
  26.              {
  27.                  reader = kbd.nextLine();
  28.                 System.out.println ( reader);
  29.                 System.out.println();
  30.              }
  31.              }finally{
  32.                  if (name !=null){
  33.                  name.close();
  34.              }
  35.              }
  36.           }
Thanks
sandyw
Apr 30 '07 #1
7 1519
JosAH
11,448 Expert 8TB
Variable 'name' is a String; you can't close Strings. Your compiler must've said
something like "close is not a String method". Try to read those compiler error
diagnostics.

kind regards,

Jos
Apr 30 '07 #2
sandyw
122 100+
Thanks figure that out.
But what My real task to figure out is
I need a scanner what will prompt the user to type in a file they are looking for on a hard drive (C:\\ ...) then display that file.

I have found some info on this but I lose and confused on how to tie everything together.

I have these codes so far.
This one to read the file with FileReader.
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2. import java.util.Scanner;
  3.  
  4. public class ScanXan {
  5.     public static void main(String[] args) throws IOException {
  6.         Scanner s = null;
  7.         try {
  8.             s = new Scanner(new BufferedReader(new FileReader("xanadu.txt")));
  9.  
  10.             while (s.hasNext()) {
  11.                 System.out.println(s.next());
  12.             }
  13.         } finally {
  14.             if (s != null) {
  15.                 s.close();
  16.             }
  17.         }
  18.     }
  19. }
This one for Scanner prompt...
Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) throws Exception {
  2.         Scanner kbd = new Scanner(System.in);
  3.         int choice;
  4.         System.out.println("Make a Section: ");
  5.         System.out.println("1. Read Text File ");
  6.         System.out.println("2. Copy Text File ");
  7.         System.out.println("3. Exit ");
  8.         System.out.print("\nPlease press Enter afer each response");
  9.         System.out.println("Enter your choose please: ");
  10.         choice = kbd.nextInt();
  11.         kbd.nextLine();
  12.         if (choice == 1) { // if 1 is select go to makePerson
  13.             String fname;
  14.  
  15.             FileInputStream fin;
  16.  
  17.             long datalen=0;
  18.  
  19.             Scanner kbd1 = new Scanner(System.in);
  20.  
  21.             System.out.println("Enter file name");
  22.  
  23.             fname = kbd1.next();
  24.  
  25.             kbd1.nextLine(); 
  26.  
  27.             fin = new FileInputStream(fname);
  28.  
  29.             while( fin.read()!=-1) {
  30.  
  31.                       datalen++;
  32.  
  33.             }
  34.  
  35.             System.out.println("File "+fname+" is "+datalen+" bytes long");
  36.  
  37.   }
SandyW
Apr 30 '07 #3
JosAH
11,448 Expert 8TB
I get the impression that you are googling for snippets of code and that you are
trying to glue those together without understanding what you're doing.

The snippets your showed in your last reply show different indentation style and,
more important, different coding stye, and most important: they don't do what
your assignment says they should do. And no wonder: those snippets weren't
made for those purposes.

Chop up your problem to smaller sub-problems, Try to write a method first that
opens a file, reads all the lines and display every line. Finally the method should
close the resources again. Hint: read the API documentation for the Buffered-
Reader and pay close attention to its readLine() method. And please *read* the
API documentation.

kind regards,

Jos
Apr 30 '07 #4
sandyw
122 100+
You are right jos,
I'm doing this for fun....
here is what I have now
Sorry its a learning process...

Getting an error on s1.close();
with is error
Exception in thread "main" java.lang.NullPointerException
at ioassign.ReadCopy.main(ReadCopy.java:54)


Expand|Select|Wrap|Line Numbers
  1.     public static void main(String[] args) throws Exception {
  2.         Scanner kbd = new Scanner(System.in);
  3.         int choice;
  4.         System.out.println("Make a Section: ");
  5.         System.out.println("1. Read Text File ");
  6.         System.out.println("2. Copy Text File ");
  7.         System.out.println("3. Exit ");
  8.         System.out.print("\nPlease press Enter afer each response");
  9.         System.out.println("Enter your choose please: ");
  10.         choice = kbd.nextInt();
  11.         kbd.nextLine();
  12.         if (choice == 1) { // if 1 is select go to makePerson
  13.             String fname;
  14.  
  15.             FileInputStream fin = null;
  16.  
  17.             long datalen=0;
  18.  
  19.             Scanner kbd1 = new Scanner(System.in);
  20.  
  21.             System.out.println("Enter file name");
  22.  
  23.             fname = kbd1.next();
  24.  
  25.  
  26.  
  27.  
  28.             Scanner s1 = null;
  29.  
  30.                     try {
  31.                         s1 = new Scanner(new BufferedReader(new FileReader("fname")));
  32.  
  33.             while( fin.read()!=-1) {
  34.  
  35.                       datalen++;
  36.  
  37.             }
  38.  
  39.             System.out.println("File "+fname+" is "+datalen+" bytes long");
  40.             System.out.println("File "+s1+" is "+datalen+" bytes long");
  41.                     }finally{
  42.                         if (fname !=null){
  43.                             s1.close();
  44.                         }
  45.                     }
  46.   }
thanks
Sandyw
PS I hope you don't mind helping me.
Apr 30 '07 #5
JosAH
11,448 Expert 8TB
Getting an error on s1.close();
with is error
Exception in thread "main" java.lang.NullPointerException
at ioassign.ReadCopy.main(ReadCopy.java:54)


Expand|Select|Wrap|Line Numbers
  1.             String fname;
  2.    ...
  3.             Scanner s1 = null;
  4.  
  5.                     try {
  6.                         s1 = new Scanner(new BufferedReader(new FileReader("fname")));
  7.  
  8.    ...
  9.                     }finally{
  10.                         if (fname !=null){
  11.                             s1.close();
  12.                         }
  13.                     }
  14.   }
I suggest you should read some tutorials first. This code just doesn't make
sense and there are so many errors in it I hardly know where to start. For
one thing, what are those double quotes doing there in "fname"? Most likely
a file named "fname" doesn't exist so the entire opening sequence will fail
so the Scanner s1 will be null (you don't even use it at all) and hence the
closing will fail too. Everything fails in that messy code.

Take it from me: study those tutorials first so that you *know* what you're doing.
Glueing code snippets together by trial and error is not the way to go. I wish you
all the best studying the tutorials. Here's the link.

kind regards,

Jos
Apr 30 '07 #6
NeoPa
32,556 Expert Mod 16PB
Sandy,
I'm just visiting from another forum, but it seems to me that Jos is actually being very helpful here.
It seems like you're new to this so Jos is pointing you in the best direction for getting up and running, while giving you some very useful tips on how to find problems in your code on the way.

We don't do the work for people here. We try to help members to learn and hold their hands through the harder bits where necessary. Jos is very experienced and I strongly recommend that you follow the instructions he's posted for you in posts #2, 4 & 6. If you can show that you've taken those steps then I'm sure he can take you further.

Best regards -NeoPa.
May 1 '07 #7
JosAH
11,448 Expert 8TB
*blush* ;-)

kind regards,

Jos
May 1 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Johnny Knoxville | last post by:
I've added a favicon to my site (http://lazyape.filetap.com/) which works fine if you add the site to favourites the normal way, but I have some JavaScript code on a couple of pages with a link,...
6
by: Steven Blair | last post by:
Hi, I am writing an application using a 3 tier model (Client, Business Layer and DB) The DB layer creates a OdbcDataReader object back up to the Client where the data is read and displayed on...
4
by: Jeff User | last post by:
Hi all I am using an OleDbDataReader. I need to establish and then keep the connection that I use, but I do not need to keep the data reader, after this operation is over. Therefore, regardless...
0
by: lostdreamz | last post by:
I'm able to successfully retrieve a webpage from a web server with a VB.NET desktop application using (HttpWebRequest/HttpWebResponse), but when I click the Close button on the application, both it...
2
by: tshad | last post by:
I have a section of code that is giving me an error: ***************************************************************** Invalid attempt to Read when reader is closed. Description: An unhandled...
31
by: Extremest | last post by:
I have a loop that is set to run as long as the arraylist is > 0. at the beginning of this loop I grab the first object and then remove it. I then go into another loop that checks to see if there...
1
by: S. David Kyle | last post by:
I am having a weird issue when I generate an input Html tag from a Xsl transformation. When the trasform is executed on the xsl below, the <inputtag has the closing slash removed in the first...
10
by: John Kraft | last post by:
Hello all, I'm experiencing some, imo, strange behavior with the StreamReader object I am using in the code below. Summary is that I am downloading a file from a website and saving it to disk...
1
by: Mel | last post by:
I am performing the same recordset multiple times, just passing different parameters each time. Is there a way to do this more efficiently without having to close and re-open the connection and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.