由帮助文档知FileInputStream与FileoutStream分别集成自InputStream和OutputStream;
FileInputStream(File file)用于从文件读取数据;FileOutputStream(File file)用于将数据写入文件。
具体流程包括:
1.找到并确定源文件;
2.打开流;
3.关闭流;
4.操作流。
由帮助文档:FileoutputStream含有write方法有三个:
void
write(byte[] b)
Writes b.length
bytes from the specified byte array to this file output stream.
void
write(byte[] b, int off, int len)
Writes len
bytes from the specified byte array starting at offset off
to this file output stream.
void
write(int b)
Writes the specified byte to this file output stream.
用代码实现:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) throws IOException {
FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");
//1
fos.write(89);
fos.write(90);
//2
byte[] a={91,92,93,94};
fos.write(a);
//3
byte[] b={95,96,97,98,99};
fos.write(b,2,2);
fos.close();
}
}
运行后文件内容为:YZ[\]^ab
FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");在 FileOutputStream中含有一个判断,它会将"D:\\666\\he1,.java"自动生成一个文件对象。作用相当于FileOutputStream fos1=new FileOutputStream(new File("D:\\666\\he1.java"));
FileOutputStream fos=new FileOutputStream("D:\\666\\he1,.java");
FileOutputStream fos1=new FileOutputStream(new File("D:\\666\\he1.java"));
此两行作用一样。
FileInputStream:
由帮助文档:FileInputStream也含有三个read方法:
int
read()
Reads a byte of data from this input stream.
int
read(byte[] b)
Reads up to b.length
bytes of data from this input stream into an array of bytes.
int
read(byte[] b, int off, int len)
Reads up to len
bytes of data from this input stream into an array of bytes.
1.
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; // Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`, // then press Enter. You can now see whitespace characters in your code. public class Main { public static void main(String[] args) throws IOException { FileInputStream fis=new FileInputStream("D:\\666\\he1,.java"); int a=0;while((a=fis.read())!=-1) { System.out.println(a); fis.close; } } }
打印结果:
89
90
91
92
93
94
97
98
2。
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
// Press Shift twice to open the Search Everywhere dialog and type `show whitespaces`,
// then press Enter. You can now see whitespace characters in your code.
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fis=new FileInputStream("D:\\666\\he1,.java");
FileInputStream fis1=new FileInputStream("D:\\666\\he1,.java");
byte[] a=new byte[10];
int f=fis.read(a);
System.out.println(new String(a));
byte[] b=new byte[8];
int f1=fis1.read(b,0,5);
System.out.println(new String(b));
fis.close();
}
}
结果:
YZ[\]^ab
YZ[\]