This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Java in General and the fly likes trying to write a log file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "trying to write a log file" Watch "trying to write a log file" New topic
Author

trying to write a log file

Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
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 ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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().


[Jess in Action][AskingGoodQuestions]
Raj kalaria
Ranch Hand

Joined: Sep 08, 2005
Posts: 72
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
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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

Joined: Sep 08, 2005
Posts: 72
Thanks for the reply, I will try it.
 
GeeCON Prague 2014
 
subject: trying to write a log file