一. 字节流 FileOutputStream 和 FileInputStream
1. 创建 FileOutputStream 对象
// 方法1:输入文件名创建 FileOutputStream, true 表示打开方式是 Append,默认情况是 false 覆盖
private static void test1() {
FileOutputStream out = null;
try {
out = new FileOutputStream("test.txt",true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
// 方法2:使用 File 对象创建 FileOutputStream
private static void test1() {
File file = new File("test.txt");
FileOutputStream out = null;
try {
out = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
2. 如何写 FileOutputStream 对象
字节流一次只写一个字节,如果想写汉字,不能用字节流来写,可以用字符流。
private static void test2() {
FileOutputStream out = null;
try {
out = new FileOutputStream("test.txt");
// 写法1:写一个字符
out.write(64); // 写 ASCII
// 写法2:for 循环写一个字符串
String str = "hello world2";
for(int i=0;i<str.length();i++){
int c = str.charAt(i);
out.write(c);
}
// 写法3:使用 getBytes() 方法
// 第二个参数表示从str的第几个字符开始写,第三个参数表示写多少个字符
out.write(str.getBytes(), 2, 3);
//out.write(str.getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
if(out!=null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
3. FileInputStream 对象,read() 方法读到文件尾部会返回 -1。
private static void test3() {
FileInputStream in = null;
try {
in = new FileInputStream("test.txt");
byte[] buffer = new byte[20];
// 读到文件结束
int i;
while((i=in.read())!=-1){
System.out.print((char)i);
}
// 默认到 buffer 中
//in.read(buffer);
// 从第2个字符开始读,一共读4个字符
in.read(buffer, 2, 4);
System.out.println(new String(buffer));
} catch (Exception e) {
e.printStackTrace();
}
}
4. 拷贝文件
private static void copy() {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("test.txt");
out = new FileOutputStream("test2.txt");
byte[] buffer = new byte[1024];
in.read(buffer);
out.write(new String(buffer).trim().getBytes());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
if(in!=null)
in.close();
if(out!=null)
out.close();
} catch (IOException e) {
}
}
二. 字符流 FileWriteer 和 FileReader
1. 创建字符流对象和写文件流
private static void test5() {
FileWriter out = null;
try {
out = new FileWriter("test.txt");
// 写一个字节
out.write('好');
// 写一个字符串
out.write("你好,世界!");
// 写一个 buffer
char[] buffer = {'你','好',',','世','界','!'};
out.write(buffer);
} catch (IOException e) {
e.printStackTrace();
}finally{
if(out!=null){
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
2. 读出文件内容到字符对象
private static void test6() {
FileReader in = null;
try {
in = new FileReader("test.txt");
char[] buffer = new char[20];
in.read(buffer);
System.out.println(new String(buffer).trim());
} catch (Exception e) {
e.printStackTrace();
}
}
3. 拷贝文件
private static void copy2() {
FileReader in = null;
FileWriter out = null;
try {
in = new FileReader("test.txt");
out = new FileWriter("test2.txt");
char[] buffer = new char[1024];
in.read(buffer);
out.write(new String(buffer).trim());
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}