473,508 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File saving

4 New Member
I'm learning Java for about 2-3 weeks and I'm stuck and I'm not sure if I have the car in front of my horses or no:
All the programs I learned from the book (yep that one for dummies) is about posting data on the screen or saving data into a file but the data was already pre-written in the code. I want to my program to take what I write on the keyboard, I will see on the screen and then that will be saved on a file. I want to be able to type a few things before the program ends like:
Name : Luis Barros
Age : 99
State : New Jersey
etc....
Then the program will end when I type exit or quit.
Is that too complicated and I should buy another book -the one for idiots or is normal to be on the pause mode when we are learning??
Thanks in advanced for your answers.
Dec 15 '07 #1
5 1822
BigDaddyLH
1,216 Recognized Expert Top Contributor
I/O, especially console-based I/O can be fiddley. Imagine trying to get the user to enter a number from 0 to 100. You have to try to parse the string entered to make sure it first can parse, that it's "50", not "fifty", and second that it's in range and not -4 or 104. If this fails, you may want to repeat your attempt at input. Tedious.

That being said, getting input from the console window is no harder that getting it from a file. Here is a demo that copies that input to a file.

Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class SaveToFile {
  4.     public static void main(String[] args) throws IOException {
  5.         System.out.println("Enter text. End with a blank line.");
  6.         BufferedReader in = new BufferedReader(
  7.             new InputStreamReader(System.in));
  8.         PrintWriter out = new PrintWriter("temp.txt");
  9.         String line;
  10.         System.out.print(">");
  11.         while ((line = in.readLine()) != null && !line.isEmpty()) {
  12.             out.println(line);
  13.             System.out.print(">");
  14.         }
  15.         out.close();
  16.         in.close();
  17.     }
  18. }
I've never looked at a Dummies book for Java so I can't comment on them. There are other free resources out there you could look at. For example, Sun's tutorials:

http://java.sun.com/docs/books/tutorial/

This site has it's own "howtos" as well:

http://www.thescripts.com/howtos.php#link163

And if you haven't already, always keep a browser tab open to the API:

http://java.sun.com/javase/6/docs/api/
Dec 15 '07 #2
slan
4 New Member
I used part of your code and everything works fine..but will not save into a file :-)))
Seeing my code you can tell me what's wrong?
Thanks

Expand|Select|Wrap|Line Numbers
  1. import static java.lang.System.out;
  2. import java.util.Scanner;
  3. import java.io.*;
  4.  
  5. class Info {
  6.     public static void main(String args[]) throws IOException {
  7.  
  8.         Scanner myScanner = new Scanner(System.in);
  9.         out.print("Serial Number : ");
  10.         String num = myScanner.next();
  11.         out.print("Model : ");
  12.         String mod = myScanner.next();
  13.         out.print("Parts : ");
  14.         String part = myScanner.next();
  15.         out.print("Press Enter to exit the program ");
  16.         String press = myScanner.next();
  17.  
  18.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  19.         PrintWriter out = new PrintWriter("e:\\java\\data.txt");
  20.  
  21.         while ((press = in.readLine()) != null && !press.isEmpty()) {
  22.             out.println(press);
  23.         }
  24.         out.close();
  25.         in.close();
  26.  
  27.  
  28.     }
  29. }
  30.  
  31.  
Dec 15 '07 #3
BigDaddyLH
1,216 Recognized Expert Top Contributor
First, I'm not a fan of static imports. In your code, I'm wondering if you are confusing the two uses of "out" -- one out is System.out, the other is the PrintWriter that writes to the file. Here is your code disambiguated. Do you understand why it does what it does, now?

Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. class Info {
  5.     public static void main(String args[]) throws IOException {
  6.         Scanner myScanner = new Scanner(System.in);
  7.         System.out.print("Serial Number : ");
  8.         String num = myScanner.next();
  9.         System.out.print("Model : ");
  10.         String mod = myScanner.next();
  11.         System.out.print("Parts : ");
  12.         String part = myScanner.next();
  13.         System.out.print("Press Enter to exit the program ");
  14.         String press = myScanner.next();
  15.  
  16.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  17.         PrintWriter fileOut = new PrintWriter("data.txt");
  18.  
  19.         while ((press = in.readLine()) != null && !press.isEmpty()) {
  20.             fileOut.println(press);
  21.         }
  22.         fileOut.close();
  23.         in.close();
  24.     }
  25. }
  26.  
