Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Help Required, Problem in writing a File.

Thread view: 
hammad.afzal@gmail.com - 23 Nov 2006 18:51 GMT
Hello Dear Fellows.

I am having a problem in writing files. Problem seems to be very
trivial but I dont know, why I havent been able to figure it out. I am
pasing a code snippet below. Task is to read files from a directory,
process it a bit, and write again.

   /* fileManager is an instance of userdefined class. Contents of
files are successfully read into StringBuffer contents*/

               for (int n = 0; n < terms.length; n++)
               {
                   StringBuffer strBuffer = new
StringBuffer(replacer_terms[n]);
                   contents.append(strBuffer);

contents.append(System.getProperty("line.separator"));
               }
               fileManager.fileWriter(contents,f2 + "\\Profile_" +
str[i]);
               System.out.println("Value of File is : " + str[i]);
               System.out.println("Value of Contents is : " +
contents);

f2 is a path of directory, and this code runs in a loop for all files
in direcory f2 where str[i] is the name of the file in directory f2,
which is being processed. Problem is the last two lines also shows the
correct value, but I dont know why it is not writing into file.

The code of fileManager.fileWriter is here:
public void fileWriter(StringBuffer stringBuffer,String path)
   {
       try
       {
           File writeFile = new File(path);
           OutputStream fout = new FileOutputStream(writeFile);
           OutputStream bout = new BufferedOutputStream(fout);
           OutputStreamWriter writer = new OutputStreamWriter(bout);
           writer.write(stringBuffer.toString());
       }
  }

Kindly enlighted me if you figure out where lies the problem. Many
Thanks
Oliver Wong - 23 Nov 2006 18:55 GMT
> The code of fileManager.fileWriter is here:
> public void fileWriter(StringBuffer stringBuffer,String path)
[quoted text clipped - 11 lines]
> Kindly enlighted me if you figure out where lies the problem. Many
> Thanks

   You have to close the output streams when finished writing.

   - Oliver
Madi - 23 Nov 2006 19:08 GMT
Thanks Oliver ..it worked ..:) .... I had written this method for
writing a single file...
and it worked well in that case,,,,

> > The code of fileManager.fileWriter is here:
> > public void fileWriter(StringBuffer stringBuffer,String path)
[quoted text clipped - 15 lines]
>
>     - Oliver
Thomas Hawtin - 24 Nov 2006 14:34 GMT
>> The code of fileManager.fileWriter is here:
>> public void fileWriter(StringBuffer stringBuffer,String path)
[quoted text clipped - 3 lines]
>>            File writeFile = new File(path);
>>            OutputStream fout = new FileOutputStream(writeFile);
            try {
>>            OutputStream bout = new BufferedOutputStream(fout);
Single write => buffer probably not useful. ^
>>            OutputStreamWriter writer = new OutputStreamWriter(bout);
              ^^^^^^^^^^^^^^^^^^ Just Writer would be better.
>>            writer.write(stringBuffer.toString());
              writer.flush();
            } finally {
              fout.close();
            }
>>        }
>>   }
[quoted text clipped - 3 lines]
>
>     You have to close the output streams when finished writing.

More than that, you should always close resources, even if you get an
exception.

Tom Hawtin


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.