BYTES逐行打破大文件:java - Break large file line by line by BYTES :java -开发者知识库

BYTES逐行打破大文件:java - Break large file line by line by BYTES :java -开发者知识库,第1张

I am trying to break a large file into separate smaller files. This files is basically a list of number each on new line. I am using the code below to separate the file based on bytes each of 20bytes. The problem I am facing is that the numbers dont remain intact. For example if the original file is



the result I am getting is


file1     file2          CorrectFile1    CorrectFile2
1                             123           3456
2456      3                   2456
456       23

This is just an example. What i need is represented by correctfile. I get that this is possible because i am breaking it using bytes. Is there any way I can achieve that (Note cant use array or anything as I have to transfer the byte over the network)


 public static void splitFile(File f) throws IOException {
            System.out.println("entering split file");
            int splitCounter = 1;

            int sizeOfFiles = 20; // 200kb
            byte[] buffer = new byte[sizeOfFiles];

            try (BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(f))) {
                //String name = this.getFileNameWithoutExtension(f.getName());

                int tmp = 0; // chunk size
                while ((tmp = > 0) {
                    //File newFile = new File("f.txt");
                    String fileName = String.format("%sd.txt", "job",splitCounter  );
                    File newFile = new File(f.getParent(),fileName);
                    try (FileOutputStream out = new FileOutputStream(newFile)) {
                        out.write(buffer, 0, tmp);
                        //this.parentString = f.getParent();

2 个解决方案


You can Try StringTokenizer


The string tokenizer class allows an application to break a string into tokens. The tokenization method is much simpler than the one used by the StreamTokenizer class.



DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
U19学习网站 » BYTES逐行打破大文件:java - Break large file line by line by BYTES :java -开发者知识库