Dec 15 '07 #4
BigDaddyLH
1,216 Recognized Expert Top Contributor
Or did you want to do something like this?
Expand|Select|Wrap|Line Numbers
  1. import java.util.Scanner;
  2. import java.io.*;
  3.  
  4. class Info {
  5.     public static void main(String args[]) throws IOException {
  6.         Scanner myScanner = new Scanner(System.in);
  7.         System.out.print("Serial Number : ");
  8.         String num = myScanner.next();
  9.         System.out.print("Model : ");
  10.         String mod = myScanner.next();
  11.         System.out.print("Parts : ");
  12.         String part = myScanner.next();
  13.  
  14.         PrintWriter fileOut = new PrintWriter("data.txt");
  15.         fileOut.format("Serial Number : %s%n", num);
  16.         fileOut.format("Model : %s%n", mod);
  17.         fileOut.format("Parts : %s%n", part);
  18.         fileOut.close();
  19.     }
  20. }
Have you studies the I/O tutorial at Sun?
http://java.sun.com/docs/books/tutor.../io/index.html
Dec 16 '07 #5
slan
4 New Member
Have you studies the I/O tutorial at Sun?
http://java.sun.com/docs/books/tutor.../io/index.html
That's it, now after you did it looks easy :) I did some of the Sun's tutorial but I was getting obcessed for the fact I couldn't do something like that, everywhere I looked I couldn't find a sample were I could based on it. Now I'm going to do that tutorial, and I bet the answer I was looking will jump in front of my eyes.
Thank you.
Luis Barros
Dec 16 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
12008
by: Alper Adatoz | last post by:
Hi, i have a little problem. i hope u guys give me a clear solution (: db: mssql i just want to put jpeg file to the image field at the mssql db. and after that i want to call it back..
7
16211
by: Martin | last post by:
Again drawing on the groups experience:- 1. For general file opening and file saving, using VB6, are there any issues with using the FileOpen and FileSave Common Dialog Boxes? 2. Is using the...
7
7511
by: G-Factor | last post by:
Hi all I've just started learning about saving files. I got bit of a problem. The following code gives me an error about incompatible types. (Cannot covert from class character to char *). I...
13
9475
by: Bob Darlington | last post by:
I have a repair and backup database routine which runs when a user closes down my application. It works fine in my development machine, but breaks on a client's at the following line: If...
3
1615
by: NuB | last post by:
I want to allow the user to upload a file to a server from my windows form and then display that file name in a grid. When the user clicks on the file name i want the user to be able to open the...
8
1217
by: Corey B | last post by:
I am writing a web application in ASP.NET that allows a user to download an XML file to their machine that contains all of their work. Then later they can upload that file to the server and...
6
6427
by: Mark Denardo | last post by:
My question is similar to one someone posted a few months back, but I don't see any replies. Basically I want to be able to have users upload photos and save them in a database (as byte data)...
0
2009
by: Speilman_54 | last post by:
Hi, I'm converting an excel Macro into visual basic 2005 express, as I don't have a copy of VB 6 and trying to make and executable from it, I know this version doesn't have the save file as .exe,...
0
1413
by: Waqas.L.Khan | last post by:
Hi guys, I have a problem when trying to create an image file. Basically my code takes any file and gets it's system icon using SHGetFileInfo and then saves the file either by converting it into...
6
3344
by: Karl | last post by:
Hi all, It may seem like a rather odd request (or not) but I would like to be able to create a file (doc, jpg, xls or one of many other files that can be automated) on a website and stream it to...
0
7129
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7398
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7061
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7502
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5637
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4716
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3194
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1566
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.