怎么樣得到平臺相關的換行符?

問題:怎么樣得到平臺相關的換行符?

Java里面怎么樣得到平臺相關的換行符。我不可能到處都用"\n"

回答一

In addition to the line.separator property, if you are using java 1.5 or later and the String.format (or other formatting methods) you can use %n as in

除了line.separator這個屬性,如果你使用的是1.5或者后面的版本的話,你可以在String.format(或者其他格式化的方法)中使用%n

Calendar c = ...;
String s = String.format("Duke's Birthday: %1$tm %1$te,%1$tY%n", c); 
//Note `%n` at end of line                                  ^^String s2 = String.format("Use %%n as a platform independent newline.%n"); 
//         %% becomes %        ^^
//                                        and `%n` becomes newline   ^^

如果想知道得更多,那就去看看 Java 1.8 API關于Formatter的內容吧

回答二

Java 7 有一個 System.lineSeparator() 的方法

回答三

你可以使用

System.getProperty("line.separator");

去獲取行分隔符

回答四

StringBuilder newLine=new StringBuilder();
newLine.append("abc");
newline.append(System.getProperty("line.separator"));
newline.append("def");
String output=newline.toString();

上面的代碼會有兩個字符串,被一個平臺無關的換行分隔開

回答五

這是可行的:

String.format("%n").OrString.format("%n").intern()

去保存一些字符。

回答六

如果你嘗試在文件中寫入一個新行,你可以使用 BufferedWriter的newLine() 方法.

回答七

在commons-lang包下有一個常量域叫SystemUtils.LINE_SEPARATOR

回答八

如果你嘗試寫入一個文件,使用BufferedWriter實例,使用這個實例的newLine()方法。它提供一個平臺無關的方法去在文件中寫入一個新行。

文章翻譯自Stack Overflow:https://stackoverflow.com/questions/207947/how-do-i-get-a-platform-dependent-new-line-character

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/390980.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/390980.shtml
英文地址,請注明出處:http://en.pswp.cn/news/390980.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

scrapy常用工具備忘

scrapy常用的命令分為全局和項目兩種命令,全局命令就是不需要依靠scrapy項目,可以在全局環境下運行,而項目命令需要在scrapy項目里才能運行。一、全局命令##使用scrapy -h可以看到常用的全局命令 [rootaliyun ~]# scrapy -h Scrapy 1.5.0 - n…

機器學習模型 非線性模型_機器學習:通過預測菲亞特500的價格來觀察線性模型的工作原理...

機器學習模型 非線性模型Introduction介紹 In this article, I’d like to speak about linear models by introducing you to a real project that I made. The project that you can find in my Github consists of predicting the prices of fiat 500.在本文中,…

NOIP賽前模擬20171027總結

題目: 1.壽司 給定一個環形的RB串要求經過兩兩互換后RB分別形成兩段連續區域,求最少操作次數(算法時間O(n)) 2.金字塔 給定一個金字塔的側面圖有n層已知每一層的寬度高度均為1要求在圖中取出恰好K個互不相交的矩形(邊緣可以重疊),求最多可以取…

虛幻引擎 js開發游戲_通過編碼3游戲學習虛幻引擎4-5小時免費游戲開發視頻課程

虛幻引擎 js開發游戲One of the most widely used game engines is Unreal Engine by Epic Games. On the freeCodeCamp.org YouTube channel, weve published a comprehensive course on how to use Unreal Engine with C to develop games.Epic Games的虛幻引擎是使用最廣泛的…

建造者模式什么時候使用?

問題:建造者模式什么時候使用? 建造者模式在現實世界里面的使用例子是什么?它有啥用呢?為啥不直接用工廠模式 回答一 下面是使用這個模式的一些理由和Java的樣例代碼,但是它是由設計模式的4個人討論出來的建造者模式…

TP5_學習

2017.10.27:1.index入口跑到public下面去了 2.不能使用 define(BIND_MODULE,Admin);自動生成模塊了,網上查了下: \think\Build::module(Admin);//親測,可用 2017.10.28:1.一直不知道怎么做查詢顯示和全部顯示,原來如此簡單&#x…

sql sum語句_SQL Sum語句示例說明

sql sum語句SQL中的Sum語句是什么? (What is the Sum statement in SQL?) This is one of the aggregate functions (as is count, average, max, min, etc.). They are used in a GROUP BY clause as it aggregates data presented by the SELECT FROM WHERE port…

