在写入文件和创建新文件时检查文件大小 [英] check file size while writing file and create new file

查看:131
本文介绍了在写入文件和创建新文件时检查文件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试编写java代码来编写.dat文件..同时拧紧检查大小...每200 KB创建新的.dat继续写入文件。我有什么方法可以做到这一点..

Trying to Write java Code to write ".dat" File.. While wring check size as well.. every 200 KB create new ".dat" continue to write file. Is there any way that i can do that..

当文件超过200KB并创建一个像这样的新文件名时

when file more than 200KB and create new one filename like this

第一个文件名.. 1_filename
第二个文件名.. 2_filename
第三个文件名.. 3_filename ........

1st file name.. 1_filename 2nd file name.. 2_filename 3rd file name.. 3_filename........

或者如果文件只创建一个文件意味着小于200KB而不是

or if file create only one file means less than 200KB than

Filenamd .. filename代码..

Filenamd.. filename as in code..

请帮帮我..

我写代码写文件但是..它只写了2个文件..那不是正确的方法..请帮助我..

I wrote code to write files but.. its only writing 2 files.. and that's not right way to do it.. please help me..

这是我的代码..

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class xmlfile1filebytes {

  public static void main(String[] args) throws IOException {

    File folder = new File ("07072013");
    File[] listOfFiles = folder.listFiles();

    System.out.println("There are " + listOfFiles.length + " files"); 
    String filesin;

    String timeStamp = new SimpleDateFormat("MM-dd-yyyy[HH.mm.ss]")
     .format(Calendar.getInstance().getTime());
     System.out.println(timeStamp);

    BufferedWriter xmlfile = null;
    BufferedWriter datfile = null;

    String outxmlfile = ("07072013\\" + timeStamp + ".xml");
    xmlfile = new BufferedWriter(new FileWriter(outxmlfile));

    String outdatfile = ("07072013\\" + timeStamp + ".dat");
    datfile = new BufferedWriter(new FileWriter(outdatfile));

    int offset = 0;
    int size = 0;

    for (int i = 0; i < listOfFiles.length; i++) {

        File f = listOfFiles[i];

       // System.out.println(i + " " + f.getAbsolutePath());
        if (f.isFile()) {

            filesin = listOfFiles[i].getName();

            if (filesin.endsWith("pdf")) {

                Path aPath = Paths.get(f.getAbsolutePath()); 

                System.out.println(filesin);

                byte[] actualBytes = Files.readAllBytes(aPath);
                size = actualBytes.length;

                xmlfile.append((i + 1) + ")" + " File = " + filesin + ", Offset = " + offset + ", Size = " + size + "\n");


                offset = offset + size;
                xmlfile.newLine();

                String s = new String(actualBytes);

                datfile.append(s);
                datfile.newLine();


                File datfileinfolder = new File ("07072013\\" + timeStamp + ".dat");

                long datfilesize = datfileinfolder.length();
                long datfilesizeinkb = datfilesize /1024;

                System.out.println(datfilesizeinkb);

                if (datfilesizeinkb >= 200) {
                     datfile.close();

                      BufferedWriter datfile1 = null;
                      String outdatfile1 = ("07072013\\" + "1_"+ timeStamp + ".dat");
                      datfile1 = new BufferedWriter(new FileWriter(outdatfile1));

                      String s1 = new String(actualBytes);
                      datfile1.append(s1);
                      datfile1.close();
                }


             }
        }
    }

     xmlfile.close();
  }
}

写入文件超过400 KB时出错..

And I get error when write file more than 400 KB..

错误:

There are 10 files
07-09-2013[16.03.00]
1192970_eBill_20130709.pdf
96
1321470_eBill_20130709.pdf
208
1724897_eBill_20130709.pdf
Exception in thread "main" java.io.IOException: Stream closed
    at java.io.BufferedWriter.ensureOpen(Unknown Source)
    at java.io.BufferedWriter.write(Unknown Source)
    at java.io.Writer.write(Unknown Source)
    at java.io.Writer.append(Unknown Source)
    at xmlfile1filebytes.main(xmlfile1filebytes.java:65)

请帮帮我!!提前致谢!!
谢谢!!

Please help me!! Thanks In advanced!! Thanks!!

推荐答案

我建议你创建一个单独的类,它唯一的工作是创建文件(按顺序),将数据写入文件,密切关注大小,并根据需要关闭/打开文件。这将使您的代码更有条理 - 您将能够更清楚地看到您的错误。该类将具有以下方法:

I suggest you create a separate class whose only job it is to create files (in sequence), write data to the file, keep an eye on the size, and close / open files as needed. This will make your code more organized - you will be able to see your mistakes more clearly. The class would have the following methods:

setMax(maxVal)                 - set the maximum file size (default 400k)
setRoot(rootPath)              - folder where files will be created
setName(rootName)              - "format string" used to generate file names 
                                 (e.g. "myFile%03d.dat")
writeData(dataSize, dataBlock) - write data. If no file open, open it. 
                                 If file too big, split it.  
closeFile()                    - flush buffers, close the last file.
                                 Could be part of the destructor
currentFile()                  - returns name of current file (for debug)
currentSize()                  - returns current file size (for debug)

如果你能弄清楚如何编写这个课程,你将解决你的初始问题并有一些你可以同时重复使用的东西。

If you can figure out how to write this class you will solve your initial problem and have something you can re-use at the same time.

如果您想(大部分)保留您已有的代码结构,那么您的修复就是删除当前行85.

If you want to stay with (mostly) the code structure you already have, then your fix is to remove your current line 85.

         83:         String s1 = new String(actualBytes);
         84:         datfile1.append(s1);
         85:         datfile1.close();  <<<<<<<<<<< remove this line
         86:      }

关闭文件后,下次尝试写入它将失败 - 这就是你所看到的错误。

Once you have closed the file, your next attempt to write to it will fail - and that's the error you are seeing.

这篇关于在写入文件和创建新文件时检查文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