Java Program to write int array to a file


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

karthikeya Boyini
karthikeya Boyini

I love programming (: That's all I know

Updated on: 30-Jul-2019

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements