?
Solved

output files in java.

Posted on 2006-05-10
4
Medium Priority
?
157 Views
Last Modified: 2011-04-14

 Hello experts,

     I tried to output a String to a file in the following way.
   ------------------------------
   String myData = " a lot of data here";
   FileWriter outFile = new FileWriter(new File("result.txt"));
   outFile.write(myData);
 -----------------------------------
   but my "result.txt" is empty. Does anyone know what I did wrong ? Thanks.
       
0
Comment
Question by:meow00
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 800 total points
ID: 16654421
try closing the file:

   String myData = " a lot of data here";
   FileWriter outFile = new FileWriter(new File("result.txt"));
   outFile.write(myData);
   outFile.close();
0
 
LVL 23

Assisted Solution

by:rama_krishna580
rama_krishna580 earned 200 total points
ID: 16654499
Hi,

Sample one...

import java.io.*;
 
public class FWTest
{
   public static void main(String args[])
   {
      try
      {
                  File file = new File("password.txt");
                  FileWriter text = new FileWriter(file);
                  text.write("userid");
                  text.write("\n");
                  text.write("pwd");
                  text.flush(); //only addition; good practise to flush buffers and toilets!
                  text.close();
            }
            catch(IOException e)
            {
                  System.out.println(e.getMessage());
            }
   }
}

R.K
0
 
LVL 8

Assisted Solution

by:colr__
colr__ earned 200 total points
ID: 16656663
You can use this simple method ot write text to a file:
      
      public static void writeTextFile(String[] output, File file, boolean append) throws IOException{
            
            BufferedWriter out = new BufferedWriter(new FileWriter(file.getPath(), append));
            
            if (output.length > 0){
                  out.write(output[0]);
            }
            for (int i=1; i<output.length; i++){
                  out.newLine();      
                  out.write(output[i]);
            }
            
            out.close();
            
      } // end writeTextFile


colr__
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16659779
And as a good practice, close () in finally blocks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month14 days, 19 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question