Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Read and write last lines  RSS feed

 
Amy Howard
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is anyone know how to read only certain last lines from a given file and save it in a file?
public File downLoadFile(File file, int lastLines){
...
}
Thanks for your help in advance!
 
jason adam
Chicken Farmer ()
Ranch Hand
Posts: 1932
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a LineNumberReader that can help you with this, I believe.
Is the number you are passing in the number of lines at the end of the file you want to read, or the line number you want to start reading at?
 
Amy Howard
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wrote the following test code, but the output file format is not the same as input source file. Do you know how to fix this problem?

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.io.*;

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

FLVDownLoad f=new FLVDownLoad();

StringBuffer newCutFile;

newCutFile=f.readAndCutFiles(new File("C:\\var\\websphere4\\logs\\ArtSecurityLog.log"),1);

try{
File file=new File("C:\\var\\websphere4\\logs\\tempLogFile.log");
FileOutputStream outstr=new FileOutputStream(file);
ObjectOutputStream outStream = new ObjectOutputStream(outstr);
outStream.writeObject(newCutFile);

}catch (IOException e) {
System.out.println("File write error: " + e.toString());
}

}

//Description: This method is for reading and cutting of files.
//This would display the latest n lines.
//Parameters: file - File to be read.This should be of File type. numLines - the n number of lines you want to display

public StringBuffer readAndCutFiles(File file, int numLines){
StringBuffer display_line = new StringBuffer();
try
{
String line = "";
long number = 0;
long cursor = 0;
BufferedReader count = new BufferedReader(new FileReader(file));
BufferedReader in = new BufferedReader(new FileReader(file));
while ((count.readLine())!=null)
{++number;}
count.close();
if (number>numLines)
{
for (cursor = 0; cursor<number-numLines; cursor++){
in.readLine();
}
while ((line=in.readLine())!=null){
display_line.append(line);
display_line.append("\n");
}
}else{
while ((line=in.readLine())!=null){
display_line.append(line);
display_line.append("\n");
}
}
}catch(Exception ex){
System.out.println("Exception is: " + ex);
}
return display_line;
}

}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like the problem is probably that you're using an ObjectOutputStream to write the output. That's only useful for object serialization, not for writing text files. Try replacing it with some sort of Writer - a simple FileWriter would work, or you can wrap it with a BufferedWriter and/or PrintWriter.
 
Amy Howard
Ranch Hand
Posts: 86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I have got it works using FileWriter. But I don't know how to wrap it with a BufferedWriter. Can you tell me how to that?
FileWriter outStream=new FileWriter(file);
outStream.write(newCutFile.toString(), 0, newCutFile.length());
outStream.close();
[ March 14, 2003: Message edited by: Amy Howard ]
[ March 15, 2003: Message edited by: Amy Howard ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!