Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Scala中计算文件的行数_Scala_File Io_Newline_Standard Library - Fatal编程技术网

在Scala中计算文件的行数

在Scala中计算文件的行数,scala,file-io,newline,standard-library,Scala,File Io,Newline,Standard Library,我现在正在学习,这是我用来计算文本文件中行数的代码片段 //returns line number of a file def getLineNumber(fileName: String): Integer = { val src = io.Source.fromFile(fileName) try { src.getLines.size } catch { case error: FileNotFoundException =

我现在正在学习,这是我用来计算文本文件中行数的代码片段

  //returns line number of a file
  def getLineNumber(fileName: String): Integer = {
    val src = io.Source.fromFile(fileName)
    try {
      src.getLines.size     
    } catch {
      case error: FileNotFoundException => -1
      case error: Exception => -1
    }
    finally {
      src.close()
    }
  }
我使用的是Source.fromFile方法,如中所述。问题是:如果我的文本文件是这样的:

baris

ayse


deneme
我得到了正确的结果。如果我在单词deneme之后按enter键,我仍然会得到数字6,但在这种情况下我会导出7。如果我按回车键后按空格键,我得到7,这也是正确的。这是Scala标准库中的一个bug还是我遗漏了什么

最后,我的基本主要方法如下:

  def main(args: Array[String]): Unit = {
    println(getLineNumber("C:\\Users\\baris\\Desktop\\bar.txt"))
  }

如果在单词deneme后按enter键,只需在第6行添加一个行尾序列(在您的情况下为CR+LF)。您可以看到光标移到新行,但并没有创建新行:您只需指定第六行结束。要创建新行,必须在行尾序列后添加一个字符,就像按空格键一样。

如果在单词deneme后按enter键,只需在第6行添加一个行尾序列(在您的情况下为CR+LF)。您可以看到光标移到新行,但并没有创建新行:您只需指定第六行结束。要创建新行,必须在行尾序列后添加一个字符,就像按空格键一样。

它使用
java.io.BufferedReader
readLine
。以下是该方法的来源:

/**
 * Reads a line of text.  A line is considered to be terminated by any one
 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 * followed immediately by a linefeed.
 *
 * @return     A String containing the contents of the line, not including
 *             any line-termination characters, or null if the end of the
 *             stream has been reached
 *
 * @exception  IOException  If an I/O error occurs
 *
 * @see java.nio.file.Files#readAllLines
 */
public String readLine() throws IOException {
    return readLine(false);
}
这就叫:

...
* @param      ignoreLF  If true, the next '\n' will be skipped
...
String readLine(boolean ignoreLF) ...
...
/* Skip a leftover '\n', if necessary */
            if (omitLF && (cb[nextChar] == '\n'))
                nextChar++;
            skipLF = false;
            omitLF = false;

基本上就是这样实现的。我想这取决于一条线对你意味着什么。您正在计算包含某些字符或新行字符的行数吗?-不同的东西很明显。

它使用
java.io.BufferedReader
readLine
。以下是该方法的来源:

/**
 * Reads a line of text.  A line is considered to be terminated by any one
 * of a line feed ('\n'), a carriage return ('\r'), or a carriage return
 * followed immediately by a linefeed.
 *
 * @return     A String containing the contents of the line, not including
 *             any line-termination characters, or null if the end of the
 *             stream has been reached
 *
 * @exception  IOException  If an I/O error occurs
 *
 * @see java.nio.file.Files#readAllLines
 */
public String readLine() throws IOException {
    return readLine(false);
}
这就叫:

...
* @param      ignoreLF  If true, the next '\n' will be skipped
...
String readLine(boolean ignoreLF) ...
...
/* Skip a leftover '\n', if necessary */
            if (omitLF && (cb[nextChar] == '\n'))
                nextChar++;
            skipLF = false;
            omitLF = false;

基本上就是这样实现的。我想这取决于一条线对你意味着什么。您正在计算包含某些字符或新行字符的行数吗?-显然不同的东西。

检查文本编辑器的设置。看起来不是Scala在做。请检查文本编辑器的设置。看起来不是Scala干的。谢谢,这和新线的解释有关。javadoc中的“一行被认为是由换行符('\n')、回车符('\r')或回车符紧跟着换行符中的任何一个终止的。”这句话解释了所有情况。感谢您,它与解释新行有关。javadoc中的句子“一行被认为是由换行符('\n')、回车符('\r')或紧接着换行符的回车符中的任何一个终止的。”解释了每一种情况。