• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

trying to write a log file

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Can some one tell me what mistake i am doing in the below code. I want to write a log file. But the file is created without content. that is
c_objLogFile.write("we are inside");// this is not printed in the file which i am creating.

Basically i want to impement a interface which has two methods and in the implementation class i need to creat a log file and write. please can u help me


// interface
package com.cp.Bul;
public interface writetest {
public String transformPostLoad();
public String trandformPreLoad();
}
----------------------------------------------------------------------
entryclass
-----------------------------------------------------------------------
package com.cp.Bul;

public class entryclass {
int[] arra = new int[5];
public static void main(String[] args) {
int i;
for(i=0;i<5;i++)
{writefile wf = new writefile();
wf.trandformPreLoad();
}
}
-------------------------------------------------------------------------

package com.cp.Bul;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class writefile implements writetest {
private FileWriter c_objLogFile;
private File file = new File ("C:\\write.txt");
private static int count =0;

public static void main(String[] args) throws IOException {
}

public String transformPostLoad() {
return "" ;
}
public String trandformPreLoad() {
System.out.println("the value of count: " + count);
count = count+1;
String source_lifecycle_state = "life";
tocalculate(source_lifecycle_state);
return "";
}

private void tocalculate (String message_i)
{
try
{
System.out.println("We are inside the file function");
System.out.println ("Log file is: " + file.getName());
c_objLogFile = new FileWriter (file);
c_objLogFile.write("we are inside");// this is not printed in thje file
c_objLogFile.write(message_i); // this is not printed in the file
}
catch (Exception ge)
{
System.out.println (ge.getMessage ());
}
}
}
---------------------------------------------------------------------
[ July 01, 2006: Message edited by: Raj kalaria ]
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to close the files at some point; you're not doing that here. Until you close a file, there's no guarantee the contents have actually been written out to the disk. You can also force intermediate results out to a file by calling flush(). The best thing to do is to always close() a file when you're done with it, even if you've been using flush().
 
Raj kalaria
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks, Yeh that was the mistake, i was not closing the file.
I have one more doubt.

Now since i have a loop, the output file is created for each item in the loop that is when i = 1 to i = 5 . and i get the result for last "i" value only. is that a way where i can get for all the value of "i" in the out put file.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can either 1) just open the file one time, and do all the writes to that one FileWriter, then close it; or 2) open it five times and pass "true" as the second argument to the constructor, which tells the FileWriter to append new data to the end of the file, rather than erasing and recreating the file each time. For log files, "1" is the better option.
 
Raj kalaria
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the reply, I will try it.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic