好的,我尝试了一切,但我找不到答案。我的数据读取器在从txt文件读取时跳过空的下一行。 它应该从txt文件中删除所有注释并按原样打印其余数据。我的读者确实删除了评论和评论。打印数据,但它会跳过空的新行..
public String readLine()
{
String buf = new String();
String readStr = new String();
int end = 0;
int done = 0;
try
{
// checks if line extraction is done and marker has non null value
while (done != 1 && marker != null)
{
readStr = theReader.readLine(); // Reads the line from standard input
if (readStr != null)
{
/* If the first character of line isnt marker */
if (readStr.length() > 0)
{
if (!readStr.substring(0, 1).equalsIgnoreCase(marker))
{
end = readStr.indexOf(marker); // checks if marker exists in the string or not
if (end > 0)
buf = readStr.substring(0, end);
else
buf = readStr;
done = 1; // String extraction is done
}
}
}
else
{
buf = null;
done = 1;
}
}
}
// catches the exception
catch (Exception e)
{
buf = null;
System.out.println(e);
}
return buf;
}
String myStr = new String();
myStr = _mdr.readLine();
while (myStr != null)
{
//System.out.println("Original String : " + myStr);
System.out.println(myStr);
myStr = _mdr.readLine();
}
答案 0 :(得分:2)
if(readStr.length()> 0)
这是跳过空行的代码行。
答案 1 :(得分:1)
此代码中存在许多问题,但您要处理的主要问题是readLine结果中不包含新行。因此,您的if语句不为真(该行实际上是空的
答案 2 :(得分:1)
您的读者不会在readStr中包含换行符号,因此在“\ n”行中读取将使readStr为“”,并且
readStr.length()> 0
将评估为false,从而跳过该行。