java中實(shí)現(xiàn)換行有以下幾種方法:
創(chuàng)新互聯(lián)公司專注于竹山企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站制作。竹山網(wǎng)站建設(shè)公司,為竹山等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計(jì),全程項(xiàng)目跟蹤,創(chuàng)新互聯(lián)公司專業(yè)和態(tài)度為您提供的服務(wù)
1.使用java中的轉(zhuǎn)義符"\r\n":
String str="aaa";
str+="\r\n";
這樣在str后面就有換行了.
注意:\r,\n的順序是不能夠?qū)Q的,否則不能實(shí)現(xiàn)換行的效果.
2.BufferedWriter的newline()方法:
FileOutputStream fos=new FileOutputStream("c;\\11.txt");
BufferedWriter bw=new BufferedWriter(fos);
bw.write("你好");
bw.newline();
bw.write("java");
w.newline();
3.使用System.getProperty()方法:
String str = "aaa"+System.getProperty("line.separator");
附:針對(duì)常用的系統(tǒng),可以使用如下的轉(zhuǎn)義符實(shí)現(xiàn)換行:
windows下的文本文件換行符:\r\n
linux/unix下的文本文件換行符:\r
Mac下的文本文件換行符:\n
可以使用Java中\(zhòng)n和\r的換行,不過也是有區(qū)別的,如下:
1.\r 叫回車 Carriage Return
2.\n 叫新行 New Line
但是都會(huì)造成換行,使用System.getProperty("line.separator")來獲取當(dāng)前OS的換行符
java 代碼
1. String userInputString = userInput;
2. userInputString = userInputString.replaceAll ( "\r", "" );
3. userInputString = userInputString.replaceAll ( "\n", "\\\\"+System.getPropert("line.separator"));
第一種:使用System.out.println()//這是換一行。
第二種:使用System.out.print("\n");//這也是換一行.
第一種和第二種差不多。只是,如果你要換兩行,三行,多行的話。
就用:System.out.println("\n\n");換兩行
換三行:System.out.println("\n\n"\n);
樓主.java中的換行是\n,而windows中的換行符是\r\n.因?yàn)閈n在windows中為不可識(shí)別字符.所以用
■
來顯示的.如果你需要在java正確讀取,那就別忘了替換啊.
//這帖子放了很久了,怎么還能在提問區(qū)看到啊...給你寫個(gè)完整的吧!
import?java.io.BufferedReader;
import?java.io.File;
import?java.io.FileInputStream;
import?java.io.FileNotFoundException;
import?java.io.IOException;
import?java.io.InputStreamReader;
import?java.io.UnsupportedEncodingException;
public?class?Day01_ReadTxt?{
public?static?void?main(String[]?args)?{
File?file=new?File("K:\\Test\\TestTxt.txt");//路徑
if(file.canExecute())//如果存在就繼續(xù);
init(file);
}
private?static?void?init(File?file)?{
System.gc();
BufferedReader?br=null;
try?{
br=new?BufferedReader(new?InputStreamReader(new?FileInputStream(file),"GBK"));
for(String?str=br.readLine();str!=null;str=br.readLine())?{
str=str.replaceAll("[{}]+",?"\r\n");//正則替換;
System.out.print(str);//輸出控制臺(tái)
}
}?catch?(UnsupportedEncodingException?e)?{
e.printStackTrace();
}?catch?(FileNotFoundException?e)?{
e.printStackTrace();
}?catch?(IOException?e)?{
e.printStackTrace();
}finally?{
if(br!=null)?{
try?{
br.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
}
標(biāo)題名稱:java中換行的代碼是 java語言中換行的代碼是什么
URL地址:http://m.kartarina.com/article0/hgjdio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供域名注冊(cè)、App開發(fā)、靜態(tài)網(wǎng)站、服務(wù)器托管、網(wǎng)站改版、標(biāo)簽優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)