FileInputStream读出的是什么?

李老七 2016-04-08 04:23:20
File a = new File("D:/1.txt");
FileInputStream b = new FileInputStream(a);
int c ;
while((c = b.read())!=-1){
System.out.println((char)c);
}
这里,Stream是字节流,输出为一个字节的char我也就认了,为什么是int?
百度了一下,大家说返回的int是字节数量,但是这个“字节数量”输出是我文本里的内容,不懂,大神呢
...全文
321 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
li4c 2016-04-14
  • 打赏
  • 举报
回复
package http.hello;

import java.io.FileInputStream;
import java.io.IOException;

public class Demo6 {

	public static void main(String[] args){
		FileInputStream fileInput = null;
		try{
			fileInput = new FileInputStream("D:\\a.txt");
			int c = 1;
			//返回的是一个8位的数字,之所以返回类型是int而不是char,因为可能返回-1,文件结束标示EOF
			while((c = fileInput.read()) != -1){
				System.out.printf("%c",c);
			}
		}catch(Exception e){
		}finally{
			try {
				fileInput.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
PJ_Richard 2016-04-12
  • 打赏
  • 举报
回复
引用 7 楼 imfang的回复:
[quote=引用 6 楼 PJ_Richard 的回复:] 首先int c=0,局部变量要初始化。 其次,read返回的是int。因为读取的字节,如果文本中内容是-1,那么它会读到-1,但这不表示读取结束!那怎样区分文本的-1和结束标志-1呢?这时可以将byte提升为int,然后与上255取其低八位。问题就解决了!
如果文本中内容是-1,你是读不到-1的,你读的‘-’的ASCII码,再次读读到的是‘1’的ASCII码。[/quote] 嗯,那个应该不是文本,该怎么说呢,文件以二进制字节形式来读取时读到-1,记得看视频时,让我们学着写一个read( ),当时是这样理解的!不是这样吗?
imfang 2016-04-11
  • 打赏
  • 举报
回复
引用 6 楼 PJ_Richard 的回复:
首先int c=0,局部变量要初始化。 其次,read返回的是int。因为读取的字节,如果文本中内容是-1,那么它会读到-1,但这不表示读取结束!那怎样区分文本的-1和结束标志-1呢?这时可以将byte提升为int,然后与上255取其低八位。问题就解决了!
如果文本中内容是-1,你是读不到-1的,你读的‘-’的ASCII码,再次读读到的是‘1’的ASCII码。
imfang 2016-04-09
  • 打赏
  • 举报
回复
引用 楼主 huanshen7428 的回复:
File a = new File("D:/1.txt"); FileInputStream b = new FileInputStream(a); int c ; while((c = b.read())!=-1){ System.out.println((char)c); } 这里,Stream是字节流,输出为一个字节的char我也就认了,为什么是int? 百度了一下,大家说返回的int是字节数量,但是这个“字节数量”输出是我文本里的内容,不懂,大神呢
FileInputStream是字节流,读出来自然是一个字节,但b.read()返回的却是int,为何不是byte呢? 因为byte正好是一个字节,是一个有符号的数据,不便于处理,最主要的是它无法表示是否成功读到数据。 而int的表值范围比较大,可用0~255来表示读到的字节值,而读到的若是-1,则表示没能读到数据。
PJ_Richard 2016-04-09
  • 打赏
  • 举报
回复
首先int c=0,局部变量要初始化。 其次,read返回的是int。因为读取的字节,如果文本中内容是-1,那么它会读到-1,但这不表示读取结束!那怎样区分文本的-1和结束标志-1呢?这时可以将byte提升为int,然后与上255取其低八位。问题就解决了!
Spinach007 2016-04-09
  • 打赏
  • 举报
回复
学的时候说的是返回的int值是低八位有效的。返回的值说明是读到多少个字节,并且读到的字节存储到这个int值里。
heiyedezhou 2016-04-08
  • 打赏
  • 举报
回复
补充2楼 你想读取字符串必须这样写
justdo_ 2016-04-08
  • 打赏
  • 举报
回复
FileInputStream的read大概有两种使用方法 1. c=b.read(),这个是从流中读取一个字节,并以int(0~255)形式返回该字节 2. byte buf[] = new byte[1024]; c=b.read(buf); 这个才是所读取的字节数,也是int,如果数据足够多的话c=1024,没那么多的话,buf有数据部分长度就是c了
7875405 2016-04-08
  • 打赏
  • 举报
回复
下一个数据字节;如果已到达文件末尾,则返回 -1。
Java面向对象程序设计试题一 一、单项选择题 D1、欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的?( ) A ArrayList myList=new Object(); B List myList=new ArrayList(); C ArrayList myList=new List(); D List myList=new List(); C2、paint ( )方法使用哪种类型的参数? A Graphics B Graphics2D C String D Color D3、指出正确的表达式 ( ) A byte=128; B Boolean=null; C long l=0xfffL; D double=0.9239d; 4、指出下列程序运行的结果 ( )。 public class Example{   String str=new String("good");   char[]ch={'a','b','c'};   public static void main(String args[]){     Example ex=new Example();     ex.change(ex.str,ex.ch);     System.out.print(ex.str+" and ");     Sytem.out.print(ex.ch);   }   public void change(String str,char ch[]){     str="test ok";     ch[0]='g';   } } A good and abc B good and gbc C test ok and abc D test ok and gbc A5、运行下列程序, 会产生什么结果 ( ) public class X extends Thread implements Runable{  public void run(){   System.out.println("this is run()");  }  public static void main(String args[])  {   Thread t=new Thread(new X());   t.start();  } } A 第一行会产生编译错误 B 第六行会产生编译错误 C 第六行会产生运行错误 D 程序会运行和启动 A6、要从文件" file.dat"文件中读出第10个字节到变量C中,下列哪个方法适合? ( ) A FileInputStream in=new FileInputStream("file.dat"); in.skip(9); int c=in.read(); B FileInputStream in=new FileInputStream("file.dat"); in.skip(10); int c=in.read(); C FileInputStream in=new FileInputStream("file.dat"); int c=in.read(); D RandomAccessFile in=new RandomAccessFile("file.dat"); in.skip(9); int c=in.readByte(); 7、容器被重新设置大小后,哪种布局管理器的容器中的组件大小不随容器大小的变化而 改变? ( ) A CardLayout B FlowLayout C BorderLayout D GridLayout C8、给出下面代码: public class Person{   static int arr[] = new int[10];   public static void main(String a[])   {    System.out.println(arr[1]);   } } 那个语句是正确的? ( ) A 编译时将产生错误; B 编译时正确,运行时将产生错误; C 输出零; D 输出空。 9、哪个关键字可以对对象加互斥锁 ?( ) A transient B synchronized C serialize D static 10、下列哪些语句关于内存回收的说明是正确的? ( ) A 程序员必须创建一个线程来释放内存; B 内存回收程序负责释放无用内存 C 内存回收程序允许程序员直接释放内存 D 内存回收程序可以在指定的时间释放内存对象 二、多项选择题 AB1、下面哪些标识符在Java语言中是合法的?( ) A persons$ B TwoUsers C *point D instanceof F.end-line BD2、下面哪些代码片段

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