• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

files

 
Ranch Hand
Posts: 635
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might want have to explore classes like Scanner, BufferedReader for the same.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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

 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic