1

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);}
       }
}
Kapil
  • 817
  • 2
  • 13
  • 25
  • 2
    Quick fix: replace `fout.write(i);` with `fout.write(Integer.toString(i).getBytes());` – Kapil Dec 13 '19 at 06:10
  • 1
    Does this answer your question? [Write int to text file using Writer](https://stackoverflow.com/questions/7357852/write-int-to-text-file-using-writer) – Ashutosh Dec 13 '19 at 06:11

6 Answers6

4

You can use PrintWriter and FileWriter to write into the file. You can refer below code for your reference:

public class FileWriterExample {
    public static void main(String args[]) {
        try {
            PrintWriter fileout = new PrintWriter(new FileWriter("C://Users//Desktop//random.txt"));

            for (int i = 1; i < 1001; i++) {
                fileout.println(i);
            }
            fileout.close();
            System.out.println("success...");
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
I'm_Pratik
  • 541
  • 8
  • 22
3
import java.io.FileOutputStream;
class FileWriterExample {
   public static void main(String args[]){
       try{
           FileOutputStream fout=new FileOutputStream("./trial.txt");

          int i;
          for(i = 0; i<= 1000 ; i++) {
              String a = new Integer(i).toString();
              a = a + ' ';
              fout.write(a.getBytes());
          }


           fout.close();
           System.out.println("success...");
       }catch(Exception e){System.out.println(e);}
   }
}
Pushpendra Kumar
  • 1,721
  • 1
  • 15
  • 21
2

I would like to suggest using a FileWriter (java.io.FileWriter) instead of a FileOutputStream. It assumes that the file being written is a text file.

You might have to explicitly put in the new lines, so your write would look like

fout.write(i + "\n");

And you will still need to declare i to be an integer. Those should be the only change to you code.

NomadMaker
  • 447
  • 1
  • 5
  • 9
0

Try this one. I converted the integer value to String and used BufferReader, FileWriter and InpuStreamReader Classes

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileWriterExample {
    public static void main(String[] args) throws IOException {

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        BufferedWriter out = new BufferedWriter(new FileWriter("E:\\trial.txt"));
        try {
            for(int i = 0; i< 1000 ; i++) {

                // in here value will be written as String format
                 out.write(String.valueOf(i));
                 // goes to new line if want
                 out.newLine();
        }

            System.out.print("Write Successful");
        } catch(IOException e1) {
            System.out.println("Error during reading/writing");
        } finally {
            out.close();
            in.close();
        }

    }

}
0

With PrintStream or PrintWriter you can print all basic types directly without needing to convert to String beforehand:

    public static void main(String args[]){
        try{
            FileOutputStream fout = new FileOutputStream("D:\\trial.txt");
            PrintStream pout = new PrintStream(fout);
            for(int i = 1; i<= 1000 ; i++) {
                pout.println(i);
            }
            pout.close();
            System.out.println("success...");
        } catch(Exception e){
            System.out.println(e);
        }
    }

marco
  • 711
  • 6
  • 14
0

You can use the following two functions to do the job. Both will write the numbers separated by spaces.

The first function will always append the numbers to the file. The second function will write the numbers discarding the previous content of the file. You can use them according to your requirement.

public static void writeNums(int[] arr, File file) throws Exception{
    FileWriter fw = new FileWriter(file, true);
    for(int x:arr){
        fw.write(Integer.toString(x));
        fw.write(" ");
    }
    fw.close();
}

public static void writeNums(int[] arr, File file) throws Exception{
    PrintWriter pw = new PrintWriter(file);
    for(int x:arr){
        pw.format("%d ", x);
    }
    pw.close();
}