-5
import java.io.*;

class SplitFile    
{    
    private File fSplit;
    private int sizeInBytes;

    private int count;

    public static void main(String[] args) throws IOException
    {    
        Console con = System.console();

        String fileName;

        int size = 0;

        System.out.print("Enter the file name to split: ");

        fileName = con.readLine();

        System.out.print("Enter the size of the target file: ");

        size = Integer.parseInt(con.readLine());

        SplitFile sf = new SplitFile(fileName, size);

        sf.split();

    }

    public File checkFileExists(String fName)    
    {    
        File f = new File(fName);

        if (!f.exists())    
        {    
            System.out.println("File " + fName + " does not exists");

            System.exit(0);

        }

        return f;

    }

    public int validateSize(int s)    
    {    
        if (fSplit.length() < s)    
        {

            System.out.println("Invalid Size");

            System.exit(0);

        }

        return s;

    }

    public String createNextFileName()    
    {    
        ++count;

        String fileName;

        fileName = "part_" + count + "." + fSplit.getName();

        return fileName;

    }

    public SplitFile(String fName, int s)    
    {

        fSplit = checkFileExists(fName);

        sizeInBytes = validateSize(s);

        count = 0;

    }

    public void split() throws IOException

    {

        FileInputStream fis = new FileInputStream(fSplit);

        BufferedInputStream bis = new BufferedInputStream(fis);

        File fileSegment = new File(createNextFileName());

        FileOutputStream fos = new FileOutputStream(fileSegment);

        BufferedOutputStream bos = new BufferedOutputStream(fos);

        int ch;

        int currentByteCount = 0;

        while ((ch = bis.read()) != -1)

        {

            bos.write(ch);

            ++currentByteCount;

            if (currentByteCount == sizeInBytes)

            {

                bos.close();

                fos.close();

                fileSegment = new File(createNextFileName());

                fos = new FileOutputStream(fileSegment);

                bos = new BufferedOutputStream(fos);

                currentByteCount = 0;

            }

        }

        bis.close();

        fis.close();

        bos.close();

        fos.close();

    }

}
4

2 回答 2

5

based from the documentation

An abstract representation of file and directory pathnames.

The File is a reference type. it is used for handling files (eg. creating) It would be more easier for you to understand it by looking at this link

UPDATE

Just by looking at your comments from the previous answer I've observed that you do not know/unfamiliar with the different data types in java, there are two data types.

  1. Primitive Data Types (char,int,boolean)
  2. Reference Types/Object(User Defined classes, the Superclass Object and in your case, the File)
于 2013-02-11T08:23:20.693 回答
2

文件Object就是一个用于处理文件的对象,您可以在此处阅读它的文档。

于 2013-02-11T08:20:51.207 回答