Win a copy of Serverless Applications with Node.js this week in the NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Ron McLeod
  • Tim Moores
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Vijitha Kumara

where to create the TEXT. txt file and how to compile ?  RSS feed

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not sure how to run the  code. I created a  text file in the package, but I am not sure how that text file is linked to the code. This code has been taken from java 11th edition by harbert schild chapter 13 .

I tried >java ShowFile TEST.TXT  command in command line, but I got an error.



In the code below, how the TEST.txt file is related to the program, I am not getting it .

Thnaks in Advance
 
Saloon Keeper
Posts: 2476
317
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:I tried >java ShowFile TEST.TXT  command in command line, but I got an error.


What was the error?
 
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a suggestion: if you're going to print something and give up when an exception is thrown, instead of a one-line description, print something more informative. Like the actual description of the exception and the stack trace (which tells you where it happened). Like so:



Or in the code you posted you could just declare that your main() method throws Exception, and that will happen automatically. And you wouldn't need to write any of that code which handles the exceptions.

 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Md Zuanyeed Kamal wrote:I tried >java ShowFile TEST.TXT  command in command line, but I got an error.


What was the error?


I forgot to mention the information below
Purpose of the program: This  program uses read( ) to input and display the contents of a
file that contains ASCII text. The name of the file is specified as a commandline
argument.

Here my another doubt is where should the file be located ?
Overall, I didn't understand the code .

Some information author added:   In the program, notice the try/catch blocks that handle the I/O errors that
might occur. Each I/O operation is monitored for exceptions, and if an
exception occurs, it is handled. Be aware that in simple programs or example
code, it is common to see I/O exceptions simply thrown out of main( ), as was
done in the earlier console I/O examples. Also, in some real-world code, it can
be helpful to let an exception propagate to a calling routine to let the caller
know that an I/O operation failed. However, most of the file I/O examples in
this book handle all I/O exceptions explicitly, as shown, for the sake of
illustration.


textfilenotfound.PNG
[Thumbnail for textfilenotfound.PNG]
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:Just a suggestion: if you're going to print something and give up when an exception is thrown, instead of a one-line description, print something more informative. Like the actual description of the exception and the stack trace (which tells you where it happened). Like so:




Actually, I am confused about the information of the code.
 
Ron McLeod
Saloon Keeper
Posts: 2476
317
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch>java ShowFile TEST.TXT
Error: Could not find or load main class ShowFile
Caused by: java.lang.ClassNotFOundException: ShowFile


1. It looks like you are trying to run the application from a source directory.  How did you compile the ShowFile.java file?

Is your class located in C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch, or in another directory like C:\Users\zunayeed\eclise-workspace\Java_Problems\bin\inputOutputWithTryCatch?

2. Since your class is in the inputOutputWithTryCatch package, you should be trying to run it using the fully qualified class name: inputOutputWithTryCatch.ShowFile

3. Try running your application from the top of the directory structure for the class files, not inside the inputOutputWithTryCatch directory:
    C:\Users\zunayeed\eclise-workspace\Java_Problems\bin>java inputOutputWithTryCatch.ShowFile TEXT.TXT
 
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did Schildt really write that sort of code? It is full of errors.
ShowFile is not a good name for a class, but a method.
Don't use anything called XYZInputStream to read a text file. Use something called XYZReader.
Don't close anything explicitly. Use try with resources instead.
Don't use the read() method, an abomination really awkward to use. Wrap your reader so as to buffer it and read line by line.
The loop isn't quite correct; you are not stopping when you get to the EOF character but after that. That is why you are reapeating (i != −1) in lines 39‑40. If you want to stop on the −1, you would need something like this:-You need to assign to i before the test; since > has a higher precedence than =, you have to wrap the left half of the expression in an additional pair of () so the = is executed first.
That code has far too long a main() method.
This is what I would suggest, using the NIO classes to open a Path:-
 
Greenhorn
Posts: 23
IntelliJ IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote: This code has been taken from java 11th edition by harbert schild chapter 13 .



Hello.

I'm reading from that same book - a couple of chapters behind you.

I see you are using Eclipse to practice the examples provided. That will only get you into trouble (like the very situation you are describing in this topic).

You can of course replicate the examples in any IDE, but the author makes an early point in the book by stating that a simple text editor plus the command line should be utilized.

IDE's have a specific way to structure projects and files inside folders (with packages in the mix) - Ron's response above makes it clear that there are issues in your code related to that - you are not controling where files are being stored.

 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:

Md Zuanyeed Kamal wrote:C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch>java ShowFile TEST.TXT
Error: Could not find or load main class ShowFile
Caused by: java.lang.ClassNotFOundException: ShowFile


1. It looks like you are trying to run the application from a source directory.  How did you compile the ShowFile.java file?

Is your class located in C:\Users\zunayeed\eclise-workspace\Java_Problems\src\inputOutputWithTryCatch, or in another directory like C:\Users\zunayeed\eclise-workspace\Java_Problems\bin\inputOutputWithTryCatch?

