Easy java question

Status
Not open for further replies.

JeffN825

Smack-Fu Master, in training
88
I'm a VERY novice programmer and I need a little bit of help with a piece of code I'm working on. As the subject of this message suggests, I am working in JAVA. My first problem is with outputting information to a file. I'm using BufferedWriter and basically the following two lines of code to accomplish this task.

PrintWriter outFile=new PrintWriter(new BufferedWriter(new FileWriter("testout.txt")));

outFile.write("hello");

This code, when run, creates the text file specified, but does not output anything to it. My second question is even simpler: how would I go about having JAVA find out how many lines long an input file it is parsing for information is? All help is appreciated. Thanks in advance.
 

sorghumking

Ars Scholae Palatinae
989
Writing information to a file bit by bit is a very inefficient operation - it's generally quicker to gather (buffer) a fair amount of data, then write it all at once. The BufferedWriter class manages this operation for you, writing the buffered data to the file (a process known as flushing) when the buffer is full. "Full", in this case, is the default buffer size, which is fairly large - definitely larger than five characters ("hello"). There are a number of ways to deal with this, the easiest being to call the PrintWriter.flush() method when you're done writing to the file. This should guarantee that everything you've dumped into the buffer actually ends up being written.

To count the number of lines in a file, it's not pretty, but it should work:

code:<HR>
Code:
FileInputStream fis = new FileInputStream("foo.txt");
int lines = 0;
while (true) {
   int i = fis.read();
   if (i == -1)
      break;
   if ((char)i == '\n')
   lines++;
}
System.out.println("lines: " + lines);
 

Evil Dale

Smack-Fu Master, in training
51
You don't need to wrap your BufferedWriter in the PrintWriter: Just make the write calls directly to the BufferedWriter. And remember to close() it when you're done, otherwise it may/may not actually write the data to the file. Calling flush() before close() is also probably a good idea to be safe.

An easy way of counting the number of lines in a text file is to construct a new BufferedReader(new FileReader("filename")), and loop calling readLine() and incrementing a counter every iteration. You have reached the end of the file when readLine() returns null.
 

ssmith

Ars Scholae Palatinae
928
Assuming that the output is OK, counting the lines should be simple:
code:<HR>
Code:
public int countLines( Reader aReader )
{
   // using line number reader, you could just as easily use a BufferedReader
   // and count the lines coming in
   java.io.LineNumberReader lineReader = new java.io.LineNumberReader( aReader );
   String lineIn = null;
   while( null != ( lineIn = lineReader.readLine( ) ) ) { }
   
   return lineReader.getLineNumber( );<P>}

edit: put in CODE tags....
 
Status
Not open for further replies.