This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "files" Watch "files" New topic
Author

files

abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


I open myfile1 in windows, I write blah blah blah...

But when I run this program it does not print out. and after that when I open again, I see the file is empty. Why?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

If the file is already existing- Try using only Reading the file. Because if you open the stream for writing to the file- The contents will get erased provided you are not trying to append the contents to the file.


Mohamed Sanaulla | My Blog
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Mohamed Sanaulla wrote:The contents will get erased provided you are not trying to append the contents to the file.


Agreed. Line 11 will truncate the file.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
then it is a mistake of sun web site:

http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html

How can I report them to fix it?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

abalfazl hossein wrote:
How can I report them to fix it?


There is nothing wrong with the Sun / Oracle code. It's your version that broken -- as you use the same filename for both the in and out file.

Henry
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

abalfazl hossein wrote:then it is a mistake of sun web site:

http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html

How can I report them to fix it?


its correctThe program is correct. They are using 2 different files to copy the content.



abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
Now this error

Exception in thread "main" java.lang.NullPointerException
at filetestread.Main.main(Main.java:36)
Java Result: 1
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

abalfazl hossein wrote:Now this error

Exception in thread "main" java.lang.NullPointerException
at filetestread.Main.main(Main.java:36)
Java Result: 1


There is no Line 36 in the code you have posted. Can you post the updated code you are using where you found this exception?
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635




run:
Exception in thread "main" java.lang.NullPointerException
at filetestread.Main.main(Main.java:29)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

At line 20 you have commented out the instantiation of the FileOutputStream. So out is null and at line 24 you are trying to use a null value. So it should throw a java.lang.NullPointerException

But I dont understand why its giving java.lang.NullPointerException at line 29.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

Mohamed Sanaulla wrote:
But I dont understand why its giving java.lang.NullPointerException at line 29.


Probably the OP not providing all the lines in the file. Next time it may be a good idea to bring up the text editor, find line 29, and tell us which line it is -- as it may not be the same if you don't provide all of the file.

Henry
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


This line has problem

The whole code:



I have a text file I want read it and show the content by byte stream
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

abalfazl hossein wrote:

This line has problem


it has problem because You haven't initialized the in reference to a valid FileInputStream reference. Please note that unless you initialize the object references with valid instances/objects you cannot use those references.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


The output is:

1039710310011510310011510311597100103100115103100115103100115103100115103100115103100115103


Now, I want to see the contents as the character, How can I do this?
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

You might want have to explore classes like Scanner, BufferedReader for the same.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Or go through the Basic I/O tutorial.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635
If I save this string in a text file, Is it possible to cover it to char? by FileOutputStream?


http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3068
    
  33

abalfazl hossein wrote:If I save this string in a text file, Is it possible to cover it to char? by FileOutputStream?


http://download.oracle.com/javase/tutorial/essential/io/bytestreams.html


Did you try executing the example- After all the suggestions given above? It would be helpful if you could understand and execute that sample.
abalfazl hossein
Ranch Hand

Joined: Sep 06, 2007
Posts: 635


Is it possible to read char without using any other class in this code? and without typecast?

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: files