2. Since your class is in the inputOutputWithTryCatch package, you should be trying to run it using the fully qualified class name: inputOutputWithTryCatch.ShowFile

3. Try running your application from the top of the directory structure for the class files, not inside the inputOutputWithTryCatch directory:
    C:\Users\zunayeed\eclise-workspace\Java_Problems\bin>java inputOutputWithTryCatch.ShowFile TEXT.TXT



I tried in numerous ways, but still I am unable to read the files, even though I have been able to create the class files using command javac ShowFile.java in command line. I have provided the screenshot of the code in command line.
Thanks
ShowFile-Attempt.PNG
[Thumbnail for ShowFile-Attempt.PNG]
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ricky Bee wrote:

Md Zuanyeed Kamal wrote: This code has been taken from java 11th edition by harbert schild chapter 13 .



Hello.

I'm reading from that same book - a couple of chapters behind you.

I see you are using Eclipse to practice the examples provided. That will only get you into trouble (like the very situation you are describing in this topic).

You can of course replicate the examples in any IDE, but the author makes an early point in the book by stating that a simple text editor plus the command line should be utilized.

IDE's have a specific way to structure projects and files inside folders (with packages in the mix) - Ron's response above makes it clear that there are issues in your code related to that - you are not controling where files are being stored.



I got your point. Eventhough the .java file  is in the eclipse directory,  I compiled the program from command line. Yeah you are right, notepad might be the best way to run the code from this chapter as this chapter is too much confusing for me.
Thanks for your suggestion, I appreciate it.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Did Schildt really write that sort of code? It is full of errors.
ShowFile is not a good name for a class, but a method.
Don't use anything called XYZInputStream to read a text file. Use something called XYZReader.
Don't close anything explicitly. Use try with resources instead.
Don't use the read() method, an abomination really awkward to use. Wrap your reader so as to buffer it and read line by line.
The loop isn't quite correct; you are not stopping when you get to the EOF character but after that. That is why you are reapeating (i != −1) in lines 39‑40. If you want to stop on the −1, you would need something like this:-You need to assign to i before the test; since > has a higher precedence than =, you have to wrap the left half of the expression in an additional pair of () so the = is executed first.
That code has far too long a main() method.
This is what I would suggest, using the NIO classes to open a Path:-



This chapter is really too confusing or me, and  while reading  the chapter from the book, I got more confused!!!
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:I have been able to create the class files using command javac ShowFile.java in command line.



That's the wrong way to compile a class which is in a package. Here's a tutorial which explains how to compile and run Java classes in packages.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:. . . . Here's a tutorial which explains how to compile and run Java classes in packages.

It has a misprint in about imports (numbered 5).
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I looked online for tutorials which explained how to compile and use classes which are in packages, but the ones I found were a pretty bad lot. So yeah, I'm not surprised. Anybody who finds better tutorials is very welcome to post them.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:. . . This chapter is really too confusing or me, and  while reading  the chapter from the book, I got more confused!!!

Please explain what you are getting confused about. Also, you didn't answer my question about whether Schildt wrote that code.

Please don't quote the whole of a preceding post; that adds nothing to the discussion.
 
Md Zuanyeed Kamal
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Please explain what you are getting confused about. Also, you didn't answer my question about whether Schildt wrote that code.



The I/O file system  seems confusing as still I didn't understand the concepts reading the chapter . Like why do we use FileInputSrteam, InputStream, inputbufferedreader and when would I have to use reader. Those stuffs are not clearly explained by Herbert Shcild in the book.
Yes, the code has been copied directly from the herbert Schild Complete reference 11th edition chapter 13, which I already acknowledged in my very first post.  
Thanks
 
Paul Clapham
Sheriff
Posts: 24295
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:The I/O file system  seems confusing as still I didn't understand the concepts reading the chapter . Like why do we use FileInputSrteam, InputStream, inputbufferedreader and when would I have to use reader.



The Oracle tutorial tells you all about that, with code examples which you can download and play around with: Lesson: Basic I/O.
 
Campbell Ritchie
Marshal
Posts: 63849
209
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Md Zuanyeed Kamal wrote:. . . Yes, the code has been copied directly from the herbert Schild Complete reference 11th edition chapter 13, which I already acknowledged in my very first post.  
Thanks

Thank you for the info. The same 🕱&☠@!!✊ code appears in my 8th edition of the same book (pages 299ff); it looked so bad I couldn't believe it came out of a book like that.
 
Greenhorn
Posts: 28
Eclipse IDE Java Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
DANG!! I just purchased Java: A Beginner's Guide, Seventh Edition 7th Edition by Herbert Schildt. I haven't cracked it open yet but I sure hope it doesn't lead me astray!!!
 
You have to be odd to be #1 - Seuss. An odd little ad:
global solutions you can do at home or in your backyard
https://www.kickstarter.com/projects/paulwheaton/better-world-boo
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!