Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class createStringOnFile {
- static String[] name1 = {"ahmed","Mohamed","Ali"};
- static String[] name2 = {"Ali","momen","Sena"};
- static String[] name3 = {"Ramy","Yasser","Mariem"};
- static String[] filenames = {"file1","file2","file3"};
- static String[][] names = {name1,name2,name3};
- static Formatter writer;
- public static void main(String args[]) {
- CreateFiles(filenames);
- }
- public static void CreateFiles(String[] filename) {
- for(int x =0;x<filename.length;x++) {
- try {
- writer = new Formatter(filename[x]+".txt");
- WriteToFile(names);
- }catch(Exception e) {
- System.out.println("Error :"+e);
- }
- }
- }
- public static void WriteToFile(String[][] names) {
- try {
- for(int x=0;x<names.length;x++) { // creates variable x and assign it to value 0; if x is less than name.length(the length of String[][] names which is 3) then go to next loop; increment x by 1 after the second for loop.
- for(int y=0;y<names[x].length;y++) { // creates variable y and assign it to value 0; if y is less than name[x].length(the length of String[][] names with index x(0,1,2) which is 3) then go to next loop; incremenent y by 1 after the after the loop.
- writer.format("%s "+(y == names[x].length-1 ? "%n" : ""), names[x][y]);
- }
- }
- writer.close();
- }catch(Exception e) {
- System.out.println("Error writing to file: "+e);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement