• Post Reply Bookmark Topic Watch Topic
  • New Topic

Writing to a file  RSS feed

 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's what I want to do. I have a recursive method that builds combinations of things. It doesn't matter what. I'm generating data to use in some research. I want to write this data to a single file.

Here's what I tried. I tried creating a very large array of this data and write it out all at once but the array was too large and it was crashing. So I decided to write the objects out to a file as I created them. I created a Log class that has static methods to set the filename and write data to the file. I created them as static methods because I was under the assumption I needed it to be static to use the same file without passing it around. Kind of like using System.out.println.

Log.init ("test.txt");
Log.write ("test data");

Anyway, I can write strings fine. My problem occurs when I try to write out any other data type. It won't compile. I guess the toString method doesn't get called at compile time unless I explicitly call it?

My question is how can I write data to a single file from within my recursive method?
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you coded for write method ? Can you post that ?
You have mentioned that "when I want to write any other data type.." - what does that mean ?
 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For instance I can write

System.out.println (myInt);

and the system will convert the int to a string and write it out. I guess there is probably an overloaded version of println that takes an int, right?

I have created a toString method for my data object. I can call it explicitly and my log works. If I don't call it, my code doesn't compile. Normally I don't have to explicitly call a toString method. Does this also mean that I need to create a version of my write method that takes an Integer or explicitly call toString on everthing?

Here are my two methods in my Log class. I have taken some of the extra stuff out. I have found this class years ago and kept it until I found a need recently.

public static void init(String _fileName) {

//*******Create system dependent variable***********//

OSName = System.getProperty("os.name");

if (OSName.indexOf("Windows") >= 0) {

dir = "logs";

fileName = "\\" + _fileName;

} else {

dir = "logs";

fileName ="/" + _fileName;

}

}



public static void write(String str) {

//******************first check if the dir exists or not

file = new File(dir);

if (!file.exists()) {

try {

file.mkdirs();

} catch(Exception e) {

System.out.println(e);

}

}



//********Create or open the log File *************//

file = new File(dir + fileName);

if(!file.exists()) {

try {

file.createNewFile();

} catch(Exception e) {

System.out.println(e);

}
}
try {

fileOutputStream = new FileOutputStream(dir+fileName, true);

} catch(Exception e) {

System.out.println(e);

}



//************write the string to the file ************//

try {

str = str + "\n";

fileOutputStream.write(str.getBytes());

fileOutputStream.close();

} catch(Exception e) {

System.out.println(e);

}


If anyone has a more elegant way of writing to a log file without constantly opening and closing, I'm open for suggestions.

Thanks.
 
Rahul Bhattacharjee
Ranch Hand
Posts: 2308
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not think that its a properly designed class.

But first how to make this work ?

Change the parameter to write function to Object msg.
and then fout.write(((String)msg.toString()).getBytes());

it should work ?
[ September 09, 2007: Message edited by: Rahul Bhattacharjee ]
 
R Jarman
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it's not designed correctly, how would you go about designing a Log class? I'd like to be able to use this with other code I'm writing.

Thanks
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd go for the java.util.logger classes.

Re making your logger or other writer available, just define a member variable outside your recursive routine:


I hope you're not writing your own logger now, but re how System.out print methods work, you can download the source to the Java libraries and look them up. PrintStream has print() methods for each of the primitive types, most of which are one liners like:

The method for any Object is just as simple:

[ September 10, 2007: Message edited by: Stan James ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!