- FileOutputStream:写入字节流的方法
try {
String hello = "hello China";
FileOutputStream out = new FileOutputStream("d:/Lenovo/IO.txt");
out.write(hello.getBytes());
out.close();
} catch (FileNotFoundException e) { e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
- FileInputStream:从文件系统中的某个文件中获得输入字节
try {
FileInputStream input = new FileInputStream("d:/Lenovo/IO.txt");
try {
int n = input.read();
while (n > -1) {
System.out.print((char) n);
n = input.read();
}
input.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
- FileInputStream和FileOutputStream的综合使用:
try {
FileOutputStream output = new FileOutputStream("d:/Lenovo/IO1.txt");
FileInputStream input = new FileInputStream("d:/Lenovo/IO.txt");
int n = input.read();
while (n != -1) {
output.write(n);
n = input.read();
}
output.close();
input.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}