java java中的“文件”数据类型是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14808219/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
what is "file " datatype in java..?
提问by joy
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();
}
}
回答by KyelJmD
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
的File
是参考的类型。它用于处理文件(例如创建)通过查看此链接,您会更容易理解它
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.
仅通过查看您在上一个答案中的评论,我发现您不知道/不熟悉 java 中的不同数据类型,有两种数据类型。
- Primitive Data Types (char,int,boolean)
- Reference Types/Object(User
Defined classes, the Superclass
Object
and in your case, theFile
)
- 原始数据类型(char、int、boolean)
- 引用类型/对象(用户定义的类,超类
Object
,在您的情况下,是File
)