I would like to replace some items in a file, based on some regular expressions. In order to do that:
I read the file line per line
For every line, I check for the regular expression and I perform the replacement
Every line gets written in an array of strings
When all this is finished, I try to delete the file (in order to recreate it again with the replaced lines).
For some reason this does not work: it seems that Java keeps a handle on that file, even after the BufferedReader has been closed.
Does anybody have a solution for this (newbie) question?
Code excerpt:
Pattern oDatePattern = Pattern.compile("at \\d{2}:\\d{2}:\\d{2} "); // meaning: "at xx:xx:xx"
Pattern oTimePattern = Pattern.compile("Kernel time [0-9]*\\.?[0-9]+ User time: [0-9]*\\.?[0-9]+"); // "[0-9]*\.?[0-9]+" stands for any floating point number
Pattern oMemoryPattern = Pattern.compile("\\([0-9,A-F]*\\)"); // "[0-9,A-F]*" stands for any hexadecimal number
Matcher oDateMatcher;
Matcher oTimeMatcher;
Matcher oMemoryMatcher;
List<String> sLog_Content = new ArrayList<String>();
BufferedReader br = new BufferedReader(new FileReader(sLp_LogFile));
try {
String sLine = br.readLine();
while (sLine != null) {
System.out.println("ORIG : " + sLine);
oDateMatcher = oDatePattern.matcher(sLine);
sLine = oDateMatcher.replaceAll("at <timestamp> ");
oTimeMatcher = oTimePattern.matcher(sLine);
sLine = oTimeMatcher.replaceAll("Kernel time <Kernel_Time_usage> User time: <User_Time_usage>");
oMemoryMatcher = oMemoryPattern.matcher(sLine);
sLine = oMemoryMatcher.replaceAll("<Memory_Address>");
System.out.println("REPL : " + sLine);
sLog_Content.add(sLine);
sLine = br.readLine();
}
} finally {
br.close();
}
System.out.println("All lines are read and regex replaced, try to delete the file");
File tst_File = new File(sLp_LogFile);
if (tst_File.exists()) {
System.out.println(sLp_LogFile + " exists");
} else {
System.out.println(sLp_LogFile + " does not exist");
}
if (tst_File.delete()) {
System.out.println(sLp_LogFile + " is deleted");
} else {
System.out.println(sLp_LogFile + " is not deleted");
}
Output logs:
ORIG : Reading buffer 1 (0000000002ED0070) at 15:40:44 (index 125999, size 4410000 lines 126000, total lines read 126000)
REPL : Reading buffer 1 <Memory_Address> at <timestamp> (index 125999, size 4410000 lines 126000, total lines read 126000)
...
ORIG : Sending buffer 1 (0000000002ED0070) at 15:40:44 (index 125999, size 4410000, lines 126000, total lines sent 126000)
REPL : Sending buffer 1 <Memory_Address> at <timestamp> (index 125999, size 4410000, lines 126000, total lines sent 126000)
...
ORIG : Kernel time 0.2808 User time: 0.312
REPL : Kernel time <Kernel_Time_usage> User time: <User_Time_usage>
...
All lines are read and regex replaced, try to delete the file
D:\Logfile_lp.log exists
D:\Logfile_lp.log is not deleted
One possible explanation is that your application has the file open somewhere else.
Or it could be another application that has the file open.
Or maybe the application / user has permission to read the file but not to delete it.
I concur with the suggestion of using Files.delete ..
I see no issues in your code.
Seemingly closing the BufferReader ensure the file is closed. (cf this response).
Maybe you can give a try to Files.delete cf this response.
It will give more information about the deletion fail by throwing different exceptions.
Good afternoon,
I would like to thank you all for having searched for a solution of this problem. Unfortunately the problem is not Java based: the file I'm trying to write to is created by a redirection cmd /c <program>.exe >> <output>.log, and it seems that Windows has not fully flushed the output buffer towards the output file, creating the problem.
I am currently using following (very dirty) work-around for this issue:
boolean bFile_can_be_opened = false;
while (!bFile_can_be_opened) {
try {
fwLog2 = new FileWriter(sLp_LogFile, true);
bFile_can_be_opened = true;
}
catch (Exception e)
{}
}
Further information on this issue can be found under the following new StackOverflow question: How to release a file, locked by the application, in Java
I'm a beginner, I do not know as much things as you. But if I am right you should save your changes first a temp file. Afterwards you will read again the temp file and later you'll write to your real file. I hope my comment will help you.
Related
I am writing an application that writes to a text file.
In this text file, at the beginning of each line, is the line number
How do I get the current line number so that I can write it in my file?
I've thought of a simple counter, but when I terminate and restart my project the counter would reset back to 1.
I've tried LineNumberReader, but that keeps on giving me 0.
Is there any way to get the current line number when writing to a file?
Thanks
try {
FileWriter writer = new FileWriter("src\\book_store\\transaction.txt", true);
read = new FileReader("src\\book_store\\transaction.txt");
line = new LineNumberReader(read);
for(int i = 0; i < output.size(); i++) {
line.readLine();
temp = line.getLineNumber() + " " + timeStamp2 + " " + output.get(i) + " " + timeStamp + "\n";
output.set(i, temp);
writer.write(output.get(i));
}
writer.close();
read.close();
line.close();
} catch (IOException ex) {
System.out.println("File not found.");
}
The mistake you are making is that while you are writing to a file through FileWriter, you are trying to read the same file using FileReader.
This could potentially work if you fsynced the file after each line. However, this is not what you really want to do. As you see from the comments, people get confused as you are complicating the problem.
You already have the counter. It’s the i loop variable. You might add +1, if you want to start from 1.
I've got a loop that reads through a text file and outputs it, now I'm trying to get it to loop through, and write what's printed out into a text file as I want it to display as HTML. This is what I've got so far for this method:
public void hChoice()
{
File fbScores = new File ("P:/SD/Assignment1/fbScores.txt");
String line = "";
try {
Scanner scanScores = new Scanner(fbScores);
while(scanScores.hasNext())
{
line = scanScores.nextLine();
stringArr = line.split(":");
if(stringArr.length == 4)
{
System.out.println("<h1>" + stringArr[0]+" [" +stringArr[2]+"] |" + stringArr[1]+" ["+ stringArr[3]+" ]<br></h1> ");
PrintWriter out = new PrintWriter("P:/SD/Assignment1/HTMLscores.txt");
out.close();
}
}
}
catch (FileNotFoundException e)
{
System.out.println("problem " +e.getMessage());
}
}
I've added the HTML tags in the print out and it prints it out fine, but I've tried several different methods to get it to print to a text file but none have worked. Pretty new to Java so any help would be much appreciated. Thankyou. :)
You've gotten your syntax and code wrong for writing to files.
Please Google and check the right syntax for writing to files using java. Plenty of resources available. You'll learn better if you try it yourself.
FYR, here is one: http://www.tutorialspoint.com/java/java_files_io.htm
Here is my workflow:
I get job from DB, I run a few tasks, I run an external program that reads a file and produces another one (this usually takes under 10 seconds). Here is the code:
Process p = Runtime.getRuntime().exec(prog, null, new File(path));
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String s;
String errorString = "";
while((s = stdInput.readLine()) != null) {
if(s.toLowerCase().contains("error")) {
Log.writeLog("error: " + s);
errorString += s + "\r\n";
}
}
if(errorString.length() > 1) {
Emailer.email(name + "*" + errorString, "ERROR");
}
while((s = stdError.readLine()) != null) {
Log.writeLog("ERROR: " + s);
}
However, the snippet hanged. I control the server that the code runs on through LogMeIn, once I logged in, the process unblocked (total running time around 280 seconds) and continued. The process did not produce an ERROR results. This happens from time to time more often than we would like to. We do quite a bit of small IOs operation in the program and the harddrive gets pretty full from time to time.
Any idea what could be happening?
Thanks!
EDIT: the server is a just a regular computer that is connected to LogMeIn. My fear is that since it is a regular computer, it may powerdown the CPU/hard drive when not in use (not sure the correct terminology). This would somewhat explain why it would continue once I logged in to LogMeIn and had access to a computer.
EDIT2: directly following the process, I run this. And this hangs for an absurd amount of time as well (usually 5 seconds, took 200+ seconds). Makes me thing that the hard drive is decided to take a nap?
private void cleanup(String path) {
File srcPath = new File(path);
File[] files = srcPath.listFiles();
if(files != null) {
for(File file : files) {
if(file.isDirectory()) {
cleanup(file.getAbsolutePath());
} else {
if(file.getAbsolutePath().endsWith(".original")) {
String fileName = file.getAbsolutePath().substring(0, file.getAbsolutePath().lastIndexOf(".original"));
IO.delete(fileName);
if(!IO.renameFile(file.getAbsolutePath(), new File(fileName).getAbsolutePath())) {
Log.writeLogSevere("Failed to rename file, this could be a problem..." + fileName);
} else {
Log.writeLog("Cleaned up: " + fileName);
}
}
}
}
}
}
You are not draining the error stream. You do it at the end, which may often be too late. The output buffer of the process fills up and the process blocks waiting to get more space in the stderr output buffer.
You must either use a separate thread for that, or (much simpler) redirectErrorStream using ProcessBuilder.
The most likely thing is that the thread running p didn't die and p.getInputStream() is not null, and but not data on it.
While it's hanging, I would check the current running processes (ps command on Unix) or Task manager on windows. This will tell you if p is done or not. If it is not, then whatever that program is, has issues and it's holding up the rest of your code.
I'm trying to get input from the console of a .exe process started by a Java script. Nothing appears in the console window, and nothing is read by the program until the process is terminated.
blServ = new ProcessBuilder(blPath + "Blockland.exe", "ptlaaxobimwroe", "-dedicated", "-port " + port, "-profilepath " + blPath.substring(0, blPath.length() - 1)).start();
System.out.println("Attempting to start server...\n" + blPath);
consoleIn = new BufferedReader(new InputStreamReader(blServ.getInputStream()));
'blServ' is a Process object. And yes, the program is starting successfully.
public void blStreamConsole() //called once every 500 milliseconds
{
String lineStr = "";
String line = "";
int lines = 0;
try
{
if (consoleIn != null)
{
while ((line = consoleIn.readLine()) != null)
{
//if (!line.equals("%"));
//{
lineStr += line + wordSym;
lines++;
//}
}
}
}
catch (IOException e)
{
netOut.println("notify" + wordSym + "ERROR: An I/O exception occured when trying to get data from the remote console. Some lines may not be displayed.");
}
if (!lineStr.equals("") && !(lineStr == null))
netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr);
}
Basically, this method sees if there is more input waiting in the consoleIn object, and if there is, it appends every line it has to another string, and that other string is sent to a client. Unfortunately, it is all sent in one big chunk right when Blockland.exe is closed. Sorry about the indenting issues. The Stackoverflow editor re-arranged all of the code.
It seems to me that there are two possibilities here:
readLine blocks, waiting for input (and doesn't return null as you expect). You may be able to fix it by not using BufferedReader and instead using the InputStream
The output stream doesn't flush until all the input has been written. Try putting a flush there:
Also note that if lineStr is null, you'll get a NullPointerException as your code currently is (you need to swap your conditions), but it can't even be null.
if (!lineStr.isEmpty())
{
netOut.println("streamconsole" + wordSym + lines + wordSym + lineStr);
netOut.flush();
}
while ((line = consoleIn.readLine()) != null){
lineStr += line + wordSym;
lines++;
}
The problem with this piece of code is that it will keep running until the program exits. It will append every single line to lineStr until the program exits (when console.readLine() is null). The whole lineStr is then printed afterwards, containing the whole console.
If you want to continuously print the output, you will need to print it immediatly:
while ((line = consoleIn.readLine()) != null){
netOut.println(line);
}
You can run this in one separate thread, and it will keep outputting the console to the output stream until the program exits.
The code below reads from a file 'helpFile.txt' and checks for an index represented by '#'.
The variable read from the file is stored in the integer c and compared with '#' if the read character is '#' without casting the integer into character. I want to know if the comparison is valid as the compiler is not showing any error.
Also, suppose '#' is found by the program in the file and a string called 'topic' immediately follows '#' and it is read using readLine(). Will the 'String info = br.readLine()' be just 'topic' or '#'+'topic'?
Sorry for such a lengthy question. Help much appreciated.
boolean helpOn(String what){
private BufferedReader br;
private String info, topic;
private static PrintWriter pw = new PrintWriter(System.out, true);
int c;
br = new BufferedReader(new FileReader("helpFile.txt"));
try{
do{
//read characters until '#' is found
c = br.read();
if(***c=='#'***){ //check if the character is '#'
pw.println(c);
if((**topic=br.readLine()**).equalsIgnoreCase(what)){ //check if 'what' string is equal to 's' which is the string after '#'
while((info=br.readLine())!=null){ //print info until the end of info
if(info!=null)
pw.println(info);
}
return true;
}
}
}
while(c!=-1);
}
catch(IOException ex){
pw.println("File error.");
ex.printStackTrace();
try{
br.close();
}
catch(IOException e){
pw.println("Error closing file.");
e.printStackTrace();
}
return false;
}
try{
br.close();
}
catch(IOException ex){
pw.println("Error closing file.");
ex.printStackTrace();
}
return false; //topic not found
}
I tried your code, it is woking fine with me, i think u need to check your "helpFile.txt". i used this in it.
adad#hello
howareyou
and this is the outout i am getting.
c: 35
topic: hello
info: howareyou
i printed all the three vars u used. c, topic, info.
Now since you are using readline() after reading a character, u must give your "info" from next line in "helpFile.txt"
info will contain anything after topic, as you are using readline() function, it will go to the next line. try with my example.
As soon as "#" is encountered, your var
C will have "#" (35).
then
topic will have anything after the "#" till the end of line, because of readline();
then
info will have the next line after topic.
If you format your helpFile.txt properly, this will work fine
EDIT
i have to specify the full file name everytime
You are using eclipse, and you are saving the file in the "SRC" folder i guess. Save them in your Project folder. just one above the SRC folder and then do this.
br = new BufferedReader(new FileReader("helpFile.txt"));
it should work.
EDIT2
you don't need to check info for null twice
while((info=br.readLine())!=null){
//print info until the end of
// if(info!=null) noT needed, u alreay did that above
pw.println("info"+info);
}
If it is NULL, it will automatically come out of loop.
EDIT3
i don't want to print all the texts
As you used # to mark the begining of the block, you could use anything to mark the end of it. eg
helpFile.txt
adad#hello
howareyou
$
Other text here
blah blah blah...
...
Now, you can modify your while as:
while(!(info=br.readLine()).equals("$")){
pw.println("info"+info);
}
The loop will exit as soon as it gets "$", and it won't print anything after that.
You may wanna read the javadoc for the class you are using (BufferedReader), it can be found here, but i think it's ok to compare the characters with == because a char is really a numeric type. But i suggest you read more about enconding, because i think FileReader will use the default plataform encoding to read your file and that may not be your file encoding. It's always good practice to inform the encoding of the file.
About the second question, acoording to javadoc (again), it will read the entire line (not from the point you read the char). Hope that helps, i'm goona try to run this later to see the results