File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

trying to write a log file

 
Raj kalaria
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • 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 ]
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply, I will try it.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic