fileSize帮助 [英] fileSize help

查看:40
本文介绍了fileSize帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用上一个答案中的countChars()方法,编写一个名为fileSize()的方法,声明如下:

public int fileSize(String name){

。 ..

}


它计算文件中的字符数,其名称作为名称提供。参数。此方法应返回文件中的字符数,如果发生IOException,则返回-1。




public int fileSize(String name)抛出IOException

{


try {

InputStreamReader isr = new InputStreamReader(new FileInputStream(" name")));

int charLength = 0;


while(isr.read()!= -1)

charLength ++;


返回charLength;

}


catch(例外e){

返回-1;

}

}


它总是返回-1

我知道哪里出错了?

解决方案

名为name的文件是否为在您当前的工作目录中存在?您可以从Java找到

当前工作目录,如下所示:

< span class =codeLinkonclick =Blur(this,this.parentNode.parentNode,getChildren(this),true);>展开 | 选择 | Wrap | 行号


但问题是否未说明该文件被称为名称?我提交作品时是否有文件并不重要。

是否与我放置的地方有关尝试{


谢谢对于帮助



但问题是否未说明该文件被称为名称?我提交作品时是否有文件并不重要。

是否与我放置的地方有关尝试{


谢谢对于帮助



不,你的方法应该采用一个String参数,该参数应该是一个文件的名称。字面意思是文件的名称应该是name,即如果你有一个名为fronobulax.foo的文件,那么

。在你当前的工作目录中传递

字符串到你的方法。


亲切的问候,


Jos


Using the countChars() method from the previous answer, write a method called fileSize() declared as follows:
public int fileSize (String name) {
...
}

which counts the number of characters in the file whose name is supplied as the "name" parameter. This method should return the number of characters in the file, or -1 if an IOException occurs.




public int fileSize(String name) throws IOException
{

try {
InputStreamReader isr = new InputStreamReader(new FileInputStream("name"));
int charLength = 0;

while (isr.read() != -1)

charLength++;

return charLength;
}

catch (Exception e) {
return -1;
}
}

it always returns -1
any know where i am going wrong?

解决方案

Does a file named "name" in your current working directory exists? You can find
your current working directory from Java as follows:

Expand|Select|Wrap|Line Numbers


But does the question not state that the file is called "name"? it would not matter whether i have the file or not when i submit my work.
Is it something to do with where i have placed try {

thanks for the help


But does the question not state that the file is called "name"? it would not matter whether i have the file or not when i submit my work.
Is it something to do with where i have placed try {

thanks for the help

Nope, your method is supposed to take a String argument which is supposed to
be the name of a file. Not literally that the name of the file should be "name", i.e.
if you have a file named "fronobulax.foo" in your current working directory pass
that string to your method.

kind regards,

Jos


这篇关于fileSize帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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