java--File的读取和写入实例
实例来源think in java读取:根据需要读取文件返回不同类型的信息进行选择使用。实例1使用BufferReader读取。[java]package io;// 使用Reader方式进行读取import java.io.*;public class BufferedInputFile {// Throw exceptions to console:public static String read(String filename) throws IOException {// 使用BufferedReader进行缓冲,来提高文件的读取数据BufferedReader in = new BufferedReader(new FileReader(filename));String s;StringBuilder sb = new StringBuilder();while ((s = in.readLine()) != null)sb.append(s + "\n");in.close();return sb.toString();}public static void main(String[] args) throws IOException {System.out.print(read("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\BufferedInputFile.java"));}}实例2使用StringReader类读取,in.read()以int形式返回下一个字节。[java]package io;// 从内存中读取import java.io.*;public class MemoryInput {public static void main(String[] args) throws IOException {StringReader in = new StringReader(BufferedInputFile.read("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\MemoryInput.java"));int c;while ((c = in.read()) != -1)System.out.print((char) c);}}实例3使用DataInputStream类读取,in.readByte()返回的byte类型。[java]package io;//相对TestEOF.java 此方法不能检测字节是否结束//面向字节的IO类import java.io.*;public class FormattedMemoryInput {public static void main(String[] args) throws IOException {try {DataInputStream in = new DataInputStream(new ByteArrayInputStream(BufferedInputFile.read("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\FormattedMemoryInput.java").getBytes()));while (true)System.out.print((char) in.readByte());} catch (EOFException e) {System.err.println("End of stream");}}}另一种用法,差异在第9行,使用in.available() != 0,判断检测字节是否结束。FormattedMemoryInput例子在运行时 不能检测流中是否还有字节,所以在控制台最后会抛出一个异常:End of stream下面用法可以避免此问题。[java]package io;import java.io.*;public class TestEOF {public static void main(String[] args) throws IOException {DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\TestEOF.java")));while (in.available() != 0)System.out.print((char) in.readByte());}}写:实例1基本的文件写入[java]package io;// 基本的文件输出import java.io.*;public class BasicFileOutput {static String file = "C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\BasicFileOutput.out";public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\BasicFileOutput.java")));PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(file)));int lineCount = 1;String s;while ((s = in.readLine()) != null)out.println(lineCount++ + ": " + s);out.close();// Show the stored file:System.out.println(BufferedInputFile.read(file));}}快捷用法,PrintWriter提供了进行缓存操作构造函数,省去了我们自己进行包装。[java]package io;// 输出方式的快捷用法,PrintWriter提供了进行缓存操作构造函数import java.io.*;public class FileOutputShortcut {static String file = "C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\FileOutputShortcut.out";public static void main(String[] args) throws IOException {BufferedReader in = new BufferedReader(new StringReader(BufferedInputFile.read("C:\\Users\\zm\\workspace\\Thinking in java\\src\\io\\FileOutputShortcut.java")));// Here's the shortcut:PrintWriter out = new PrintWriter(file);int lineCount = 1; &补充:软件开发 , Java ,
上一个:获取经纬度之间的距离
下一个:J2EE个人开发流程
- 更多JAVA疑问解答:
- java怎么在线读取ftp服务器上的文件内容
- 关于程序员的职业规划
- HTML和JSP矛盾吗?
- java小程序如何打包?
- java怎么split路径文件名?
- jsp+javaBean中Column 'ordersPrice' specified twice的错误
- Java TCP/IP Socket网络编程系列
- 大家来讨论一下我到底该用什么好?Swing 还是 JavaFX
- 关于Hibernate实体自身多对一的抓取问题
- 关于apache2+tomcat群集出现的问题
- spring 获取上下文问题
- SSH 导入导出excel 谁有这块的资料吗?
- Ext TreePanel 刷新问题
- springmvc 加载一个jsp页面执行多个方法 报404
- checkbox数组action怎么向页面传值