aspose file tools*
The moose likes I/O and Streams and the fly likes problem in reading a file. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "problem in reading a file." Watch "problem in reading a file." New topic
Author

problem in reading a file.

Ankit Tripathi
Ranch Hand

Joined: Oct 17, 2009
Posts: 198
I want to read the frequency of each character through my java program.It has compiled successfully but when i am running it,it is showing me null pointer exception.what is the
problem with it.




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;

public class FileInput {
char ch;int count;

public static void main(String[] args) {
FileInput sts = new FileInput();



InputStream is = sts.getClass

().getResourceAsStream("F:/a.txt");


System.out.println

(sts.convertStreamToString(is));
}

public String convertStreamToString

(InputStream is) {

BufferedReader reader = new

BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();

String line = null;
try {
while ((line = reader.readLine()) != null )

{
sb.append(line + "\n");
if(ch=='E')
{
count=count+1;
}
System.out.println(count);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

return sb.toString();
}
}
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

I did a bit of a formating (yours job actually) ... learn how it is done.


Anyhow, regarding your problem.
Method getResourceAsStream returns null in case the resource is not found.
You have to specify a vaild URL name in order to work ... at lesat for getResourceAsStream() or getResource().openStream().


Regards,
Rok


SCJP, SCWCD
Somnath Mallick
Ranch Hand

Joined: Mar 04, 2009
Posts: 477

Shouldn't it be:
Rok Štelcer
Ranch Hand

Joined: Nov 03, 2009
Posts: 101
Hi,

Shouldn't it be:
InputStream is = sts.getClass().getResourceAsStream("F:\\a.txt");

This is not a valid URL file name.
Check the following link:
http://java.sun.com/docs/books/tutorial/networking/urls/index.html

Regarding the file reading, check the following link (various examples):
http://www.google.si/search?hl=sl&client=firefox-a&rls=org.mozilla%3Asl%3Aofficial&hs=ZER&q=java+how+read+a+file&btnG=Iskanje&meta=&aq=f&oq=


Regards,
Rok
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Also, the "getResourceAsStream" method searches your classpath for the resource, and the URL you provide is relative to the class on which you call the method. Unless you put a slash at the beginning of the URL, in which case the URL is considered to be in the root of one of the entries of the classpath.

But if you want to specify a file name, the easiest change to make would be to not use that method, but to use an ordinary FileInputStream.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in reading a file.