- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Java Program to write an array of strings to a file
Here’s our file −
FileWriter writer = new FileWriter("E:/demo.txt");
Now, consider a string array −
String arr[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" };
Write the above array to the file “demo.txt” −
int len = arr.length; for (int i = 0; i < len; i++) { writer.write(arr[i] + "-"); }
The following is an example. Here, our file is “E:/demo.txt” −
Example
import java.io.FileWriter; public class Demo { public static void main(String[] argv) throws Exception { FileWriter writer = new FileWriter("E:/demo.txt"); String arr[] = { "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" }; int len = arr.length; for (int i = 0; i < len; i++) { writer.write(arr[i] + "-"); } writer.close(); } }
The output is as follows i.e. the text in the file is −
Output
ONE-TWO-THREE-FOUR-FIVE-SIX-SEVEN-EIGHT-NINE-
- Related Articles
- Java Program to write int array to a file
- C# Program to write an array to a file
- Write a program to convert an array to String in Java?
- How to write contents of a file to byte array in Java?
- How to create and write JSON array to a file in java?
- Write a Python program to export a dataframe to an html file
- Write a Golang program to reverse an array
- C Program to Sort an array of names or strings
- C# Program to search for a string in an array of strings
- Golang program to write into a file
- Write a program to reverse an array in JavaScript?
- Java Program to Convert contents of a file to byte array and Vice-Versa
- Write a Golang program to search an element in an array
- Write a C program to find total number of lines in an existing file
- Java Program to Append Text to an Existing File

Advertisements