java的InputStream系列例子

DataInputStream 和ByteArrayInputStream 结合的例子;

 

//从内存取出格式化输入

class Test { public static void main(String []args) throws IOException { DataInputStream di=new DataInputStream(new ByteArrayInputStream(args[0].getBytes())); for(int i=0;i<args[0].length();i++) { System.out.print((char)di.readByte()); } di.close(); } };

 

StringBufferInputStream类已经过时,不再熬述.

 

 

//下面是关于FileInputStream的例子

//然后结合BufferedReader实现高效率的输入.这里的BufferedReader要结合其他Reader

class Test
{
    public static void main(String []args) throws Exception
    {
        FileInputStream fi=new FileInputStream("temp.txt");
        //输出文件的大小
        System.out.println(fi.available());
        BufferedReader bf=new BufferedReader(new InputStreamReader(fi));
        //输出文件的内容
        String temp;
        while((temp=bf.readLine())!=null)
        {
            System.out.println(temp);
        }

        fi.close();
        bf.close();
    }
};

class Test { public static void main(String []args) throws Exception { FileInputStream fi=new FileInputStream("temp.txt"); //输出文件的大小 System.out.println(fi.available()); BufferedReader bf=new BufferedReader(new InputStreamReader(fi)); //输出文件的内容 String temp; while((temp=bf.readLine())!=null) { System.out.println(temp); } } };

你可能感兴趣的:(java,exception,String,null,Class)