Java Program to write int array to a file

Java 8Object Oriented ProgrammingProgramming

Here’s our file −

FileWriter writer = new FileWriter("E:/demo.txt");

Now, consider an Integer array −

Integer arr[] = { 10, 20, 30, 40, 50 };

Write the above array to the file “demo.txt” −

int len = arr.length;
for (int i = 0; i < len; i++) {
   writer.write(arr[i] + "\t" + "");
}

The following is an example. Here, our file is “E:/input.txt” −

Example

import java.io.FileWriter;
public class Demo {
   public static void main(String[] argv) throws Exception {
      FileWriter writer = new FileWriter("E:/input.txt");
      Integer arr[] = { 10, 20, 30, 40, 50 };
      int len = arr.length;
      for (int i = 0; i < len; i++) {
         writer.write(arr[i] + "\t"+ "");
      }
      writer.close();
   }
}

The output is as follows i.e. the text in the file is −

Output

1020304050
raja
Published on 25-Apr-2019 09:21:17
Advertisements