hive 自定义分隔符_在Java中自定义行分隔符行为

hive 自定义分隔符

terminator" to refer to the characters that signify the end of a line of text, but the Java API documentation uses "line 终止符 ”一词来表示表示文本行尾的字符,但是Java API文档使用“行separator," so I've used that here, for consistency.分隔符 ”,所以我为了保持一致性,在这里使用了它。

If you've ever run the same Java code on a Windows-based machine and a Unix- or Linux-based machine, you've probably noticed that lines of text don't always end the same way. Windows uses two characters: a carriage return (CR) and a line feed (LF). (Java uses '\r' and '\n' to represent these characters, respectively.) A good chunk of the rest of the world gets by with just an LF character ('\n'). Java does a pretty decent job of insulating developers from this distinction by having BufferedWriter.newLine() use whatever line separator is appropriate for the current operating system. (System.out.println(), by the way, typically is a call to PrintStream.println(), which calls BufferedWriter.newLine() behind the scenes.)

如果您曾经在基于Windows的计算机和基于Unix或Linux的计算机上运行相同的Java代码,则可能已经注意到文本行不一定总是以相同的方式结束。 Windows使用两个字符:回车符(CR)和换行符(LF)。 (Java分别使用'\ r''\ n'来表示这些字符。)世界上其他大部分地区仅靠LF字符( '\ n' )就可以了。 Java通过使BufferedWriter.newLine()使用适合当前操作系统的任何行分隔符,使开发人员免受这种区分,所做的相当不错的工作。 (顺便说一下, System.out.println()通常是对PrintStream.println()的调用,该调用在幕后调用BufferedWriter.newLine() 。)

What if you need to override that behavior, though?

但是,如果您需要覆盖该行为怎么办?

A few years ago, we at Experts Exchange ran into an issue with an internal tool where some of our machines were using Windows-style line separators and others were using the Unix-style. We needed a way to keep things consistent across platforms and had a few options open to us:

几年前,我们在Experts Exchange上遇到了一个内部工具问题,其中我们的某些计算机使用Windows风格的行分隔符,而其他计算机则使用Unix风格。 我们需要一种方法来保持平台之间的一致性,并为我们提供一些选择:

  • We could have overridden the line separator behavior for the entire JVM by specifying a value for the line.separator system property. We didn't want to do this, though, because it would have required making sure everybody was running the internal tool in the same, specific way. One person using the wrong command-line arguments would have sent us back to the drawing board.

    通过为line.separator系统属性指定一个值,我们可以覆盖整个JVM的行分隔符行为。 但是,我们不想这样做,因为这将需要确保每个人都以相同的特定方式运行内部工具。 一个人使用错误的命令行参数会把我们送回绘图板。
  • We could have tried using System.setProperty() to set that property using code. This would have ensured some consistency, but there's a problem: the System.lineSeparator() method only ever returns what that property was when the JVM started—setting it later has no effect. Various bits of code could wind up using different line separators, depending on whether the value came from System.lineSeparator() or from System.getProperty("line.separator"). We didn't want to try to figure out what side effects that might have.

    我们可以尝试使用System.setProperty()通过代码设置该属性。 这样可以确保一定的一致性,但是存在一个问题: System.lineSeparator()方法仅返回JVM启动时的该属性,而以后再进行设置则无效。 可以使用不同的行分隔符来结束各种代码,具体取决于值是来自System.lineSeparator()还是来自System.getProperty(“ line.s 分隔符”) 。 我们不想尝试找出可能产生的副作用。

Ultimately, we picked an option that we knew wouldn't have any side effects: override the behavior in the instance of BufferedWriter we were using.

最终,我们选择了一个我们知道不会有任何副作用的选项:覆盖我们正在使用的BufferedWriter实例中的行为。

BufferedWriter writer = new BufferedWriter(outputStream)
{
   /**
    * Writes an LF character on all platforms, regardless of the value of the <code>line.separator</code> property.
    */
   @Override
   public void newLine() throws IOException
   {
      write('\n');
   }
};

This solution confined the custom behavior to the exact spot we needed it and kept everything running smoothly.

该解决方案将自定义行为限制在我们需要的确切位置,并保持一切平稳运行。

翻译自: https://www.experts-exchange.com/articles/17160/Customizing-line-separator-behavior-in-Java.html

hive 自定义分隔符

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值