[英]Why I am getting this output on screen while the file I am writing to is fine?
Sample code is :- 示例代码是:-
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
public class CopyBytes {
public static void main(String [] args){
CopyBytes obj = new CopyBytes();
File file = new File("/home/mount/Data/JAVA/xanadu.bak");
obj.copyBytes(file);
}
public void copyBytes(File ifile){
FileInputStream reader = null;
FileOutputStream output =null;
int c=0;
try{
reader = new FileInputStream(ifile);
output = new FileOutputStream("outfile");
while((c = reader.read()) != -1)
{
System.out.print(c);
output.write(c);
}
System.out.println();
}
catch(FileNotFoundException fnfex){
fnfex.getMessage();
fnfex.printStackTrace();
}
catch(IOException ioex){
ioex.getMessage();
ioex.printStackTrace();
}
finally{
if(reader !=null)
{
System.out.println("Closing the Stream");
try{
reader.close();
System.out.println("Closed the Streams");
}
catch(IOException ioex)
{
ioex.printStackTrace();
}
}
else{
System.out.println("Stream not open");
}
}
}
}
Contents of the xanadu.bak
are as follows:- xanadu.bak
内容如下:-
buffer@ankit:/home/mount/Data/JAVA/practice/src/fileio.bytestreams.com$ cat /home/mount/Data/JAVA/xanadu.bak
IA
When above code is run;it gives following output: 当运行上述代码时,它将给出以下输出:
buffer@ankit:/home/mount/Data/JAVA/practice/src/fileio.bytestreams.com$ java CopyBytes buffer @ ankit:/home/mount/Data/JAVA/practice/src/fileio.bytestreams.com$ java CopyBytes
736510
Closing the Stream
Closed the Streams
whereas i should get 而我应该得到
7365
Closing the Stream
Closed the Streams
The file to which I am writing is perfectly fine. 我正在写入的文件非常好。 Please provide your valuable inputs.
请提供您宝贵的意见。
The last character printed is 10
d , or 0x0A
. 最后打印的字符是
10
d或0x0A
。 That's a newline character. 那是换行符。 Other combinations like
0x0D 0x0A
or the reverse could happen on other platforms. 其他组合(例如
0x0D 0x0A
或相反的组合)可能在其他平台上发生。
A lot of editors add a newline at the end of files if there isn't one, and that's what you're seeing. 如果没有,许多编辑器会在文件末尾添加换行符,这就是您所看到的。
Use something like hexdump
to ascertain what your file really contains. 使用
hexdump
东西来确定您的文件真正包含了什么。
You code works pretty well for that too :-) 您的代码也可以很好地工作:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.