Java file line separator

Prefer System.lineSeparator() for Writing System-Dependent Line Separator Strings in Java

Join the DZone community and get the full member experience.

JDK 7 introduced a new method on the java.lang.System class called lineSeparator(). This method does not expect any arguments and returns a String that represents «the system-dependent line separator string.» The Javadoc documentation for this method also states that System.lineSeparator() «always returns the same value — the initial value of the system property line.separator .» It further explains, «On UNIX systems, it returns » \n «; on Microsoft Windows systems it returns » \r\n «.»

Given that a Java developer has long been able to use System.getProperty(«line.separator») to get this system-dependent line separator value, why would that same Java developer now favor using System.lineSeparator instead? JDK-8198645 [«Use System.lineSeparator() instead of getProperty(«line.separator»)»] provides a couple reasons for favoring System.lineSeparator() over the System.getProperty(String) approach in its «Description»:

A number of classes in the base module use System.getProperty(«line.separator») and could use the more efficient System.lineSeparator() to simplify the code and improve performance.

As the «Description» in JDK-8198645 states, use of System.lineSeparator() is simpler to use and more efficient than System.getProperty(«line.separator») . A recent message on the core-libs-dev mailing list provides more details and Roger Riggs writes in that message that System.lineSeparator() «uses the line separator from System instead of looking it up in the properties each time.»

The performance benefit of using System.lineSeparator() over using System.getProperty(«line.separator») is probably not all that significant in many cases. However, given its simplicity, there’s no reason not to gain a performance benefit (even if tiny and difficult to measure in many cases) while writing simpler code. One of the drawbacks to the System.getProperty(String) approach is that one has to ensure that the exactly matching property name is provided to that method. With String -based APIs, there’s always a risk of spelling the string wrong (I have seen «separator» misspelled numerous times as «seperator»), using the wrong case, or accidentally introducing other typos that prevent exact matches from being made.

Читайте также:  Изображения

The JDK issue that introduced this feature to JDK 7, JDK-6900043 («Add method to return line.separator property»), also spells out some benefits in its «Description»: «Querying the line.separator value is a common occurrence in large systems. Doing this properly is verbose and involves possible security failures; having a method return this value would be beneficial.» Duplicate JDK-6264243 («File.lineSeparator() to retrieve value of commonly used ‘line.separator’ system property») spells out advantages of this approach with even more detail and lists «correctness», «performance», and «ease of use and cross-platform development» as high-level advantages. Another duplicate issue, JDK-6529790 («Please add LINE_SEPARATOR constant into System or some other class»), makes the point that there should be a «constant» added to «some standard Java class, as String or System,» in a manner similar to that provided for file separators by File.pathSeparator.

One of the messages associated with the JDK 7 introduction of System.lineSeparator() justifies it additions with this description:

Lots of classes need to use System.getProperty(«line.separator») . Many don’t do it right because you need to use a doPrivileged block whenever you read a system property. Yet it is no secret — you can divine the line separator even if you have no trust with the security manager.

An interesting side note related to the addition of System.lineSeparator() in JDK 7 is that the Javadoc at that time did not indicate that the method was new to JDK 7. JDK-7082231 («Put a @since 1.7 on System.lineSeparator») addressed this in JDK 8 and two other JDK issues (JDK-8011796 and JDK-7094275) indicate that this was desired by multiple Java developers.

Читайте также:  Python перемножить все элементы массива

The introduction of System.lineSeparator() was a very small enhancement, but it does improve the safety and readability of a relatively commonly used API while not reducing (and, in fact, while improving) performance.

Published at DZone with permission of Dustin Marx , DZone MVB . See the original article here.

Opinions expressed by DZone contributors are their own.

Источник

Line Separator in Java

Strings have no newlines. We can form them into two lines by concatenating a newline string. Use System lineSeparator to get a platform-dependent newline string.

The following is an example.

Example

Output

Let us see another example. On Linux based system, the program will work correctly.

Example

Output

karthikeya Boyini

I love programming (: That’s all I know

  • Related Articles
  • Display path separator in Java
  • Display numbers with thousands separator in Java
  • How to add separator in a ToolBar with Java?
  • Get the substring after the first occurrence of a separator in Java
  • Get the index of the first occurrence of a separator in Java
  • Get the substring before the last occurrence of a separator in Java
  • New line separator doesn’t work for group_concat function in MySQL? How to use it correctly?
  • Compare Two Different Files Line by Line in Java
  • Command line arguments in Java
  • How can we import the text file, having data on the same line with a separator, into MySQL table?
  • How to place a thousand separator in MySQL records?
  • How to set comma as decimal separator in R?
  • How to style the separator using CSS in JavaFX?
  • Program to find number with thousand separator in Python
  • Splitting the string after the specified separator in Golang
Читайте также:  Php функция поиск массиве

Источник

Оцените статью