0

I am trying to write a simple method to save my document to a file (overwriting any previous contents of the file.) Unfortunately, my implementation does not seem to work. I am calling it on my document, which is, for all intents and purposes, an array of String. What I'd like to do is to write the contents of my array, with a separate line for each value in the array, the value in position [0] on the first line, and the value for [1] on the second. How would I go about this ?

This is my implementation so far :

  public void save()
   { 
      try
      {
         PrintWriter outputFile = 
            new PrintWriter(new BufferedWriter(new FileWriter(docName)));
         int lineNo = 1;

         while (lineNo != lineNo) // CHANGE THIS!!!
         {  outputFile.println(" ~ ");
            lineNo++;
         }
         outputFile.flush();
      }
      catch (Exception e)
      {
         System.out.println("Document: Touble writing to "+docName);
         System.exit(1);
      }

}

3
  • Nothing happens, and no error is returned ! This is why I am confused so much ! :) Commented Feb 26, 2011 at 18:22
  • becasue it never goes in while loop -- int lineNo = 1; while (lineNo != lineNo) // always FALSE Commented Feb 26, 2011 at 18:23
  • This is the sort of thing a debugger would show you pretty quickly. Commented Feb 26, 2011 at 18:36

4 Answers 4

1

If a is an array of strings,

for (String s : a)
     outputFile.println(s);

will print the array line-by-line to outputFile.

Sign up to request clarification or add additional context in comments.

Comments

0

Iterator over the array, and write the current element.

    String document[] = {"String1","String2","String3"};
    PrintWriter outputFile = 
        new PrintWriter(new BufferedWriter(new FileWriter(docName)));
     int lineNo = 1;

     for(int i = 0; i < document.length; i++)
     {  outputFile.println(document[i]);
        lineNo++;
     }

Comments

0
// myDoc is the "array of string"
foreach (String line : myDoc) {
    outputFile.println(line);
}

Comments

0

I might write it more like this:

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.PrintStream;

/**
 * FileDemo
 * @author Michael
 * @since 2/26/11
 */
public class FileDemo
{
    public static void main(String[] args)
    {
        try
        {
            FileDemo fd = new FileDemo();
            fd.save("out/test.txt", args);
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }

    }

    public void save(String filePath, String [] lines) throws FileNotFoundException
    {
        PrintStream ps = null;

        try
        {
            ps = new PrintStream(new FileOutputStream(filePath));
            int lineNum = 1;
            for (String line : lines)
            {
                ps.printf("%5d %s\n", lineNum++, line);
            }
        }
        finally
        {
            close(ps);
        }
    }

    public static void close(PrintStream ps)
    {
        if (ps != null)
        {
            ps.flush();
            ps.close();
        }
    }
}

I didn't see any actual content in your code, so I added some. I didn't how a file with line numbers was very interesting. You'd be able to modify this to make it differently if you wish.

Comments

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.