-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

ドキュメントに基づく

ファイルとディレクトリのパス名の抽象表現。

File参照型です。ファイルの処理(作成など)に使用されます。このリンクを見ると、理解しやすくなります。

アップデート

前の回答からのコメントを見るだけで、Javaのさまざまなデータ型を知らない/よく知らないことがわかりました。2つのデータ型があります。

  1. プリミティブデータ型(char、int、boolean)
  2. 参照型/オブジェクト(ユーザー定義クラス、スーパークラスObject、そしてあなたの場合は File
于 2013-02-11T08:23:20.693 に答える
2

ファイルObjectは、ファイルを処理するために使用されるオブジェクトです。ここでドキュメントを読むことができます。

于 2013-02-11T08:20:51.207 に答える