Java帮助如何读取文件linebyline [英] Java help how to read file linebyline

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

问题描述

currentTimeMillis();





while(linReader.hasNext())

{

String line = linReader.nextLine();

System.out.println(line);

}

linReader .close();

long estimatedTime = System.currentTimeMillis() - start_time;

System.out.println(估计时间);

}

}

}

>



我有什么试过:



<

currentTimeMillis();


while (linReader.hasNext())
{
String line = linReader.nextLine();
System.out.println(line);
}
linReader.close();
long estimatedTime = System.currentTimeMillis() - start_time;
System.out.println(estimatedTime);
}
}
}
>

What I have tried:

<

 else{
			 Scanner linReader = new Scanner(inputfile);
			        long start_time = System.currentTimeMillis();


			        while (linReader.hasNext())
			        {
			            String line = linReader.nextLine();
			            System.out.println(line);
			            }
			        linReader.close();
                        long estimatedTime = System.currentTimeMillis() - start_time;
			            System.out.println(estimatedTime);
            }
}
}







我想尝试通过linebyline方法读取文件,我希望时间作为输出。但我的逐行方法不是输出时间。而不是给它时间,它给我这个作为输出



输入输入文件namek.txt

输入输出文件namei.txt

按0和1进入模式

0 - 逐个字符模式

1 - 表示逐行模式

1

k.txt

0



哪个错了




I am trying to read file through linebyline method and i want time as output. But my line by line method is not outputting time. Instead of giving time, it is giving me this as output

Enter an input file namek.txt
Enter an output file namei.txt
Enter an mode by pressing 0 and 1
0 -- character-by-character mode
1 -- means line-by-line mode
1
k.txt
0

Which is wrong

推荐答案

未阅读您的源代码

这里有一个示例:

Did not read your source code
here is a sample example:
FileInputStream fstream;
BufferedReader br;
fstream = new FileInputStream(fileName);
br = new BufferedReader(new InputStreamReader(fstream, "UTF-8"));
String line;
while ((line = br.readLine()) != null) {
    System.out.printlne(line);
}
br.close();


您正在使用输入文件字符串创建一个Scanner对象,因此扫描程序将扫描那个文字。您需要使用实际文件创建它:

You are creating a Scanner object using the inputfile string, so the Scanner will scan that text. You need to create it using the actual file thus:
Scanner linReader = new Scanner(inputStream);



您需要更多地使用Java文档:Scanner(Java Platform SE 7) [ ^ ]。


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

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