为什么第一行提前换行

来源:4-1 字节流之文件输入流FileInputStream-1

神华内敛方能以柔克刚

2019-02-07 18:17

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class Demo {
    public static void main(String[] args) throws IOException{
        Demo demo = new Demo();
	demo.go("E:\\testdata\\directory1\\text1.txt");
    }
    public void go (String fn) throws IOException{
        if(! (new File(fn)).exists()) {
            IOException ioe = new IOException("文件不存在");
	    throw (ioe);
        }else {
	    FileInputStream fis = new FileInputStream(fn);
	    fis.read();
    	    int aa;
	    int bb = 1;
	    while((aa = fis.read()) != -1) {
		if((bb++ % 10) == 0) {
		    System.out.println();
		}
		if(aa <= 0xF) {
		    System.out.print(0);
	        }
	        System.out.print(Integer.toHexString(aa) + ",");
	    }
	    fis.close();
        }
    }
}

代码是这样写的,文件text1.txt中的内容是:There is a very beautiful city called Loulan,which locates in the west of ancient China.,输出结果却是:

68,65,72,65,20,69,73,20,61,

20,76,65,72,79,20,62,65,61,75,

74,69,66,75,6c,20,63,69,74,79,

20,63,61,6c,6c,65,64,20,4c,6f,

75,6c,61,6e,2c,77,68,69,63,68,

20,6c,6f,63,61,74,65,73,20,69,

6e,20,74,68,65,20,77,65,73,74,

20,6f,66,20,61,6e,63,69,65,6e,

74,20,43,68,69,6e,61,2e,

为何第一行提前了一下?

写回答 关注

2回答

  • qq_prisoner_4
    2019-05-29 22:09:19

    你自己心算下i和输出,对应一下。把i初始值赋为0就行了。

  • 创客2018
    2019-02-20 14:36:34

    这应该是文件拷贝时,文件宽度到了自动换的行,还有可能是编译器自动换的行

文件传输基础——Java IO流

为您介绍IO流的使用,以及对象的序列化和反序列化的内容

133803 学习 · 1034 问题

查看课程

相似问题