Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?

What I have tried:

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);}
   }
}
Posted
Updated 12-Dec-19 20:07pm
Comments
DaveAuld 13-Dec-19 1:01am    
Sounds like a homework question.......so what is not working? What errors do you get? Does the file get created? Is the file lock being released if the program crashes and prevent you running again? etc. etc. etc.
NarayananKrish 13-Dec-19 1:39am    
I rying to write 1 to 1000 numbers but it prints like ASCII values. I need to print 1 to 1000 integers
Richard MacCutchan 13-Dec-19 4:57am    
System.out.println("success...");
You should not post success messages until you have checked that your code actually does what is expected.

1 solution

Try:
fout.write(i.toString());
That'll fix your immediate problem.
The next problem you will notice is that your file will contain a single, very long, number ... Hint: you might want to think about how you want to separate your numbers.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900