• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

files

 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3159
33
Google App Engine Java Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now this error

Exception in thread "main" java.lang.NullPointerException
at filetestread.Main.main(Main.java:36)
Java Result: 1
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The output is:

1039710310011510310011510311597100103100115103100115103100115103100115103100115103100115103


Now, I want to see the contents as the character, How can I do this?
 
Mohamed Sanaulla
Saloon Keeper
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might want have to explore classes like Scanner, BufferedReader for the same.
 
Wouter Oet
Saloon Keeper
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or go through the Basic I/O tutorial.
 
abalfazl hossein
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3159
33
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 635
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic