Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 285
  • Last Modified:

how to read and re-write a line in a file?

Dear experts,

My application needs to test the first line of the file it reads. If there is space before the first char of the first line, I need to "move" the whole line forward, which means get rid of the space in front. What is the right way of doing this? thanks.
0
changcy77
Asked:
changcy77
  • 7
  • 4
  • 4
2 Solutions
 
lwinkenbCommented:
FileInputStream fis = new FileInputStream("file.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fis);

String line = br.readLine();
String file = line;

if(line.charAt(0) == ' ') {
  while((line = br.readLine()) != null)
    file += line;
  file = file.substring(1);
  br.close();
  FileOutputStream fos = new FileOutputStream("file.txt");
  PrintWriter outFile = new PrintWriter(fos);
  outFile.print(file);
  outFile.close();
}
else {
  br.close();
}
0
 
lwinkenbCommented:
also you can also replace
file = file.substring(1);
with
file = file.trim();
if you want to remove all the whitespace at the beginning and end of the file.
0
 
changcy77Author Commented:
so, your solution is re-write the whole file instead of just re-write one line? Is it possible to just do one line, which might save some effort?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
lwinkenbCommented:
>>Is it possible to just do one line, which might save some effort?
No, not possible.
0
 
mrigankCommented:
http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20733355.html

public static void main(String args[])    throws Exception
{
   RandomAccessFile f = new RandomAccessFile("d:\\file.txt","rw");

   String firstLine = f.readLine();
   int length = firstLine.length();
   while(firstLine.substring(0,1).equalsIgnoreCase(" ") && firstLine.length() > 1)
   {
      firstLine = firstLine.substring(1);
   }
    f.seek(0);

   int newLength = firstLine.length();
   if(length != firstLine.length())
   {
     System.out.println("White Spaces :: " + (length - newLength));
     byte b[] = new byte[(new Long(f.length())).intValue()];
     f.readFully(b);
     f.seek(0);
     String allFile = new String(b);
     String newFile = allFile.substring(length - newLength);
     f.writeBytes(newFile);
     f.seek(0) ;
     f.setLength((new Long(f.length())).intValue() - (length - newLength));
   }
    f.close();
  }
}

0
 
mrigankCommented:
You can do the required action in one go in the above case ...
0
 
lwinkenbCommented:
That code has a lot more overhead than the code I posted.

Instead of reading the file to a string then rewriting it, you are reading part of the file to a string, then reading the whole file to a byte array, then copying that byte array to a string, then copying that string to another string, then writing that string back to the file.
0
 
mrigankCommented:
My code definitely has overheads ... in fact if the file is in kbs and mbs then the entire file cannot be read also at once ... i posted the code in case simultaneous read and write is required ....
0
 
lwinkenbCommented:
I have no problem with people posting alternative solutions, that is what makes experts-exchange great :)

>>in fact if the file is in kbs and mbs then the entire file cannot be read also at once
That depends on how much RAM the user has.  Most people wouldnt have any problem loading a file that is multiple MB into memory.

>>i posted the code in case simultaneous read and write is required
RandomAccessFile is good for when you want to append or overwite files.  It is substantially slower than BufferedReader, so it is best to use BufferedReader if you have to read the whole file into memory (which both of our examples do).
0
 
mrigankCommented:
>>I have no problem with people posting alternative solutions, that is what makes experts-exchange great :)

yup ... agreed :) ...

and thanks for your input on BufferedReader and RandomAccessFile :) ....
0
 
changcy77Author Commented:
Thanks to all input. However, when I use

 file+=line;

I found the trailing space of the first line "disappeared". How could I keep the trailing space when I also concatenate two strings? thanks again.
0
 
lwinkenbCommented:
sorry, you should change:

while((line = br.readLine()) != null)
    file += line;

to the following:

file = br.readLine();
while((line = br.readLine()) != null)
    file += "\r\n" + line;

0
 
changcy77Author Commented:
Thanks.

So, string concatenation just "naturally" delete the trailing spaces?  
0
 
lwinkenbCommented:
the problem wasnt that it was deleting trailing spaces, the problem was that it didnt include the new line characters (\r\n).    When you call br.readLine(), it returns the string up to the new line characters, but it doesnt include them.  So when you concat all these strings together, you are missing those.  So if the file looked like:

this file should have
new line characters
placed at the end
of each line.

then without adding in the "\r\n" it looks like:

this file should havenew line charactersplaced at the endof each line.
0
 
changcy77Author Commented:
thanks.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now