创建一个PrintWriter>对象,向文件写入两行数据。每行都包括名字和分数。 JWriteData.java☒ PrintWriter(File file)throws 1 import java.io.* FileNotFoundException,所以这里声 2 明抛出的异常 public class WriteData 49 public static void main(String[]args)throws Exception File file new File("c:\\a.txt"); 6 PrintWriter output new PrintWriter(file); 7 output.print ("John T Smith ") 8 output.println(90); output.print ("Eric K Jones 必须使用close()方法关闭文件, 10 output.printIn(的): 否则,数据不能正确保存在文件中。 11 output.close(): 12 圆a.txt-记事本 ▣X 13 文件)编辑)格式@)查看Y)帮助D 14 John T Smith 90 Eric K Jones 80 15
必须使用close()方法关闭文件, 否则,数据不能正确保存在文件中。 创建一个PrintWriter对象,向文件写入两行数据。每行都包括名字和分数。 PrintWriter(File file)throws FileNotFoundException,所以这里声 明抛出的异常
使用Scanner类读数据 "java.util.Scanner类用来从控制台读取字 符串和基本类型数值。Scanner可以将 输入分为由空白字符分隔的有用信息。 例如:为了能从键盘读取,需要为 System.in创建一个Scannerx对象, Scanner input new Scanner(System.in);
使用Scanner类读数据 ◼ java.util.Scanner类用来从控制台读取字 符串和基本类型数值。 Scanner可以将 输入分为由空白字符分隔的有用信息。 ◼ 例如:为了能从键盘读取,需要为 System.in创建一个Scanner对象, Scanner input = new Scanner(System.in);
使用Scannera类读数据 java.util.Scanner +Scanner(source:File) 创建一个从文件扫描的扫描器 +Scanner(source:String) Creates a Scanner that produces values scanned from the specified string. +close() Closes this scanner. +hasNext():boolean 如果这个扫描器还有可读数据则返回true +next():String 从这个扫描器返回下一个标志作为字符串 +nextByte():byte Returns next token as a byte. +nextShort():short Returns next token as a short +nextInt():int Returns next token as an int. +nextLong():long Returns next token as a long. +nextFloat():float Returns next token as a float. +nextDouble():double Returns next token as a double. +useDelimiter(pattern:String) Sets this scanner's delimiting pattern. Scanner 20
20 使用Scanner类读数据 java.util.Scanner +Scanner(source: File) +Scanner(source: String) +close() +hasNext(): boolean +next(): String +nextByte(): byte +nextShort(): short +nextInt(): int +nextLong(): long +nextFloat(): float +nextDouble(): double +useDelimiter(pattern: String): Scanner Creates a Scanner that produces values scanned from the specified file. Creates a Scanner that produces values scanned from the specified string. Closes this scanner. Returns true if this scanner has another token in its input. Returns next token as a string. Returns next token as a byte. Returns next token as a short. Returns next token as an int. Returns next token as a long. Returns next token as a float. Returns next token as a double. Sets this scanner’s delimiting pattern. 如果这个扫描器还有可读数据则返回true 从这个扫描器返回下一个标志作为字符串 创建一个从文件扫描的扫描器
使用Scannera类读数据 如果文件不存在,会抛出 输入 FileNotFoundException异常 文件或其它 程序 输入设备 从文件或其它输入 设备中读出数据读 到程序中 Scanner public Scanner(Eile source)throws FileNotFoundException 创建一个从文件扫描的扫描器 口参数: ■source-要扫描的文件 抛出: ·FileNotFoundException-如果源文件未找到
程序 文件或其它 输入设备 从文件或其它输入 设备中读出数据读 到程序中 如果文件不存在,会抛出 FileNotFoundException异常 使用Scanner类读数据 Scanner public Scanner(File source) throws FileNotFoundException 创建一个从文件扫描的扫描器 ❑ 参数: ◼ source - 要扫描的文件 ❑ 抛出: ◼ FileNotFoundException - 如果源文件未找到 输入
使用Scanner类读数据 next public String next() 口从这个扫描器返回下一个标志作为字符串,在等待要 扫描的输入时此方法也可能阻塞
使用Scanner类读数据 ◼ next ◼ public String next() ❑ 从这个扫描器返回下一个标志作为字符串,在等待要 扫描的输入时此方法也可能阻塞