How to write an array into a txt file in java Currently I ha

How to write an array into a txt file in java?

Currently, I have:

if choice -1D System. out.println \"What is the length you want int l Scan .nextIntO; String arr lengthNGl); print arr);

Solution

//Test program.java

//This sample code will help to write data of array to text file
//WriteArray.java

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

public class WriteArray {
  
   public static void main(String[] args) {
      
       //your code goes here
       //call print with string array
      

   }
  
   /**
   * The print method that takes a array of type String type
   * and writes the data of the array to the output.txt file.
   * */
   public static void print(String arr[])
   {
       //Create a FileWriter object
       FileWriter filewriter=null;
       //file name
       String fileName=\"output.txt\";
      
       try {
           //open file for writing array data to file
           filewriter=new FileWriter(new File(fileName));
          
           for (int i = 0; i < arr.length; i++)
           {
               //write arr data to file
               filewriter.write(arr[i]);
           }
          
           filewriter.close();
          
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
   }//end of print method

}

How to write an array into a txt file in java? Currently, I have: if choice -1D System. out.println \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site