File file = new File("d:\\1.txt"); FileInputStream is = new FileInputStream(file);
FileOutputStream os = new FileOutputStream(file);
System.out.println(is.read());
代码如上,非常简单,此处的输出为-1,而且在进行了读操作之后,file的内容被清空。如上的代码有什么问题?
FileOutputStream os = new FileOutputStream(file);
System.out.println(is.read());
代码如上,非常简单,此处的输出为-1,而且在进行了读操作之后,file的内容被清空。如上的代码有什么问题?
但是我这里的输出为-1 意思就是没有读取出数据,为什么会这样?
就这句话,jdk这样解释:
Creates a file output stream to write to the file represented by
the specified <code>File</code> object. A new
<code>FileDescriptor</code> object is created to represent this
file connection.
就是往指定的文件里面写入数据,在开始写之前会将文件里面的内容清空。
在实际的项目开发中,file不用我们先在磁盘上面创建,只需给个文件路径参数就够了。