10款中小企業必備的開源免費安全工具

10款中小企業必備的開源免費安全工具 secist2017-05-188共527453人圍觀 ,發現 7 個不明物體企業安全工具很多企業特別是一些中小型企業在日常生產中,時常會因為時間、預算、人員配比等問題,而大大減少或降低在安全方面的投入。這時候&#xf…

為什么Java里面沒有 SortedList

問題:為什么Java里面沒有 SortedList Java 里面有SortedSet和SortedMap接口,它們都屬于Java的集合框架和提供對元素進行排序的方法 然鵝,在我的認知里Java就沒有SortedList這個東西。你只能使用java.util.Collections.sort()去排序一個list…

圖片主成分分析后的可視化_主成分分析-可視化

圖片主成分分析后的可視化If you have ever taken an online course on Machine Learning, you must have come across Principal Component Analysis for dimensionality reduction, or in simple terms, for compression of data. Guess what, I had taken such courses too …

回溯算法和遞歸算法_回溯算法:遞歸和搜索示例說明

回溯算法和遞歸算法Examples where backtracking can be used to solve puzzles or problems include:回溯可用于解決難題或問題的示例包括: Puzzles such as eight queens puzzle, crosswords, verbal arithmetic, Sudoku [nb 1], and Peg Solitaire. 諸如八個皇后…

C#中的equals()和==

using System;namespace EqualsTest {class EqualsTest{static void Main(string[] args){//值類型int x 1;int y 1;Console.WriteLine(x y);//TrueConsole.WriteLine(x.Equals(y));//True //引用類型A a new A();B b new B();//Console.WriteLine(ab);//報錯…

JPA JoinColumn vs mappedBy

問題&#xff1a;JPA JoinColumn vs mappedBy 兩者的區別是什么呢 Entity public class Company {OneToMany(cascade CascadeType.ALL , fetch FetchType.LAZY)JoinColumn(name "companyIdRef", referencedColumnName "companyId")private List<B…

TP引用樣式表和js文件及驗證碼

TP引用樣式表和js文件及驗證碼 引入樣式表和js文件 <script src"__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> <script src"__PUBLIC__/bootstrap/js/bootstrap.min.js"></script> <link href"__PUBLIC__/bo…

pytorch深度學習_深度學習和PyTorch的推薦系統實施

pytorch深度學習The recommendation is a simple algorithm that works on the principle of data filtering. The algorithm finds a pattern between two users and recommends or provides additional relevant information to a user in choosing a product or services.該…

什么是JavaScript中的回調函數?

This article gives a brief introduction to the concept and usage of callback functions in the JavaScript programming language.本文簡要介紹了JavaScript編程語言中的回調函數的概念和用法。 函數就是對象 (Functions are Objects) The first thing we need to know i…

Java 集合-集合介紹

2017-10-30 00:01:09 一、Java集合的類關系圖 二、集合類的概述 集合類出現的原因&#xff1a;面向對象語言對事物的體現都是以對象的形式&#xff0c;所以為了方便對多個對象的操作&#xff0c;Java就提供了集合類。數組和集合類同是容器&#xff0c;有什么不同&#xff1a;數…

為什么Java不允許super.super.method();

問題&#xff1a;為什么Java不允許super.super.method(); 我想出了這個問題&#xff0c;認為這個是很好解決的&#xff08;也不是沒有它就不行的&#xff09;如果可以像下面那樣寫的話&#xff1a; Override public String toString() {return super.super.toString(); }我不…

Exchange 2016部署實施案例篇-04.Ex基礎配置篇(下)

上二篇我們對全新部署完成的Exchange Server做了基礎的一些配置&#xff0c;今天繼續基礎配置這個話題。 DAG配置 先決條件 首先在配置DGA之前我們需要確保DAG成員服務器上磁盤的盤符都是一樣的&#xff0c;大小建議最好也相同。 其次我們需要確保有一塊網卡用于數據復制使用&…

數據庫課程設計結論_結論:

數據庫課程設計結論In this article, we will learn about different types[Z Test and t Test] of commonly used Hypothesis Testing.在本文中&#xff0c;我們將學習常用假設檢驗的不同類型[ Z檢驗和t檢驗 ]。 假設是什么&#xff1f; (What is Hypothesis?) This is a St…