• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to save file with condition in File IO java

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey all
i am totally new to this forum and i am not sure if i am putting this quesiton in right segment. if i am not sorry for that . i can't seems to figure out how to save file. i have two file namely lower.txt and upper.txt. i can't figure how to give condition or say like if the user input is less than number 60 then save that file in lower.txt else in upper.txt. can anyone please suggest what kinda code to use and where in try n catch statement should put in ?
thanks
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you already know how to do basic file I/O?

If so, then all you need to do is apply the desired file name when you create the File instance.

if not, then the tutorial should get you going.
 
ten chang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks for the quick response, i have all the user input in place the only problem was i couldn't redirect if the input has more than 60. i can only save in lower.txt
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And welcome to the ranch.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ten chang wrote:thanks for the quick response, i have all the user input in place the only problem was i couldn't redirect if the input has more than 60. i can only save in lower.txt



So you don't know how to write an if statement?

Or you don't know how to test if some value is greater than 60?
 
Tony Docherty
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your code (or as much as is relevant) so we can see what you are doing. Also what does " if the input has more than 60" mean. Are you talking about the ASCII value of a character?
 
ten chang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried with


am not sure if this is the right way
 
ten chang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the greetings..Tony
This is the full code

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

ten chang wrote:i tried with


am not sure if this is the right way




And when you looked at the documentation for that write() call, it told you that the Writer will attach itself to the file name you pass, and write out whatever you're thinking of, without you telling it what to write?

Because that how you must think it behaves, based on what you said you're trying to do.



 
ten chang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest i thought it's gonna write and save user input in either lower.txt or upper.txt. but i guess within the parameter should be things you want to write.

 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not "within the parameter", no. Once you have a Writer, then you call methods of the Writer which output whatever you want to write. Since you have a BufferedWriter (not a bad choice), here's a link to a tutorial about how to do that.
 
ten chang
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Paul.. after scratching my head whole lot of time finally made it worked... now i need to show the record one by one ... that i entered..
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic