Java 源码 ——顺序存取文件的创建及写入(Writing data to a sequential text file with class Formatter)
来源:互联网 发布:mac window 编辑:程序博客网 时间:2024/06/06 10:00
代码如下:
// Fig. 15.3: CreateTextFile.java// Writing data to a sequential text file with class Formatter.package ch15;import java.io.FileNotFoundException; import java.lang.SecurityException; import java.util.Formatter; import java.util.FormatterClosedException;import java.util.NoSuchElementException; import java.util.Scanner; public class CreateTextFile{ private static Formatter output; // outputs text to a file public static void main(String[] args) { openFile(); addRecords(); closeFile(); } // open file clients.txt public static void openFile() { try { output = new Formatter("clients.txt"); // open the file } catch (SecurityException securityException) { System.err.println("Write permission denied. Terminating."); System.exit(1); // terminate the program } catch (FileNotFoundException fileNotFoundException) { System.err.println("Error opening file. Terminating."); System.exit(1); // terminate the program } } // add records to file public static void addRecords() { Scanner input = new Scanner(System.in); System.out.printf("%s%n%s%n? ", "Enter account number, first name, last name and balance.", "Enter end-of-file indicator to end input."); while (input.hasNext()) // loop until end-of-file indicator { try { // output new record to file; assumes valid input output.format("%d %s %s %.2f%n", input.nextInt(), input.next(), input.next(), input.nextDouble()); } catch (FormatterClosedException formatterClosedException) { System.err.println("Error writing to file. Terminating."); break; } catch (NoSuchElementException elementException) { System.err.println("Invalid input. Please try again."); input.nextLine(); // discard input so user can try again } System.out.print("? "); } } // close file public static void closeFile() { if (output != null) output.close(); } } // end class CreateTextFile
测试结果:
1. 正常
Enter account number, first name, last name and balance.
Enter end-of-file indicator to end input.
? 101 Honglei Li 898.99
? 102 Facai Yao 999.58
? 106 Yongjiu Niu 1058.99
? ^Z
Enter end-of-file indicator to end input.
? 101 Honglei Li 898.99
? 102 Facai Yao 999.58
? 106 Yongjiu Niu 1058.99
? ^Z
2. 异常
将clients.txt文件更改为read only,运行程序,报错:
Error opening file. Terminating.
文件操作还是很有意思的!:)
0 0
- Java 源码 ——顺序存取文件的创建及写入(Writing data to a sequential text file with class Formatter)
- Java源码——读取顺序存取文件中的数据(read text file and display each record)
- Writing file-写入文件
- Code: Writing Text to a File (Visual Basic)
- 往文件中写int等数据类型数据 Writing an integer to a file with fputs()
- 在服务器端创建文件(create a sequential file)
- Writing to a Temporary File
- Writing to a Binary File
- 创建顺序存取文件
- Writing a DIB to a JPEG file
- Writing a bitmap to a BMP file
- java File文件的写入
- 关于OpenTSDB的Writing Data数据写入
- Creating a Data dictionary file to use with LogMi
- Trafodion Troubleshooting-Sort failed while writing to a scratch file with error 28
- Creating a sequential file
- //outfile.cpp--writing to a file
- 文件的创建及写入
- Hadoop机架感知配置
- Oracle12c jdbc版本驱动问题
- 简单的sql注入式攻击 教程(小白 )
- composer的简单使用
- DEBUG 之 指令
- Java 源码 ——顺序存取文件的创建及写入(Writing data to a sequential text file with class Formatter)
- java异常:org.springframework.beans.ConversionNotSupportedException
- HTTP请求
- 预处理
- 【qscoj】哗啦啦村的刁难(2)
- 剑指offer-面试题33-把数组排成最小的数
- CentOS7虚拟机无法联网,无法ping通网关
- 算法课第10周第3题——91. Decode Ways
- 记录配置Hadoop+zookeeper+Hbase的完整过程(三)