File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to read a txt file with space

 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
I am trying to read a txt file shown below
PRODUCT INFORMATION FORM
Title of application: Selling Tips
Number of products: 2
Product Name: TissueMend
Photo: tm.jpg
Description: TissueMend(r) is a strong, suturable, porous collagen biomembrane that is derived from fetal bovine dermis.
Indications: Generally indicated for surgical procedures to reinforce and repair soft tissue where weakness exists.
Number of Competitors: 1
Competitor Name: Zimmer
Competitor Description: They have bones.

I have the following questions
1. Is there some way to find the end of file is reached.
2. Also any insight, if its is possible to read a Ms-Word directly or save it to txt file
3. The curent code which is as follows throws a null pointer exception whenever bufferedReader.readLine() reads null.
System.out.println(".........Inside readMyFile");
BufferedReader br = null;
String line = null;
String num_Of_Products = null;

StringBuffer buf = null;
boolean cond = true;
try {

br = new BufferedReader(new FileReader(path));

do{

line = br.readLine();
System.out.println(line);
if(line.length()==0)
System.out.println("line is null");
counter++;
if(counter >4)
cond=false;
buf.append(line);
}while(cond);

}
catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}
catch (IOException e) {
System.out.println("ioexception" + e.getMessage());
e.printStackTrace();
}
finally {
if (br !=null) {
try {
br.close();
}
catch (IOException ioe) {
}
}



}
return buf.toString();
}
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vikram nalagampalli:

1. Is there some way to find the end of file is reached.

According to the API Documentation:

public String readLine()
Returns: A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached

Let me clarify this concept for you:

This is incorrect. if line.length() is 0 the String object is empty (i.e. a blank line). If you called line.length() on a null line you would get a null pointer exception. Check for null by comparing line==null.

2. Also any insight, if its is possible to read a Ms-Word directly or save it to txt file

If one is able to read in a file using BufferedReader, one may postulate that one can write a txt file using BufferedWriter.
[ December 09, 2003: Message edited by: Joe Ess ]
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was initially checking for line == null, it used to give me Null Pointer Exception. so i tried the other way checking line.length = 0;
but since i found another solution to access Word and Excel directly through jakarta POIs, i started looking into POI API.
So whether i got a solution to skip empty lines in ASC...Answer is no. I will really appreicate, if you can throw some light into this as i am curious to know why its throwing me that Null Pointer Error.
Thanks for answering my questions Joe, I really appreciate it.
 
Joe Ess
Bartender
Posts: 9214
9
Linux Mac OS X Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vikram nalagampalli:
I was initially checking for line == null, it used to give me Null Pointer Exception.

No it will not. Were you calling line.equals() by any chance? line == null evaluates the String handle "line". If line is null, this comparison returns true. line.equals() invokes a method on the String object the handle line points to. If line is null, you will get a null pointer exception.
 
Adrian Yan
Ranch Hand
Posts: 688
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just do this:
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how dumb i am...... I see it now......I will try it out. I strongly feel it is going to work now....
Thanks for the help Joe.....
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
currentRow.equals("null") dont want to work either. Suprisingly, I am never able to go inside my If block where i check the above condition.
It is throwing me a Null Pointer exception, even before it reaches the IF block (i.e)The very Moment CurrentRow is null
Let me know if you have ideas to tackle this.
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(FILE_NAME));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow currentRow = sheet.getRow(0);
int lastRowNum = sheet.getLastRowNum();
int rowCount = 0;
int arrayIndexCounter = 0;
while (currentRow.getRowNum() < lastRowNum) {
currentRow = sheet.getRow(rowCount++);
if(currentRow.equals("null")){
System.out.println("Inside If CondSSS");
continue;
}

}
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is throwing me a Null Pointer exception, even before it reaches the IF block (i.e)The very Moment CurrentRow is null
Well, look at the stack trace. It should tell you exactly which line is throwing the exception. Find that line number in the appropriate code and look at it. If you can't tell why it's throwing NullPointerException, then show us which line it is. That will be much easier than us trying to guess.
[ December 15, 2003: Message edited by: Jim Yingst ]
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public String getTitle(){
String title =null;
try {
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(FILE_NAME));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow currentRow = sheet.getRow(0);
HSSFCell firstCell = null;
HSSFCell secondCell = null;
int lastRowNum = sheet.getLastRowNum();
int rowCount = 0;
int arrayIndexCounter = 0;

while (currentRow.getRowNum() < lastRowNum) {
currentRow = sheet.getRow(rowCount++);
if(currentRow.equals("null")){
System.out.println("Inside CurrentRow NULl");
currentRow = sheet.getRow(currentRow.getRowNum()+1);
}

firstCell = currentRow.getCell((short) 0);
secondCell = currentRow.getCell((short) 1);

if((firstCell.getStringCellValue().equalsIgnoreCase("null"))){
System.out.println("Inside FirstCell NULl");
currentRow = sheet.getRow(currentRow.getRowNum()+1);
firstCell = currentRow.getCell((short) 0);
secondCell = currentRow.getCell((short) 1);

}
if ("Title of application".equalsIgnoreCase(firstCell.getStringCellValue())) {
title =secondCell.getStringCellValue();

arrayIndexCounter += 1;
}
}


} catch (FileNotFoundException fe) {
fe.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e1) {
e1.printStackTrace();
}


return title;
}
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Forgot to Include Exception for the above Message
The line Number Refers to the one bolded above.....Any cooments will be greatly appreicated.
Thanks
java.lang.NullPointerException
at com.so.wordxml.WordXMLMain.getTitle(WordXMLMain.java:799)
at com.so.wordxml.WordXMLMain.createTitle(WordXMLMain.java:872)
at com.so.wordxml.WordXMLMain.actionPerformed(WordXMLMain.java:119)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1461)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1515)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:392)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:264)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:254)
at java.awt.Component.processMouseEvent(Component.java:3799)
at java.awt.Component.processEvent(Component.java:3628)
at java.awt.Container.processEvent(Container.java:1195)
at java.awt.Component.dispatchEventImpl(Component.java:2678)
at java.awt.Container.dispatchEventImpl(Container.java:1244)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java(Compiled Code))
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java(Compiled Code))
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2252)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2161)
at java.awt.Container.dispatchEventImpl(Container.java:1231)
at java.awt.Window.dispatchEventImpl(Window.java:964)
at java.awt.Component.dispatchEvent(Component.java:2581)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:434)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:172)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:131)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:126)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:118)
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[B]
[/B]
If that's the line that's throwing a NullPointerException, then something to the left of a . is null. Not "null", but null. Try this:
 
vikram nalagampalli
Ranch Hand
Posts: 91
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That was of great help, Thanks Jim
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic