Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Buffered Reader Question  RSS feed

 
Keerthi Chandhu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am trying to pass a String with comments and then trying to buffer that String in a buffered Reader for further comparision and I am able to pass it without compilation errors but it is throwing a file not found exception.
fis = new FileReader(result ); // At this line throwing file not found exception b'coz it is taking result as a file name

please see the following code


Exceptions:


Please help me to correct this

Thank you

[Nitesh: Added code tags. Please UseCodeTags]
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
fis = new FileReader(result ); // At this line throwing file not found exception b'coz it is taking result as a file name

Well, yes. You're using a FileReader, which expects to be given a file to read. Apparently your "result" is a String which has nothing at all to do with files. So why use a FileReader? If you want to read the contents of a String rather than a file, there's another class in the java.io package that seems more pertinent than FileReader...
 
kannan vinayagam Duraiswamy
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For FileReader, in java doc
public FileReader(String fileName)
throws FileNotFoundException
Creates a new FileReader, given the name of the file to read from.
Parameters:
fileName - the name of the file to read from
Throws:
FileNotFoundException - if the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.


see the exception details
 
Keerthi Chandhu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Could you please suggest me if there is any way to pass a String value in a Buffered Reader.

Thank you
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If only the Javadocs listed a Reader that worked with Strings; sort of a... I don't know... String... Reader... Thing. Kind of.
 
Keerthi Chandhu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually I am reading a .properties File in a jsp screen and when I modify any value and save. I am loosing comments in the property file. To retain those comments.
I am trying to compare the String with comments and save it in .properties file.
For that I have to buffer it and read line by line and compare for comments. If comments are there then write that line in the properties file.
String result= " ##Properties start###
Props1=Key1
props2=key2
##### props end ###";
FileWriter propsFile = null;
FileReader fis = null;

BufferedReader br = null;
BufferedWriter propertiesFilewriter = null;

try {
fis = new FileReader(result );


br = new BufferedReader(fis); // reading String with comments where it is creating problem
propsFile= new FileWriter(Locfile);
propertiesFilewriter = new BufferedWriter( propsFile); //buffering properties file

String line;
while((line = br.readLine()) != null)

{
if(line.trim().length() == 0){
propertiesFilewriter.write(line);

}
else
{
line = line.trim();
if(line.charAt(0) == '#'){
propertiesFilewriter.write(line);
}
else
{
propertiesFilewriter.write(getModifiedContent(line, props));
}
}

propertiesFilewriter.newLine();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(br != null) br.close();

if(bcr != null) bcr.close();
if(propertiesFilewriter != null) propertiesFilewriter.close();
if(cfis != null) cfis.close();

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

Could you please suggest me how to read a String and buffer it for comparison or is there any other way to solve this issue?


Thank you
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you seen the Java API? In the upper left area, click on "java.io" to get a list of classes in the java.io package. One of these is FileReader, which you've already tried. You want to find another Reader, one that reads a String, not a File. Look through the names of the classes, and see if you can find any that sound like they might be helpful.
 
Keerthi Chandhu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mike,

I tried placing different Java API's but no luck. I am able to solve the issue by changing the requirement slightly by saving those properties with comments in a file and then buffering that file instead of reading string directly and now I am able to retain comments in the property file.

Thank you all for reading my post and helping me.


Thank you.
 
vipin joseph
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi please try this. hope this can solve your problem,

BufferedReader br = new BufferedReader(new StringReader("hello"));
 
Keerthi Chandhu
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vipin,

It is working!!!
Thank you somuch for helping me to solve my issue

Thank you.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line for the free fishin' lessons forms on the right.

Nobody?

Didn't think so.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!