Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 21 of 21

Thread: File I/O

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File I/O

    Hello,
    I am new here so if my post is not within any guidelines i apologize i did search for this topic before i posted. I am a little confused on my current project and was hoping for some clarification. I am to write a program that asks the user to enter the name of the text file and then displays (StringTokenized) line, word, and char counts for the total output. The text file is one that i write as output to be stored behind the scenes. This program uses file I/O. My main difficulty at this point is that i understand how to have the output written to the .txt file in the computer (via a series of print statements) and i am able to access the file. I don't understand how the user would input the file? Would i have a class that does the output behind the scenes, and then the user input class to have the file accessed? Thank you.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    how the user would input the file
    Do you mean the contents of the file
    or the name of an existing file?

    See the Scanner class for an easy way to read input from a user from the console.

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    I am given the contents to make the file (6 lines of words). I make the file using output. I see the scanner and input stuff i am just not understanding what exactly to do? I have to ask the user to enter the name of a file (which there going to be entering the name of the file which i already created. Then i have to access that file, and then parse the tokens from the input lines?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    You can also use the Scanner class to read the contents of the file.

    There are many sample programs on the forumthat use the Scanner class. Do a Search here.
    Also be sure to read the API doc for the Scanner class.
    Java Platform SE 6

  5. #5
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    Thank you for that. Do i need to have both input and output inside my try block though? I do not understand where the output file is supposed to be implemented? Do i use the input and output statements inside the try block or do i just implement the input statement?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    It does not cause problems if you put extra statements inside of the try block.
    The one thing that can be a problem is the scope of any variables defined inside a try block. They won't be known outside of the enclosing {}s.
    Be sure to define all variables that you want to use at the same nesting level of {}s so that they are in scope where you try to use them.

    If you read the API doc for the methods you are using, it will say if the method can throw an exception and needs to be in try/catch blocks. The compiler will also tell you when you try to compile the code.

  7. #7
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    When the assignment says "Write a program that asks the user to enter the name of a text file and then displays the counts", does that mean that i will have the question pop up in the output and create a scanner object to allow the user to enter a text file name?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    I assume that "asks the user" means you need to display a message and provide a way for the user to enter the filename that can be read into the program. The Scanner class has methods for reading input from a user into a variable.

  9. #9
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    here is what i have so far. I am trying to figure out the I/O thing when it comes to user input in the program itself.
    Last edited by jan07; February 29th, 2012 at 05:24 PM.

  10. #10
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    import java.io.*;
    import java.util.*;
     
    public class Project5
    {
        public static void main(String[] args) throws IOException
        {
            Scanner inputStream = null;
            PrintWriter outputStream = null;
            System.out.println("Please enter a file name");
            Scanner keyboard = new Scanner(System.in);
            String name = keyboard.nextLine();
            File file = new File(name);
            try
            {
     
            	inputStream = new Scanner(new FileInputStream(file));
                outputStream = new PrintWriter(new FileOutputStream("numbered.txt"));
     
            }
            catch(FileNotFoundException e)
            {
                System.out.println("Error");
                System.exit(0);
            }
     
            outputStream.println("Little miss Muffet,");
            outputStream.println("sat on a tuffet,");
            outputStream.println("eating her candy away.");
            outputStream.println("Along came a spider,");
            outputStream.println("who sat down beside her,");
            outputStream.println("and said \"Will you marry me?\"");
     
     
            int count = 0;
            String line = null;
            while(inputStream.hasNext())
            {
               line = inputStream.next();
               count ++;
     
            }
            outputStream.println("Word count:"+count);
            inputStream.close();
            outputStream.close();
     
     
     
        }
     
    }

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    How did it go when you executed the code? What happened?

    In your catch block you should call the Event class's printStackTrace() method to show any exception.

  12. #12
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    it asks me to enter the file i enter the file and that is all that happens. I don't believe it is accessing the actual file that i am trying to reference. I have not used printStackTrace() before i am not quite sure what it is? I guess at this point i am just trying to make sure i can access the file properly through user keyboard input and then next i will deal with parsing the file up?

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    that is all that happens.
    Add some println statements to the code to show where it is executing and what the values of the variables are as they are set and changed. Their output will show you some of what the program is doing.

  14. #14
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    so would i even need the outputStream statements in this program or do i need to just have it written to file once, and then use input statements to access it?

  15. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    would i even need the outputStream statements
    You don't need an OutputStream if you are not writing anything to a file.

  16. #16
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    It gives me the little miss muffet etc... lines and the assignment says that it is the sample text file to be used as an input to my program, so i am uncertain if the i just create that file or if i have it as part of the program?

  17. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    i am uncertain if the i just create that file or if i have it as part of the program?
    Can you ask your instructor what the assignment is so you will know?

  18. #18
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    No but i can write it out word for word?

  19. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    It all depends on what the assignment requires the program to do.
    You could create the file that is to be read by using an editor and then have the program read it.

  20. #20
    Junior Member
    Join Date
    Feb 2012
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File I/O

    Write a program that asks the user to enter the name of a text file and then displays the line count, word(token) count and the character(non-delimeter) count in the file in a nice format. Use the StringTokenizer class in the java.util pacjkage to parse the tokens from the input lines. In addition to the whitespace characters, question mark(?), double quotes("), commas(,) and periods(.) should also be treated as delimeters and thus not to be counted as tokens or parts of tokens. Use the sample text file input5.txt as an input to your program.


    input5.txt
    Little miss Muffet,
    sat on a tuffet,
    eating her candy away.
    Along came a spider,
    who sat down beside her,
    and said "Will you marry me?"

  21. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: File I/O

    Create the file with an editor.

Similar Threads

  1. solaris machine /tmp folder, File.exists() cant see the existing file.
    By aragorn1905 in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: December 27th, 2011, 09:41 AM
  2. Replies: 3
    Last Post: December 2nd, 2011, 11:53 AM
  3. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  4. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM