• 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));//变成字符串输出
    	}
    
    }
    


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

  • 相关阅读:
    zmap zgrab 环境搭建
    RF是如何工作的?
    RF的优缺点
    国内NLP的那些人那些会
    B-、B+、B*树
    关于LDA的gibbs采样,为什么可以获得正确的样本?
    LDA算法里面Dirichlet分布的两个参数alpha和beta怎样确定?
    如何确定LDA的主题个数
    SMO算法精解
    奇异值与主成分分析(PCA)
  • 原文地址:https://www.cnblogs.com/ldxsuanfa/p/10512628.html
  • Copyright © 2020-2023  润新知