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

我試圖將一個大文件分成單獨的較小文件。這個文件基本上是新行上的每個數字列表。我使用下面的代碼根據每個20字節的字節分隔文件。我面臨的問題是數字不完整。例如,如果原始文件是

123
2456
3456

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)

這只是一個例子。我需要的是由correctfile表示。我知道這是可能的,因為我使用字節打破它。有什么方法可以實現這一點(注意不能使用數組或任何東西,因為我必須通過網絡傳輸字節)

 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 = bis.read(buffer)) > 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.tracker.jobList.add(fileName);
                        //this.parentString = f.getParent();
                    }
                }
            }
        }

2 个解决方案

#1


You can Try StringTokenizer

你可以試試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.

字符串標記化器類允許應用程序將字符串分解為標記。標記化方法比StreamTokenizer類使用的方法簡單得多。

最佳答案:

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