Self Java -I / O byte stream

Java byte stream InputStream OutputStream

InputStream input stream of bytes
OutputStream output stream of bytes
to read and write data in bytes

Step. 1: the ASCII code

All data is stored in the computer are stored in digital form. So the letters need to be able to be converted to digital storage .
A corresponding digital to example 65, different numbers of A corresponds to a corresponding figure 97. letters and symbols, is a piece of code table.
It is an ASCII code table. It contains only simple English letters, symbols, numbers, and so on. It does not include Chinese, German, Russian and other complex.

Examples are listed in the visible and the ASCII codes corresponding to decimal and hexadecimal numbers, listed invisible Write
Here Insert Picture Description
Step 2: reading the contents of a file byte stream

InputStream is the input stream of bytes, but also an abstract class, method declarations only, does not provide specific implementation of the method.
FileInputStream is a subclass of InputStream to FileInputStream example file read

package stream;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
  
public class TestStream {
  
    public static void main(String[] args) {
        try {
            //准备文件lol.txt其中的内容是AB,对应的ASCII分别是65 66
            File f =new File("d:/lol.txt");
            //创建基于文件的输入流
            FileInputStream fis =new FileInputStream(f);
            //创建字节数组,其长度就是文件的长度
            byte[] all =new byte[(int) f.length()];
            //以字节流的形式读取文件所有内容
            fis.read(all);
            for (byte b : all) {
                //打印出来是65 66
                System.out.println(b);
            }
             
            //每次使用完流,都应该进行关闭
            fis.close();
              
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
          
    }
}

Step 3: In a stream of bytes to write data to a file

OutputStream is the output stream of bytes, but also an abstract class only provides a method declaration does not provide specific implementation method.
FileOutputStream is a subclass of OutputStream to write data to a file, for example FileOutputStream

Note: If the file d: /lol2.txt does not exist, write operation will automatically create the file.
But if the file d: /xyz/lol2.txt, and directory xyz not exist, it will throw an exception
In the form of a stream of bytes to write data to a file

package stream;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
 
public class TestStream {
 
    public static void main(String[] args) {
        try {
            // 准备文件lol2.txt其中的内容是空的
            File f = new File("d:/lol2.txt");
            // 准备长度是2的字节数组,用88,89初始化,其对应的字符分别是X,Y
            byte data[] = { 88, 89 };
 
            // 创建基于文件的输出流
            FileOutputStream fos = new FileOutputStream(f);
            // 把数据写入到输出流
            fos.write(data);
            // 关闭输出流
            fos.close();
             
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
}

Exercise : Split File

Find a file larger than 100k, and 100k according to the unit, split into multiple sub-file, and the number to the file name as the end.
Such as file eclipse.exe, size is 309k.
After splitting, become
the eclipse.exe-0
the eclipse.exe. 1-
the eclipse.exe 2-
the eclipse.exe. 3-
Here Insert Picture Description
Answer :

package stream;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
  
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();
            }
        }
    }
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11730133.html