• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

How to write 1 to 1000 numbers into text file using java?

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am fresher candidate. So how to write 1 to 1000 numbers into text file? I tried las below code but it writes as like ASCII values. unable to print as numbers so how to write as numbers?


import java.io.FileOutputStream;
public class FileWriterExample {
   public static void main(String args[]){
       try{
           FileOutputStream fout=new FileOutputStream("D:\\trial.txt");
        /*   int i;
        for(i = 0; i< 1000 ; i++) {
            fout.write(i);
        }

       
           fout.close();
           System.out.println("success...");
       }catch(Exception e){System.out.println(e);}
   }
}
 
Bartender
Posts: 362
44
Firefox Browser MySQL Database Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will have to write them into the file as strings to be able to read them. The FileOutputStream is meant to write raw data as it says in the documentation. The docs also say that for writing characters you should consider FileWriter instead.

To convert the integer into a string you could do or a more quick-and-dirty way could be to simply concatenate it with a string, like this:
Here is a small example which does what you want

Notice the try-with-resources which closes the FileWriter automatically.
A note on exception handling: you should always catch the most specific exception that is practical. For example, if your writer throws an IOException, you should catch the IOException, instead of a blanket Exception.

I hope that Helps!
 
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do so few of the writer classes seem to take a Path as a constructor parameter?
Maybe I would use a formatter, which gets you out of using \n because you write %n. You can use this sort of method and pass the Writer to the Formatter constructor. You would still use try with resources, as you correctly mentioned. You are also right about always using classes with names like XYZWriter. I might also use an IntStream:-Note that range will run 0...999, so I wrote i + 1 to get 1...1000. You can pass a buffered writer to a Formatter constructor because it implements Appendable.

[edit: additional] The %d tag allows you to convert the int to a String and write it all in one action.
 
Jj Roberts
Bartender
Posts: 362
44
Firefox Browser MySQL Database Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Campbell, I appreciate your tips! I haven't yet really studied IO, but I will definitely look into the classes you brought up. IO is still on my list; right now I am trying to conquer that many-headed monster Modules.
 
Campbell Ritchie
Marshal
Posts: 73979
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jj Roberts wrote:Thank you Campbell. . .

That's a pleasure We all learn from this forum, even those of us who have been here for 14 years.

I haven't yet really studied IO . . .

If you go to archive.org and find the wayback machine, you can find older versions of the Java™ Tutorials. This section of the current (Java8) Java™ Tutorials version will tell you about IO using the NIO package.

that many-headed monster Modules.

I never realised modules was a Hydra!
 
Don't MAKE me come back there with this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic