免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 12919 | 回复: 9
打印 上一主题 下一主题

请教:java如何从文件中读取一个整数 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2004-07-16 17:27 |只看该作者 |倒序浏览
我用DataInputStream的readInt方法读文件,总是出现违例错误,不知道什么原因啊

各位帮我看看代码有什么问题
      int x;
        try{
        
        DataInputStream in = new DataInputStream( new  BufferedInputStream(
        new FileInputStream("d:/int.txt"));
        x=in.readInt();
        System.out.println(x);
      }catch(EOFException e){
              System.out.println ( "err" );
      }
      catch(IOException ea){
              System.out.println ( "err2" );
      }

论坛徽章:
0
2 [报告]
发表于 2004-07-16 21:11 |只看该作者

请教:java如何从文件中读取一个整数

Try:
d:/int.txt ==>; d:\int.txt

论坛徽章:
0
3 [报告]
发表于 2004-07-17 14:24 |只看该作者

请教:java如何从文件中读取一个整数

试过了,不是这个问题啊

论坛徽章:
0
4 [报告]
发表于 2004-07-19 09:55 |只看该作者

请教:java如何从文件中读取一个整数

"d:\\int.txt"

论坛徽章:
0
5 [报告]
发表于 2004-07-19 10:00 |只看该作者

请教:java如何从文件中读取一个整数

Which Exception?

论坛徽章:
0
6 [报告]
发表于 2004-07-20 14:45 |只看该作者

请教:java如何从文件中读取一个整数

大家看一下java文档中的说明:

readInt
public int readInt()
            throws IOException
Reads four input bytes and returns an int value. Let a be the first byte read, b be the second byte, c be the third byte, and d be the fourth byte. The value returned is:


(((a & 0xff) << 24) | ((b & 0xff) << 16) |
  ((c & 0xff) << | (d & 0xff))

This method is suitable for reading bytes written by the writeInt method of interface DataOutput.
最后这句话说:这种方法适用于读出被DataOutput的writeInt方法写入的字节。
难道没有直接从文件读出整数的方法吗

论坛徽章:
0
7 [报告]
发表于 2004-07-20 15:23 |只看该作者

请教:java如何从文件中读取一个整数

DataInputStream能够读取的文件格式二进制的。换言之,所有的数字都是以byte[4]的形式存储的。如果是对象等其他数据,格式又有所不同。我不清持你的int.txt里面的格式是怎样的,不过可以肯定的是佣DataInputStream可以读写的文件通常不会是human-readable的,只能由程序来读取。
我假定int.txt的格式是每行一个数字,比如
123
456
789

那么程序如下:

  1. public class ReadIntTest {

  2.         public static void main(String[] args) {
  3.                 try {
  4.                         FileInputStream fis = new FileInputStream("c:\\temp\\int.txt");
  5.                         InputStreamReader isr = new InputStreamReader(fis);
  6.                         LineNumberReader lnr = new LineNumberReader(isr);
  7.                         String s = null;
  8.                         int i;
  9.                         while ((s = lnr.readLine()) != null) {
  10.                                 i = Integer.parseInt(s);
  11.                                 System.out.println("The decimal read: " + i);
  12.                         }
  13.                 } catch (Exception e) {
  14.                         e.printStackTrace();
  15.                 }
  16.         }
  17. }
复制代码

论坛徽章:
0
8 [报告]
发表于 2004-07-20 16:22 |只看该作者

请教:java如何从文件中读取一个整数

哦,多谢版主了。你的方法是从文件中读取字符串,然后转换成整形。

小弟初学java,运用还不够灵活,多谢版主指点。

论坛徽章:
0
9 [报告]
发表于 2004-07-21 07:39 |只看该作者

请教:java如何从文件中读取一个整数

不客气^_^

论坛徽章:
0
10 [报告]
发表于 2004-07-21 21:33 |只看该作者

请教:java如何从文件中读取一个整数

d:/int.txt, d:\\int.txt 都是对的, 抛出异常的原因也不是它的数据不是writeInt写的, 而是这个文件没有四个字节, readInt一次读4个字节。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP