Java程序辅导

C C++ Java Python Processing编程在线培训 程序编写 软件开发 视频讲解

客服在线QQ:2653320439 微信:ittutor Email:itutor@qq.com
wx: cjtutor
QQ: 2653320439
Lab 11: File I/O 
Copyright © 2012 Pearson Education, Inc. 
Nested Loop Example 
Copyright © 2012 Pearson Education, Inc. 
//******************************************************************** 
//  Stars.java       Author: Lewis/Loftus 
// 
//  Demonstrates the use of nested for loops. 
//******************************************************************** 
 
public class Stars 
{ 
   //----------------------------------------------------------------- 
   //  Prints a triangle shape using asterisks (stars) 
   //----------------------------------------------------------------- 
   public static void main (String[] args) 
   { 
      final int MAX_ROWS = 10; 
 
      for (int row = 1; row <= MAX_ROWS; row++) 
      { 
         for (int star = 1; star <= row; star++) 
            System.out.print ("*"); 
 
         System.out.println(); 
      } 
   } 
} 
Output 
* 
** 
*** 
**** 
***** 
****** 
******* 
******** 
********* 
********** 
Loop templates 
Copyright © 2012 Pearson Education, Inc. 
While Loop Index Template: 
 
initialize index 
while (condition){ 
   statements to be repeated 
   update index 
} 
For Loop Template: 
 
for(initialize index; condition; update index){ 
   statements to be repeated 
} 
For Each Loop Template: 
 
for (ElementType elementName : collection){ 
   statements to be repeated 
} 
While Loop Sentinel Template: 
 
get input value 
while (input != end condition){ 
   statements to be repeated 
   get input value 
} 
Copyright © 2012 Pearson Education, Inc. 
User Input Example: 
 
ArrayList guesses = new ArrayList(); 
Scanner s = new Scanner(System.in); 
 
String guess = s.nextLine(); // get input value 
while (!guess.equals(“quit”) && !guess.equals(“exit”)){ 
   guesses.add(guess);       // add line to array list 
   guess = s.nextLine();     // get input value 
} 
While Loop Sentinel Template (User Input): 
 
get input value 
while (input != end condition){ 
   statements to be repeated 
   get input value 
} 
Copyright © 2012 Pearson Education, Inc. 
File Reading Example: 
 
ArrayList lines = new ArrayList(); 
 
Scanner s = new Scanner(new File(“in.txt”)); 
while (s.hasNext()){ 
   String line = s.nextLine(); // get input 
   System.out.println(line);   // print line 
   lines.add(line);            // add line to array list 
} 
While Loop Sentinel Template (File Input): 
 
setup file scanner 
while (there is more input){ 
   get input 
   statements to be repeated 
} 
Iterators 
• Several classes in the Java standard class library 
are iterators 
• The Scanner class is an iterator 
– the hasNext method returns true if there is more data to 
be scanned 
– the next method returns the next scanned token as a 
string 
• The Scanner class also has variations on the 
hasNext method for specific data types (such as 
hasNextInt) 
 
Copyright © 2012 Pearson Education, Inc. 
Scanner is an Iterator 
• The fact that a Scanner is an iterator is particularly 
helpful when reading input from a file 
• A Scanner can be used to process input, whether it 
comes from the terminal (System.in), a file, or String 
• Suppose we wanted to read and process a list of 
URLs stored in a file? 
– One scanner reads each line of the input file 
– Another scanner processes each part of the URL path 
Copyright © 2012 Pearson Education, Inc. 
Nested Scanner Example 
• Suppose we wanted to read and process a list of 
URLs stored in a file? 
 
• Read from input file: 
www.linux.org/info/gnu.html 
• Output: 
URL: www.linux.org/info/gnu.html 
   www.linux.org 
   info 
   gnu.html 
• See URLDissector.java  
 
 
Copyright © 2012 Pearson Education, Inc. 
Copyright © 2012 Pearson Education, Inc. 
//******************************************************************** 
//  URLDissector.java       Author: Lewis/Loftus 
// 
//  Demonstrates the use of Scanner to read file input and parse it 
//  using alternative delimiters. 
//******************************************************************** 
 
import java.util.Scanner; 
import java.io.*; 
 
public class URLDissector 
{ 
   //----------------------------------------------------------------- 
   //  Reads urls from a file and prints their path components. 
   //----------------------------------------------------------------- 
   public static void main (String[] args) throws IOException 
   { 
      String url; 
      Scanner fileScan, urlScan; 
 
      fileScan = new Scanner (new File("urls.inp")); 
 
continue 
Copyright © 2012 Pearson Education, Inc. 
continue 
 
      // Read and process each line of the file 
      while (fileScan.hasNext()) 
      { 
         url = fileScan.nextLine(); 
         System.out.println ("URL: " + url); 
 
         urlScan = new Scanner (url); 
         urlScan.useDelimiter("/"); 
 
         //  Print each part of the url 
         while (urlScan.hasNext()) 
            System.out.println ("   " + urlScan.next()); 
 
         System.out.println(); 
      } 
   } 
} 
Copyright © 2012 Pearson Education, Inc. 
continue 
 
      // Read and process each line of the file 
      while (fileScan.hasNext()) 
      { 
         url = fileScan.nextLine(); 
         System.out.println ("URL: " + url); 
 
         urlScan = new Scanner (url); 
         urlScan.useDelimiter("/"); 
 
         //  Print each part of the url 
         while (urlScan.hasNext()) 
            System.out.println ("   " + urlScan.next()); 
 
         System.out.println(); 
      } 
   } 
} 
In file 
www.google.com 
www.linux.org/info/gnu.html 
thelyric.com/calendar/ 
www.cs.vt.edu/undergraduate/about 
youtube.com/watch?v=EHCRimwRGLs 
Sample Run 
URL: www.google.com 
  www.google.com 
 
URL: www.linux.org/info/gnu.html 
   www.linux.org 
   info 
   gnu.html 
 
URL: thelyric.com/calendar/ 
  thelyric.com 
   calendar 
 
URL: www.cs.vt.edu/undergraduate/about 
   www.cs.vt.edu 
   undergraduate 
   about 
 
URL: youtube.com/watch?v=EHCRimwRGLs 
   youtube.com 
   watch?v=EHCRimwRGLs 
Practice Loops & File IO (Scanner) 
• Step 1: Create a project named FileScreenPrinter, with a 
class FileScreenPrinter 
• Step 2: Create a file named phrases.txt in the 
FileScreenPrinter project that contains phrases to be guessed 
in your Hangman game (See next two pages) 
• Step 3: write a main that reads in a file & prints out each line 
• Step 4: convert into a class (OO format) for GuessPhrase: 
– Something to store all the lines in a text file 
– A constructor that accepts a name of a file 
– A read method that reads in the file 
– A print method that prints the file contents line by line 
– Write a main method to test your FileScreenPrinter 
• More practice: MyProgrammingLab Project 5.14 


In some systems the “\n” does not 
work. Use the following piece of code 
for writing to the file: 
Look at the second line of this piece of code to see the difference