Java Insert String array into text file -


i trying create text file information string array , have accomplished far, getting array text file content. appreciated , have copied of code involved far.

import java.io.bufferedwriter; import java.io.file; import java.io.filewriter; import java.io.ioexception;     public class writetofileexample { public static void main(string[] args) {     string newdir = "new_dir";          boolean success = (new file(newdir)).mkdir();         newdir = "/volumes/mav/names/";         success = (new file(newdir)).mkdirs();         file filename = new file("/volumes/mav/names/javaprogramming.txt");          if (success)          {             system.out.println("successfully created file @ directory " + filename);         }             else          {             system.out.println("an error occurred creating directory or file " + filename + ". please contact system administrator.");         }          try          {             string[] names = {“john”, “matthew”, “luke”, “peter”};              if (!filename.exists())              {                 filename.createnewfile();             }              filewriter fw = new filewriter(filename.getabsolutefile());             bufferedwriter bw = new bufferedwriter(fw);             bw.write(names);             bw.close();           }          catch (ioexception e)          {             e.printstacktrace();         } } } 

it looks may have smart quotes in array. smart quotes not valid java quotes (and imagine it's difficult program java in word)...

string[] names = {"john", "matthew", "luke", "peter"}; 

you can iterate on array , write each name file,

for (int = 0; < names.length; i++) {   if (i != 0) bw.write(", ");   string name = names[i];   bw.write(name); } 

but may prefer use arrays#tostring -

 bw.write(java.util.arrays.tostring(names)); 

Comments

Popular posts from this blog

html - Sizing a high-res image (~8MB) to display entirely in a small div (circular, diameter 100px) -

java - IntelliJ - No such instance method -

identifier - Is it possible for an html5 document to have two ids? -