欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

java.io使用 FileInputStream读取文件,,package cn.o

来源: javaer 分享于  点击 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);            }        }    }}
相关栏目:

用户点评