• Post Reply Bookmark Topic Watch Topic
  • New Topic

FileHandling Program is Not working?  RSS feed

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there

this is my progarm


As you can see my Intention is to save randoly generate Integer in the output file

And this is not working

but if I use Something like this


it's working fine reading that from Input.txt and writing that data in output.txt file.
but not in first case

Both file are in working directory already created


Where is wrong?

Thanks
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Although write() takes an int, the int must be in the range of 0 to 255, i.e. a value that can fit in a byte.

Edit: Also not all of those values are valid ASCII characters, which, because you're writing to a .txt file, is what I assume you want.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>And this is not working

Can you please be more specific than "this is not working"

WHAT is not working?
Does your code fail to compile?
Is there an error message?
Does the file contain the collected works of Shakespeare rather than the number you expect?

What are you seeing?
What are you expecting to see?
How can you tell "it is not working" ?

Answer those questions for yourself.
And then I would also suggest you look up when it is appropriate to use a Writer(or FileWriter) as opposed to an OutputStream (or FileOutputStream)

 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Stefan,@Carry Brown

I have changed now my code according to @Carry Suggestion


now output is saving in a file but like this

???ô???Ñ???}???_???,???t???]???ø???K?H?????????,???£???=???@???ï???‰???O??????¬???ò???t???*??????¤???¬???Ç???|???o???¿???±???ì???P???Ê???e???g???x???u??? ???+???[???ˆ???·???¨???î???m??™???^???š??????a???g?3???·???K??????`???5???[???*???ž?ë????e??????%???Ç???Ï???H???\???~??????Ã???(???`???Á???‘???Å???¿???½???d???ü???¨???ÿ???0???â???¡??????{?Š???Ý???;???Î?????????y???.???©???@???g???e???G??? ???0???·???­???®??? ???/???ô???s???œ???t???ê???v???U?-???z???´???f???Û???r???z???Ô???º???¬???[???Š???Æ???”??? ???$??¿???1???º???•???æ??????Ô???†???“???È???b???9???
????????????*???¶???Õ???k???e???¸???o?‰?'???Ô???J???%??³???
???Ÿ???ú??????—???/???®??????ú???8?¢???æ???»???ô??? ???ð???R???‡???‹???Ê??????B???Û???ß???
???3???7???G??????„??????ç???U???£???O???)???a???Õ???;??±??????,???½???ñ???ò???¯???f???t???7???Á???™???ç???9???D???Ã???÷???5???P???8???b???8???Â???Ü???Á???m???è?????????



What are you expecting to see?


@Stevan
I am expecting that whatever number is generate in the given range that number saved in Number Format in my target file
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could

Or
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Carey Brown

Awesome

Thanks its working know

My Question is Suppose I want Each number in one line in file and next number will be start from next line

How can I acheive this
 
Carey Brown
Saloon Keeper
Posts: 3323
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you had used a PrintStream or PrintWriter you'd have access to a println() method which adds the platform dependent new-line.
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quick/Simple/stupid fix: You could use the writer's write method to output a line separator.



Alternatives:
The BufferedWriter class provides a newLine() method.
A PrintWriter will let you use the println( whatever ) method.

Wrap your FileWriter in either of those, and use those provided methods to output a newline character.
 
Kishor Joshi
Ranch Hand
Posts: 674
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks @Stevan ,@Carrey

One more question will there be any perfomace in using FileWriter and PrintWriter for various InputRange

like suppose i have following range
0-100000
1-100000000
0-100000000000

Will there be any advantage or not in using PrintWriter and FileWriter
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why you are using input stream when you are not using it. Secondly you can do it like this way:




text written in file:

6 9 100 9 63 30 22 64 59 93 80 15 58 4 28 8 12 64 1 55 97 96 24 95 91 21 53 42 76 65 57 37 31 27 23 76 82 63 81 41 70
65 16 49 70 99 36 90 70 97 94 84 8 93 10 53 94 36 47 89 78 6 68 4 98 7 17 89 34 39 24 74 13 56 70 97 13 89 22 22 34
44 44 94 49 96 85 13 23 42 59 36 27 54 14 45 1 99 15 41
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have been given good advice. I would use Scanner/Formatter myself for text files, but you would do well to go through the Java® Tutorials and learn about the different types of stream for text file and binary files.
As you have already been told use writers and readers for text files.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!