Java源码——读取顺序存取文件中的数据(read text file and display each record)
来源:互联网 发布:淘宝网电子商务类型 编辑:程序博客网 时间:2024/06/11 18:24
代码如下:
// Fig. 15.6: ReadTextFile.java// This program reads a text file and displays each record.package ch15;import java.io.IOException;import java.lang.IllegalStateException;import java.nio.file.Files;import java.nio.file.Path;import java.nio.file.Paths;import java.util.NoSuchElementException;import java.util.Scanner;public class ReadTextFile{ private static Scanner input; public static void main(String[] args) { openFile(); readRecords(); closeFile(); } // open file clients.txt public static void openFile() { try { input = new Scanner(Paths.get("clients.txt")); } catch (IOException ioException) { System.err.println("Error opening file. Terminating."); System.exit(1); } } // read record from file public static void readRecords() { System.out.printf("%-10s%-12s%-12s%10s%n", "Account", "First Name", "Last Name", "Balance"); try { while (input.hasNext()) // while there is more to read { // display record contents System.out.printf("%-10d%-12s%-12s%10.2f%n", input.nextInt(), input.next(), input.next(), input.nextDouble()); } } catch (NoSuchElementException elementException) { System.err.println("File improperly formed. Terminating."); } catch (IllegalStateException stateException) { System.err.println("Error reading from file. Terminating."); } } // end method readRecords // close file and terminate application public static void closeFile() { if (input != null) input.close(); } } // end class ReadTextFile
调试结果:
Account First Name Last Name Balance
1 a b 1.00
2 c d 3.00
3 e f 8.00
1 a b 1.00
2 c d 3.00
3 e f 8.00
0 0
- Java源码——读取顺序存取文件中的数据(read text file and display each record)
- Java 源码 ——顺序存取文件的创建及写入(Writing data to a sequential text file with class Formatter)
- Use eof() to read and display a text file.
- Use eof() to read and display a text file.
- QT27 how to read text file and display file to a textbrowser or textedit
- db file sequential read-数据文件顺序读取
- db file scattered read(DB 文件分散读取)
- android中的数据存取 之 File
- Java源码——文件/文件夹的路径分析(相对/绝对)(File and Directory Info Inquiry)
- Java源码——使用JFileChooser获取文件及目录信息(Obtain file and directory information)
- Read text file
- read text file DEMO
- Java read and write csv file(注意事项)
- java 读取配置(read config from file)
- Android中的SQLite数据存取以及简单例子(源码)
- 数据驱动---文件读取(file)
- file read and write use java
- java File读取文件
- 清空数据库所有表的存储过程(没看懂)
- 这8种武器点亮程序员的个人品牌
- linux学习笔记(5)-安装五笔输入法
- PHP ——表单验证(验证 E-mail 和 URL)
- python 获取当前时间
- Java源码——读取顺序存取文件中的数据(read text file and display each record)
- java程序中的进制(进制的转换十六进制和八进制)
- 利用5随机产生7随机及其扩展
- 验证码工具类
- HTML之9大标签思维导图总结一
- C#委托初见
- Oracle Form 常用功能
- 【精华版】前缀式、中缀式、后缀式
- 《笨办法学python》--打印,打印