- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个缓冲阅读器正在传输文件。目前有两种情况:
它正在流式传输在一台 PC 上生成的文件,我们将其称为 File1。它正在流式传输另一台计算机上生成的文件,我们将其称为 File2。
我假设我的问题是由 EOL 引起的。
BufferedReader 会读取这两个文件,但对于 File2,它会为每个新行读取一个额外的空行。
此外,当我使用 line.equalsIgnoreCase("abc")
比较该行时,鉴于该行是 "abc"
它不会返回 true .
将此代码与两个链接中提供的两个文件一起使用来复制问题:
public class JavaApplication {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
File file = new File("C:/Users/User/Downloads/html (2).htm");
BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));
String line = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
}
}
注意第二个文件如何在每行后打印一个空行...
我一直在寻找、尝试、寻找、尝试,但找不到解决方案。
有什么想法可以解决这个问题吗? (尤其是比较的事情?)
最佳答案
对我有用。
public class CRTest
{
static StringReader test = new StringReader( "Line 1\rLine 2\rLine 3\r" );
public static void main(String[] args) throws IOException {
BufferedReader buf = new BufferedReader( test );
for( String line = null; (line = buf.readLine()) != null; )
System.out.println( line );
}
}
打印:
run:
Line 1
Line 2
Line 3
BUILD SUCCESSFUL (total time: 1 second)
正如 Joop 所说,我认为您混淆了哪个文件不起作用。请使用上面的骨架创建一个MCVE并向我们准确展示哪些文件输入不适合您。
<小时/>由于您似乎有一个包含反向 \r\n
行的文件,这是我第一次尝试修复。请测试一下,我还没试过。您需要使用此类包装 InputStreamReader
,然后像平常一样将 BufferedReader
包装在外部。
class CRFix extends Reader
{
private final Reader reader;
private boolean readNL = false;
public CRFix( Reader reader ) {
this.reader = reader;
}
@Override
public int read( char[] cbuf, int off, int len )
throws IOException
{
for( int i = off; i < off+len; i++ ) {
int c = reader.read();
if( c == -1 )
if( i == off ) return -1;
else return i-off-1;
if( c == '\r' && readNL ) {
readNL = false;
c = reader.read();
}
if( c == '\n' )
readNL = true;
else
readNL = false;
cbuf[i] = (char)c;
}
return len;
}
@Override
public void close()
throws IOException
{
reader.close();
}
}
关于java - BufferedReader 被不同的行分隔符搞乱了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36582103/
所以我昨天尝试开始使用 std::initializer_list 但这并不是一个巨大的成功。这是我最后的尝试之一: #include #include struct XmlState
我读到,JVM gc 在找到无法访问的对象后,会运行该对象的 Finalize() 方法。然后它检查该对象是否仍然无法访问,如果不是则删除它。所以我写了一个终结,使它的对象的引用再次可用: publi
所以,我收到了一些文件,其中包含一堆这样的东西 var time=0;//ÓÎϷʱ¼ä 很明显,评论对应的东西,我想看,但是乱七八糟的,我不知道为什么。 1) 我怎么读这个 2)如何避免 最佳答案
在阅读之前,请注意我是 jquery、html 和 css 方面的初学者。 在将 jquery 安装到我的网站之前,我将页眉和页脚固定在固定位置,以便它们粘在浏览器的顶部和底部。当我安装jquery时
我用Minamaze1.1.2主题免费添加了一个网站。在更改 CSS 代码时,我以某种方式更改了 slider 图片的尺寸。现在左侧与右侧不成比例。 在网址中可以看到: http://springlo
我已经创建了一个下拉框阴影和悬停图像的示例,除了通常的问题 internet explerror 之外,它似乎工作正常。你可以在这里查看 fiddle : www.jsfiddle.net/kcD6j
我正在按照 Spring 教程开始使用 JPA,并且我有一个带有 user 表的基本 mysql 数据库。 我已按如下方式设置User.java: package com.test.models; i
假设我有三个表 - 用户、服务器和付款。每个用户可以拥有多个服务器,每个服务器可以进行多次支付。假设我想查找最近的付款并获取有关这些付款所附加的服务器/客户的信息。这是一个可以执行此操作的查询: SE
我为我的购物车、注册、登录和联系表启用了 SSL。当启用 SSL 时,jquery 被禁用。我看到谷歌也有使用 https 导入的 jquery。 这是页面 https://americanbookc
这是一个标准任务:在应用程序的共享首选项中存储一些值,以便以后能够检索它。但是人们会发现有 3 个函数可以在其中存储一个值: //1. public static SharedPreferences
我正在尝试使用 SLRequest iOS API 获取 facebook 数据,这似乎工作正常 - NSDictionary *parameters = @{}; NSURL *feedURL =
好的,所以我用 jQuery 实现了整个页面的隐藏/淡入效果。一切都很顺利,直到我意识到delay() + fadeIn()导致我的 Hashtag Links 以顶部的滚动位置加载,而不是 #my_
因此,在我将 DNS 移至 Godaddy 之前,该站点在 Internet Explorer 中运行良好。这是网站.. http://csdassociates.com/ ...关于为什么这不再正确
由于某些奇怪的原因,我放置在任何 gridview 中的所有按钮看起来都真的缩小了(如下图所示),即使 gridview 是默认的并且没有附加 CSS。 会不会是其他一些 CSS,因为我有太多代码无法
我目前正在实现一个身份验证系统,其中用户登录并从服务器接收 JWT token ,该 token 存储在 localStorage 中。我还编写了一个自定义 HttpInterceptor,它将用户的
我正在尝试使 QGridLayout 可滚动。它可能包含几个自定义小部件,小部件的数量不固定。当有超过 x 个小部件时,QGridLayout 必须是可滚动的,x 是任意数字。 问题是,当我使用 QS
更改我的系统 PATH 变量似乎把一切都搞砸了。 因此,我尝试运行 rake device:android:product 来制作我一直在使用 RhoMobile/Rhodes 开发的应用程序的 AP
我有一些中文字符(例如中文(简体))存储在内容类型为utf8_bin的mysql数据库中。 我使用以下代码从数据库中提取数据: if($stmt = $mysqli->prepare("SELECT
我们需要创建和销毁 QApplication 的实例,因为我们想在现有主机应用程序的插件中使用 Qt。 void multiQT() { int argc = 0; QApplicat
我是一名优秀的程序员,十分优秀!