文件对象里面 我们已经掌握了最基础的 创建/删除/遍历/查询 文件和文件夹。并且获取它们的相关信息
这里演示 最基本的 字节流读写文件。 基础比天大,基础牢固的情况下就可以做更复杂的东西。
将 字节 数据 输出到文件里面 再读出来
···
public class InputAndOutputStream1 {
public static void main(String[] args) {
//创建一个文件对象 初始化它的目录
File file = new File("e:/SCfile/maru.txt");
if (file.exists()){
// 长度为2的字节数组
byte data[] = {88, 89};
try {
//创建字节输出流 注意data就是字节数组 所以这里不牵扯什么编码的问题
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data);
fileOutputStream.close();
}catch (IOException e){
e.printStackTrace();
}
}
if (file.exists()){
try {
FileInputStream fileInputStream = new FileInputStream(file);
byte[] datain =new byte[(int) file.length()];
//可以理解为流是把源头的数据读到jvm内存里面
fileInputStream.read(datain);
//我们把字符串数组的内容打出来看看是什么
for(byte b:datain){
System.out.println(b);
}
}catch (IOException e){
e.printStackTrace();
}
}
}
}
//打出来还是88, 89
···
在计算机里面有一张编码表。 字节是最基础的数字,而字符则是我们看得多懂得字母 文本 符号等。
图片.png
所以理解字节流最关键得就是,当你把字符 字符串 通过字节流 输出/输出得时候, 你必须先转化为字节,等数据到了目的地之后,再用编码格式 将字节转化为字符。
上面用文件stream流做实例,其他类型得字节流 本质上都是一样的。
注意事项
上面的代码有一个巨大的安全隐患,建议不要把close()写在try{}里面 :
如果文件不存在,或者读取的时候出现问题而抛出异常,那么就不会执行这一行关闭流的代码,存在巨大的资源占用隐患。
最后给一个复杂的例子:文件拆分
public class TestStream {
public static void main(String[] args) {
int eachSize = 100 * 1024; // 100k
File srcFile = new File("d:/eclipse.exe");
splitFile(srcFile, eachSize);
}
/**
* 拆分的思路,先把源文件的所有内容读取到内存中,然后从内存中挨个分到子文件里
* @param srcFile 要拆分的源文件
* @param eachSize 按照这个大小,拆分
*/
private static void splitFile(File srcFile, int eachSize) {
if (0 == srcFile.length())
throw new RuntimeException("文件长度为0,不可拆分");
byte[] fileContent = new byte[(int) srcFile.length()];
// 先把文件读取到数组中
try {
FileInputStream fis = new FileInputStream(srcFile);
fis.read(fileContent);
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 计算需要被划分成多少份子文件
int fileNumber;
// 文件是否能被整除得到的子文件个数是不一样的
// (假设文件长度是25,每份的大小是5,那么就应该是5个)
// (假设文件长度是26,每份的大小是5,那么就应该是6个)
if (0 == fileContent.length % eachSize)
fileNumber = (int) (fileContent.length / eachSize);
else
fileNumber = (int) (fileContent.length / eachSize) + 1;
for (int i = 0; i < fileNumber; i++) {
String eachFileName = srcFile.getName() + "-" + i;
File eachFile = new File(srcFile.getParent(), eachFileName);
byte[] eachContent;
// 从源文件的内容里,复制部分数据到子文件
// 除开最后一个文件,其他文件大小都是100k
// 最后一个文件的大小是剩余的
if (i != fileNumber - 1) // 不是最后一个
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, eachSize * (i + 1));
else // 最后一个
eachContent = Arrays.copyOfRange(fileContent, eachSize * i, fileContent.length);
try {
// 写出去
FileOutputStream fos = new FileOutputStream(eachFile);
fos.write(eachContent);
// 记得关闭
fos.close();
System.out.printf("输出子文件%s,其大小是 %d字节%n", eachFile.getAbsoluteFile(), eachFile.length());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}