java.io使用 FileInputStream读取文件,,package cn.o
分享于 点击 23070 次 点评:113
java.io使用 FileInputStream读取文件,,package cn.o
package cn.outofmemory.snippets.core;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;public class ReadFileWithFileInputStream {public static void main(String[] args) { File file = new File("inputfile.txt"); FileInputStream fin = null; int ch; StringBuffer sb = new StringBuffer(); try { // create FileInputStream object fin = new FileInputStream(file); // Read bytes of data from this input stream while((ch = fin.read()) != -1) { sb.append((char)ch); } System.out.println("File content: " + sb); } catch (FileNotFoundException e) { System.out.println("File not found" + e); } catch (IOException ioe) { System.out.println("Exception while reading file " + ioe); } finally { // close the streams using close method try { if (fin != null) { fin.close(); } } catch (IOException ioe) { System.out.println("Error while closing stream: " + ioe); } } }}
用户点评