如何计算JAVA中的行数

如何计算JAVA中的行数

本文介绍了帮助:如何计算JAVA中的行数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 你好其他程序员,我有一个问题:编写一个静态方法,Display()接受表示文件名的字符串。该方法应该只读取并显示文件内容的前五行。如果文件包含少于5行,则应显示文件的全部内容。 (假设我已完成所有必要的导入)这是我的尝试: public static void Display(String filename)throws IOException { File file = new File(filename); Scanner read = new Scanner(file); while(read.hasNext()) { //到目前为止我已经尝试过这个 int count = 0; String lineReader = read.nextLine(); count ++; if(count< 5) { System.out.println(lineReader); } } read.close(); } 解决方案 Hello there fellow programmers, I have a question: Write a static method, Display() that accepts a string representing the name of the file. The method should read and display only the first the five lines of the file''s contents. If the file contains less than 5 lines, it should display the file''s entire contents.(Assuming that I made all the necessary imports) Here is my attempt:public static void Display (String filename) throws IOException{ File file = new File (filename); Scanner read = new Scanner (file); while (read.hasNext()) {//So far I have tried this int count=0; String lineReader = read.nextLine(); count++; if (count <5) { System.out.println(lineReader); } } read.close();} 解决方案 这篇关于帮助:如何计算JAVA中的行数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-30 06:41