Results 1 to 5 of 5

Thread: A little help

  1. #1
    danielwestjr is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Question A little help

    I want 2 create an application class that reads data for an object from the keyboard ,create an object, and prints the object on the screen. It should also write the object to a file such that the created application class will read it from the file.
    What should I do this is what i have so far but I doubt it makes sense:
    import java.util.*;
    import java.io.*;

    public class StoreFile
    {
    public static void main (String[] args)
    {
    String file;

    Scanner scan2 = new Scanner(System.in);
    System.out.println("Enter a File Name : ");
    file = scan1.nextLine();

    try {
    FileWriter fw = new FileWriter (file);
    BufferWriter bw = new BufferedWriter (fw);
    PrintWriter outFile = new PrintWriter (bw);

    for (file);
    {
    outFile.print ("File Name is : " +file);
    }
    output.println();
    outFile.close();
    System.out.println("Output File created was : " + file);
    }
    catch (Exception e) {
    System.out.println(e);
    }


    }
    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    What is your question?
    create an object
    This sounds like you need to define a class to hold the data.
    write the object to a file
    Do you want to use the writeObject() method to write the object?
    If the data in the object is only String, there are other simpler ways to save the contents of an object to a file.

  3. #3
    Eku
    Eku is offline Senior Member
    Join Date
    May 2008
    Location
    Makati, Philippines
    Posts
    234
    Rep Power
    7

    Default

    hmm. . . let me give you some snippets and try editing them on your style and put them in your code =)

    This Code will get any String input from your Keyboard and store them in a String
    Java Code:
    BufferedReader input = new BufferedReader(new InputStreamReader(System.in)); //initialization of the buffered reader
    String InputString = null; //initialization of the String
    System.out.print("Enter your input here: "); //prompting the user to input a String
    try{
                InputString = input.readLine(); //Getting the String to the variable from the buffered reader
    }catch (Exception A){
                A.printStackTrace(); //in case of an error occured you can debug it here
    }finally {
                input.close(); // Always close your connections
                System.out.println("You entered: " + InputString); //Display the input String
    }
    To Save to File you can use this simple approach. Just modified it to suit to your system.
    Java Code:
    try{            
                        FileWriter fstream = new FileWriter(fileName ,false); //instantiate a Stream where file name is the location, false means overwrite if already exist set to true if you want to make it to append
                        BufferedWriter out = new BufferedWriter(fstream); //Create a buffered writer
                        out.write("Write me! ^_^"); //write the String to the File
                        out.close(); // close the bufferedwriter
                        fstream.close(); // Always close the Stream if you dont need it anymore
           }catch (Exception A){
                        A.printStackTrace(); //In case there is an error and you want to debug it
           }
    I hope that helps
    Mind only knows what lies near the heart, it alone sees the depth of the soul.

  4. #4
    danielwestjr is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    well it sorta helped but thats probably a bit too advanced. I just wanted to write the object to a file so that i can be read from the using filewriter but the user chooses the file name from a keyboard input

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,318
    Rep Power
    25

    Default

    Can you describe what you want to do in more detail. Your description is too general. Many programs read and write data to files and read input from the user.
    write the object to a file
    What is the "object" you want to write? A String? or what?

    Break the project up into small steps:
    1) get the filename from the user
    2) get the data to write to the file
    3) write the data to the file.
    4) get a filename from the user
    5) read the file and display its contents

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •