处理的都是字节流,写入的时候也为字节流,不能直接写入中文字符

1.实现文件复制

示例代码如下:

public void copyFile(String src, String dest) {
// 1.提供读入、写出的文件
File file1 = new File(src);
File file2 = new File(dest);
// 2.提供相应的流
FileInputStream fis = null;
FileOutputStream fos = null;
try {
fis = new FileInputStream(file1);
fos = new FileOutputStream(file2);
// 3.实现文件的复制
byte[] b = new byte[1024];
int len;
while ((len = fis.read(b)) != -1) {
// fos.write(b);//错误的写法两种: fos.write(b,0,b.length);
fos.write(b, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

2.FileOutputStream写入文件

写入的时候一定要转换为字节,getBytes();如果目标文件不存在,则FileOutputStream会创建目标文件(前提是父路径文件对象必须存在)。

如果文件存在,会将原有的文件覆盖。若加上参数true,则为append。

@Test
public void testFileOutputStream() {
// 1.创建一个File对象,表明要写入的文件位置。
File file = new File("test7.txt");
// 2.创建一个FileOutputStream的对象,将file的对象作为形参传递给FileOutputStream的构造器中
FileOutputStream fos = null;
try {
// 输出的物理文件可以不存在,当执行过程中,若不存在,会自动的创建。若存在,会将原有的文件覆盖
//若加上参数true,则为append;不加,则为覆盖
//fos = new FileOutputStream(file,true);
fos = new FileOutputStream(file);
// 3.写入的操作--将String转换为字节数组
fos.write(new String("I love China,你呢!").getBytes());
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4.关闭输出流
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

3.FileInputStream读出文件

只能处理字节流,英文字符,不能处理中文字符

@Test
public void testFileInputStream3() { // abcdefgcde
FileInputStream fis = null;
try {
File file = new File("test7.txt");
fis = new FileInputStream(file);
byte[] b = new byte[5];// 读取到的数据要写入的数组。
int len;// 每次读入到byte中的字节的长度,若无则返回-1
while ((len = fis.read(b)) != -1) {
// for (int i = 0; i < len; i++) {
// System.out.print((char) b[i]);
// }
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}



/*可以处理中文字符和英文字符*/
@Test
public void testFileInputStream4() { // abcdefgcde
FileInputStream fis = null;
try {
File file = new File("hello.txt");
fis = new FileInputStream(file);
InputStreamReader isr = new InputStreamReader(fis);
char[] b = new char[5];// 读取到的数据要写入的数组。
int len;// 每次读入到char[]中的长度,若无则返回-1
while ((len = isr.read(b)) != -1) {
System.out.println(len);
for (int i = 0; i < len; i++) {
System.out.print((char) b[i]);
}
String str = new String(b, 0, len);
System.out.print(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}
}

【Tips】:

在Java中,​​new File("name")​​​不会创建新文件,在​​new File()​​​之后需要调用​​createNewFile()​​才能创建新文件;

new FileOutputStream("name")会产生一个新文件;

new FileInputStream("name")不会创建新文件,若文件不存在会报错;

file=new File("name")+new FileInputStream(file)

不会创建新文件,若文件不存在也会报错;

file=new File("name")+new FileOutputStream(file),会创建新文件。