字节符输入流 InputStreamDemo

来源:互联网 发布:arduino hc05 发送数据 编辑:程序博客网 时间:2024/06/09 17:15
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

public class InputStreamDemo 
{
public static void main(String[] args) 
{
File file = new File("d:\\a\\a.txt");     //要读取的文件路径;
if(!file.exists())
{
System.out.println("文件不存在");
System.exit(-1); //文件不存在,退出;
}
InputStream is = null; //字节输出流;
try 
{
is = new FileInputStream(file);
byte[] b = new byte[(int)file.length()];//根据文件大小;
is.read(b);//读取;
System.out.println(new String(b));//在控制台输出;
is.close();//关闭流;
} catch (FileNotFoundException e)
 {
e.printStackTrace();
}catch(IOException e)
{
e.printStackTrace();
}
}
}

0 0
原创粉丝点击