Java Input/Output API

Console Input/Output:

java.io.InputStream: stores information about the connection between an input device and the computer or program.

java.util.Scanner used to read the input available from InputStream object.

// 1. Create a Scanner using the InputStream available.
    Scanner scanner = new Scanner( System.in );

    // 2. Don't forget to prompt the user
    System.out.print( "Type some data for the program: " );

    // 3. Use the Scanner to read a line of text from the user.
    String input = scanner.nextLine();

    // 4. Now, you can do anything with the input string that you need to.
    // Like, output it to the user.
    System.out.println( "input = " + input );

  // 1. Create a single shared Scanner object for keyboard input.
    //    This must be done in only one class of your program.
    //    All keyboard input must be handled through that one class.
    private static Scanner scanner = new Scanner( System.in );

//1. Get a String of characters that is in an integer format, e.g.,  "123".
String input = scanner.nextLine();  // from console input example above.

//2. Use the Integer class to parse the string of characters into an integer. 
int number = Integer.parseInt( input );  // converts a String into an int value

Here's another way to do this:

//Read the next available input as an int value.
int number = scanner.nextInt();  // from console input example above.


Output: 
System.out.print(...)
System.out.println(...) 

 statements for displaying simple text messages to the user.  


File Input/Output

java.io.File - stores information about a file on a computer drive.

//Here's a code fragment to illustrate //reading a text file:

    System.out.print( "Enter the filename: " );   // Prompt the user for a file name
    String fileName = scanner.nextLine();         // get a file name from the user
    File file = new File( fileName );             // create a File object

    if ( file.exists() )                          // check that the file exists
    {                                             // before trying to create a
                                                  // Scanner to read the file
        // Create a Scanner from the file. 
        // This statement can cause a FileNotFoundException.
        Scanner inFile = new Scanner( file );

        // For each line in the file, read in the line and display it with the line number
        int lineNum = 0;

        // Use the results of calling the hasNext method to 
        // determine if you are at the end of the file before 
        // reading the next line of the file.
        while ( inFile.hasNext() )
        {
            line = inFile.nextLine();   // read the next line

            // Output the line read to the screen for the user
            System.out.println( ++lineNum + ": " + line );
        }

        // When we're done reading the file,
        // close the Scanner object attached to the file
        inFile.close();
    }


File output:

java.io.PrintStream class for file output.

//Here's an example file output:

        // Create a PrintStream attached to a file named "out.txt".
        // This will overwrite the file if it already exists
        java.io.PrintStream ps = new java.io.PrintStream( "out.txt" );

        // Buffer some data to write to the file (doesn't actually write until flush)
        ps.print( "Some test data that will be written when flush is called.");
        
        // Flush all buffered data to the file.
        ps.flush();

        // Buffer some more data.
        ps.println( data );

        // Close the file (by closing the PrintStream).  
        // Also flushes any remaining buffered output.
        ps.close();


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值