+ Reply to Thread
Results 1 to 5 of 5

Thread: File Input and Output

  1. #1
    Join Date
    Mar 2008
    Posts
    7,145
    Rep Power
    86

    File Input and Output

    File Input and Output

    When you need a way to store data even after the program has stopped running you use files. When I use databases, these are just text files. I just don't know the details of how it handles the reading and writing.

    Text files are used in contests because it is makes for easy grading and testing of multiple test cases.

    Reading Files

    There are two main ways to read from files. The Scanner and the BufferedReader. I like using these two methods because they provide methods for reading until end of file. Thus I don't have to worry about reading past the end of file.

    For these cases, you need these imports:

    Code:
    import java.util.*;
    import java.io.*;
    When you use all of these methods, you need to either catch IOException or declare main to throw IOException.

    Code:
    public static void main(String[] args) throws IOException {
    
    }
    The IOException is thrown when there is an error with input or output.

    Scanner

    This method is used as a tokenizer to read things token at a time. This method is great when you know what the format of the data is that you are going to be reading. With methods like hasNext and hasNextInt I can check what the type of the next token is. The hasNext method allows me to read until end of file. When I am at the end of the file hasNext will return false and the loop will terminate.

    Declaring a Scanner

    Code:
    Scanner fin = new Scanner(new FileReader("file.txt"));
    Looping until end of file

    Code:
    while (fin.hasNext()) {
    
    }
    This loop runs until you reach the end of the file. In the loop body, you can read and store the values from the file. Alternatively, you can read the values and then process the values. There is not always a need to store values.

    Reading

    Code:
    String s = fin.next();
    int x = fin.nextInt();
    The fin.next method reads the next token as a String until a delimeter is found. The default delimter is a space. Alternatively, you can choose the delimeter using setDelimeter.


    Code:
    fin.setDelimeter("\\s");
    There is a read method for pretty much any data type you can think of. The nextLine method reads an entire line.

    When you are done, don't forget to close the Scanner.

    Code:
    fin.close();
    BufferedReader

    Alternatively, the BufferedReader always reads the entire line. Then you can split the string into an array at whatever delimeter you specify.

    Declaring a Buffered Reader

    Code:
    BufferedReader fin = new BufferedReader(new FileInputReader("data.txt"));
    Reading with this method of input is as simple as:

    Code:
    String sLine = fin.readLine();
    To process individual tokens, you can either use a StringTokenizer or split it into an array.

    Code:
    String[]  arsWords = sLine.split(" ");
    Writing Files

    Writing to the file is as simple as using a PrintWriter. Then calling the print or println functions.

    Example:

    Code:
    PrintWriter fout = new PrintWriter(new FileWriter("OUT.txt"));
    Scanner fin = new Scanner(new FileReader("IN.txt"));
    String sLine;
    
    while (fin.hasNext()) {
    	sLine = fin.next();
    	
    	fout.println(sLine.toUpperCase());
    }
    fout.close();
    fin.close();
    Always make sure, you close your input and output streams. This is just a functional introduction to input and output streams. There is a lot of details that I did not go into here.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: File Input and Output

    Nice tutorial! +rep

  4. #3
    Join Date
    Mar 2009
    Posts
    1,375
    Rep Power
    24

    Re: File Input and Output

    I had big problems with Java when I first tried it. I think your "Main method throwing IOException" is the solution to what I experienced. +rep

  5. #4
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: File Input and Output

    Nice one. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  6. #5
    Phineas is offline Newbie
    Join Date
    Jan 2010
    Posts
    11
    Rep Power
    0

    Re: File Input and Output

    Nice TuT, much appreciated, would rep if I knew how lol.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. File Descriptors and Input output Redirection
    By fread in forum C and C++
    Replies: 2
    Last Post: 11-18-2010, 12:24 PM
  2. Input/Output bug
    By isuru in forum Python
    Replies: 2
    Last Post: 09-11-2010, 11:22 AM
  3. Problems with file input/output
    By extrem3 in forum C and C++
    Replies: 4
    Last Post: 08-21-2009, 09:18 AM
  4. Trouble with file input/output
    By LoneWolf in forum C and C++
    Replies: 3
    Last Post: 02-03-2009, 12:30 PM
  5. Input/Output
    By scarfacemontana in forum Java Help
    Replies: 1
    Last Post: 02-19-2007, 10:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

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