zoukankan      html  css  js  c++  java
  • File类的基本操作之InputStream字节输入流

    话不多少,我直接把代码贴上来了。有什么问题请给我留言

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    /**
     * 字节输出流:OutputStream。整个IO包中字节输出流的最大父类
     * 字节输入流:InputStream
     * 
     * InputStream使用子类FileInputStream。

    读取 */ public class InStream1 { /** * @param args */ public static void main(String[] args)throws Exception{ // TODO Auto-generated method stub //第1步:使用File类找到一个文件 File file = new File("d:" +File.separator +"test.txt"); //第2步:通过子类实例化父类对象 InputStream in = null; in = new FileInputStream(file);//通过对象多态性。进行实例化 //3.读 byte[] b = new byte[1024]; in.read(b); //4.关闭 in.close(); System.out.println(new String(b));//变成字符串输出 //遗留问题:留了很多空格。

    没有那么大。开了1024空间。没有内容的为空 } }


    上一个程序的遗留问题:

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    //解决遗留问题
    public class InStream2 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		InputStream in = null;
    		in = new FileInputStream(file);//通过对象多态性,进行实例化
    		//3.读
    		byte[] b = new byte[1024];
    		
    		int len = in.read(b);//读取内容
    		//4.关闭
    		in.close();
    		
    		//解决遗留问题,看String提供的方法
    		System.err.println("读入数据的长度:" + len);
    		System.out.println(new String(b,0,len));//变成字符串输出
    		
    		//能不能依据文件大小来开辟空间呢? 
    	}
    
    }
    

    解决依据文件大小来开辟空间

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    //解决依据文件大小来开辟空间
    public class InStream3 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)throws Exception {
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		InputStream in = null;
    		in = new FileInputStream(file);//通过对象多态性,进行实例化
    		//3.读
    		byte[] b = new byte[(int) file.length()];  //解决依据文件大小来开辟空间
    		
    		int len = in.read(b);//读取内容
    		//4.关闭
    		in.close();
    		
    		//解决遗留问题,看String提供的方法
    		System.err.println("读入数据的长度:" + len);
    		System.out.println(new String(b));//变成字符串输出
    	}
    
    }
    

    仅仅适合知道文件大小的输入

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    //仅仅适合知道文件大小的输入
    public class InStream4 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args) throws Exception{
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		InputStream in = null;
    		in = new FileInputStream(file);//通过对象多态性,进行实例化
    		//3.读
    		byte[] b = new byte[(int) file.length()];  //解决依据文件大小来开辟空间
    		for (int i = 0; i < b.length; i++) {
    			b[i] = (byte) in.read();
    		}
    //		int len = in.read(b);//读取内容
    		//4.关闭
    		in.close();
    		
    		//解决遗留问题,看String提供的方法
    		System.out.println(new String(b));//变成字符串输出
    	}
    
    }
    

    当不知道读取内容有多大的时候,就仅仅能以读取的数据是否为-1为读完的标志

    package org.mark.streamRW;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;
    
    //当不知道读取内容有多大的时候,就仅仅能以读取的数据是否为-1为读完的标志
    public class InStream5 {
    
    	/**
    	 * @param args
    	 */
    	public static void main(String[] args)throws Exception {
    		// TODO Auto-generated method stub
    		//第1步:使用File类找到一个文件
    		File file = new File("d:" +File.separator +"test.txt");
    		//第2步:通过子类实例化父类对象
    		InputStream in = null;
    		in = new FileInputStream(file);//通过对象多态性,进行实例化
    		//3.读
    		byte[] b = new byte[1024];  //解决依据文件大小来开辟空间
    		int len = 0;
    		int temp = 0;//接受每一个读取进来的数据
    		while ((temp = in.read())!= -1) {
    			//表示还有内容。文件没有读完
    			b[len] = (byte) temp;
    			len++ ;
    		}
    		//4.关闭
    		in.close();
    		
    		//解决遗留问题,看String提供的方法
    		System.out.println(new String(b,0,len));//变成字符串输出
    	}
    
    }
    


    贴的太快了~几乎吧程序关了~

查看全文
  • 相关阅读:
    POJ 1887 Testing the CATCHER
    HDU 3374 String Problem
    HDU 2609 How many
    POJ 1509 Glass Beads
    POJ 1458 Common Subsequence
    POJ 1159 Palindrome
    POJ 1056 IMMEDIATE DECODABILITY
    POJ 3080 Blue Jeans
    POJ 1200 Crazy Search
    软件体系结构的艺术阅读笔记1
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10512628.html
  • Copyright © 2011-2022 走看看