Solved

Loading file to StringBuffer

Posted on 2003-12-08
12
4,203 Views
Last Modified: 2013-12-29
I am trying to load the entire file to a StringBuffer so that I can do some manipulation like delete. Below are the methods in my java class. I called the openInFiles in deleteProj method and try to loop through the file. I got a Java Null Pointer error. Is there any other way I can load this file into the StringBuffer. I don't know what I am doing wrong. I tried lot of printout and the problem is with the while loop. How do I indicate end of file with readline(). I urgently need help with this. Please help.

  public boolean openInFiles(String INFILE)  // return true if files open, else false
  {
    // open the source
    try
    {
      this.INFILE=INFILE;
      source = new BufferedReader(new FileReader( INFILE ));
     
        System.out.println("Inside openinfile " + source);
     
    }
    catch ( IOException iox )
    {
      System.out.println("Problem opening " + INFILE );
      return false;
    }

    return true;
  }


  public void deleteProj(String projectName){
                //file to open
      this.openInFiles(resource.CONFIGFILE);
      StringBuffer deleteBuffer = new StringBuffer();
      try
        {      
      line = source.readLine();
      System.out.println("Inside DeleteProj line value : " +line);

      while (line != null )
      {
//this is where I am trying to load the file to the StringBuffer
      deleteBuffer.append(source.readLine());
      }

//More codes
               .....
0
Comment
Question by:newtosvg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9901237
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9901244
newtosvg,
ignore my comment it was some other post.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9901257
Try

String line = null;
 try
       {      

     System.out.println("Inside DeleteProj line value : " +line);
     while ((line = source.readLine()) != null )
     {
//this is where I am trying to load the file to the StringBuffer
     deleteBuffer.append(line);


etc.
0
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
LVL 86

Expert Comment

by:CEHJ
ID: 9901269
(Naturally, there should be a close brace after

deleteBuffer.append(line);

)
0
 
LVL 92

Expert Comment

by:objects
ID: 9901376
> line = source.readLine();

'line' is only assigned to the first line read

>  while (line != null )

You then use 'line' in your loop test

> deleteBuffer.append(source.readLine());

you then read the next line, and append it to the buffer. This will eventually cause your NPE when your get to the end of file and attempt to append null.
You're also skipping the 1st line of the file (assigned to 'line').
0
 

Author Comment

by:newtosvg
ID: 9901551
Thanks guys. I do know that I am reading the first line and the value is <config> when I displayed it, which I am thinking it wouldn't be null at
while(line != null) and should be able to continue with the append.
You are right objects, but how do I solve the problem of the Null Pointer when the end of file is reached?
I need to load the entire file to  deleteBuffer so that I can do the delete below. Here is the entire code for deleteProj.

  public void deleteProj(String projectName){
      this.openInFiles(resource.CONFIGFILE);

      StringBuffer deleteBuffer = new StringBuffer();
      try
        {      
      line = source.readLine();
//      while (null != (line = source.readLine()))
            while (line != null )
            {
            deleteBuffer.append(source.readLine());
            }

  int startInd= deleteBuffer.toString().indexOf("<query name="+   projectName +"DefectsSnapshot\">");
  int endInd= deleteBuffer.toString().indexOf("<query name="+ projectName +"DefectsEntry\">");

    deleteBuffer.delete(startInd,endInd);
        }
      catch ( IOException iox )
      {
      System.out.println("Problem reading or writing" );
      }
  this.closeInFiles();
            
  }
 
0
 
LVL 92

Expert Comment

by:objects
ID: 9901581
As CEHJ suggested, read the line, testing whether it is null. And then append it to your buffer:

while ((line = source.readLine()) != null )
{
     deleteBuffer.append(line);
     ..
0
 
LVL 6

Expert Comment

by:dorothy2
ID: 9901952
As the other guys have mentioned, this is the usual format:

 while((line = source.readLine()) != null) { }

Write this down somewhere, you will use it a lot if you write programs regularly.

 While you're are in the early stages of development, why don't you print out lines?

Often developers have a boolean variable debug, which they set to true while they are coding and testing, and then they set the value to false before deploying.  You can add lnes of code like

if(debug)
     System.out.println(line);

Just don't delete debug when you don't need it any more, or your code won't compile.

Good luck,

Dorothy
0
 
LVL 24

Expert Comment

by:sciuriware
ID: 9902401
I did something like this before, but my append reads:

     buffer.append(line).append("\n");

to preserve the individual lines structure: all methods "readLine()" discard the line terminator!
You might want to save what you've got and what you are doing now is glueing everything into ONE line.

;JOOP!
0
 

Author Comment

by:newtosvg
ID: 9905151
CEHJ I tried   while ((line = source.readLine()) != null ) and it works with no Null Pointer. Now that I have the data in the buffer, I deleted some data of it and my question is How do I write the StringBuffer "deletebuffer" new value back to the original file. Thanks for all your help.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 140 total points
ID: 9905258
You can use:

 public void writeToFile(StringBuffer sb, String targetFile) {
    PrintWriter out = null;
    try {
      out = new PrintWriter(new FileWriter(targetFile));
      out.print(sb.toString());
      out.close();
    }
    catch(IOException e) {
      e.printStackTrace();
    }
    finally {
      out.close();
    }
  }
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9906649
8-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 476
How to convert from xls to xlsx using java 7 172
Eclipse for Java EE development 2 70
replace an existing file in Dropbox 1 10
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question