Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Reading and Writing txt files

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want to simultaneously read and write, read in a file, enter the lines into a data structure, then call a method that returns an augmented data structure which will be written to file. I also want to catch all exceptions and use the proper prompts when errors occur. Can someone help me clean up this code? Does my code catch exceptions generated by both files, or only one? I want to be specific and include the filename in the prompt as well...

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi John,

Have you tried putting each of your read and write functions into separate methods, where each method can have its own try catch block enabling you to locally catch your exceptions.

e.g. you might end up with something like:

private BufferedReader get reader(){...}
private PrintWriter getWriter(){...}
private void closeReader(){...}
private void closeWriter(){...}

private void writeNextLine(){
try{
getWriter().println( ( Queue.parseExpression(getNextLine()) ).toString() );
}catch(IOException ioe){
//do exception stuff
}
}

private String getNextLine(){
try{
return getReader().readLine();
}catch(IOException ioe){
//do exception stuff
}
}

public void doStuff(){
writeNextLine();
// etc...
}

Just a thought...

Cheers,

Zeb
 
John Lockheart
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks, but I want as little overhead as possible...something I can write inside my main method without creating an output stream over and over again. Isn't there anyway I can distinguish which file is causing the exception in the code I already have? When I tried writing a try catch block for closing the output file (when i wrote it seperate from input file), it gave my a compile time error saying that closing the output file wouldn't generate an IO Exception...is that right?
[ July 13, 2007: Message edited by: John Lockheart ]
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic