Please explain what you are getting confused about. Also, you didn't answer my question about whether Schildt wrote that code.
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:-
Ricky Bee wrote:
Md Zuanyeed Kamal wrote: This code has been taken from java 11th edition by harbert schild chapter 13 .
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.
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
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:
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?
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
Carey Brown wrote:Try:
cd "c:\program files\java\jdk-12\bin"
If this doesn't work you've got a bad install. If it does work then something is preventing you from accessing it from a command window. Usually PATH would be the culpret, but may not be in your case.
Stephan van Hulst wrote:You need to tell Eclipse where your JDK is located. Set the JAVA_HOME environment variable to the JDK root folder.