0

I am trying to create a text file with information from a String array and I have accomplished everything so far, but getting the array into the text file as content. Any help would be greatly appreciated and I have copied all of the code involved so 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 the file at directory " + filename);
        }   

        else 
        {
            System.out.println("An error occurred creating the directory or file " + filename + ". Please contact your 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();
        }
}
}
6
  • What's the problem? What happens when you do System.out.println(names)? Do you know why it prints what it prints? Commented Jan 31, 2014 at 18:23
  • You can't just write names to the file. you have to iterate over each element of the array and write them one by one. Commented Jan 31, 2014 at 18:24
  • Use Arrays.toString(names) in println. Commented Jan 31, 2014 at 18:26
  • The error I am getting when compiling is illegal character for the String array and this is where I have gotten stuck. I am still in the very early learning process so any explanations you can provide would be great! Commented Jan 31, 2014 at 18:26
  • You're not using normal quotes: instead of ". Use a text editor, not a word processing application, to type your code. And if you get an error, paste it in your question instead of letting us guess what the problem might be. Commented Jan 31, 2014 at 18:29

2 Answers 2

2

It looks like you may have smart quotes in your array. Smart quotes are not valid for Java quotes (and I imagine it's very difficult to program Java in Word)...

String[] names = {"John", "Matthew", "Luke", "Peter"};

You can iterate over the array and write each name to the file,

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

but you may prefer to use Arrays#toString like this -

 bw.write(java.util.Arrays.toString(names));
Sign up to request clarification or add additional context in comments.

3 Comments

Could you go into depth a little more on this. I am beginning to see what you mean about writing each name to the file, but the problem I am having now is with the initial declaration of the array. You mentioned Smart Quotes and I'm not even sure what those are. All help is greatly appreciated.
@CoShark Can you see the difference between "John" and “John”? The second has curly quotes, which is what Word does for you. They won't work in Java. You can turn them off in Word, but I suggest you use NetBeans or Eclipse instead (or notepad++). All three are free.
Elliot, thank you for pointing this out. I am using text wrangler to write this, but I did not even notice there was such a difference. I had copied the names from a word processor and that must be where they carried over. This actually fixed my issue!
1

My friend please intiallize your string

like this

String[] names = {"John", "Matthew", "Luke", "Peter"}; 

not like String[] names = {“John”, “Matthew”, “Luke”, “Peter”};

and one suggestion from side u have to make one finally block to close your resource not try to close them in try block

1 Comment

have you tried my suggestion, they are the part of best practices even if ur code crashes for something your resources are get closed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.