在D盤(pán)新建兩個(gè)文件test.txt,test1.txt
為蘇尼特右等地區(qū)用戶提供了全套網(wǎng)頁(yè)設(shè)計(jì)制作服務(wù),及蘇尼特右網(wǎng)站建設(shè)行業(yè)解決方案。主營(yíng)業(yè)務(wù)為成都做網(wǎng)站、成都網(wǎng)站設(shè)計(jì)、蘇尼特右網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠(chéng)的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會(huì)得到認(rèn)可,從而選擇與我們長(zhǎng)期合作。這樣,我們也可以走得更遠(yuǎn)!
把內(nèi)容拷到test中,test1為輸出。。。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
public class ZhiDao {
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedReader br = null;
String lineContent = null;
StringBuffer sb = new StringBuffer();
PrintWriter pw = null;
try {
br = new BufferedReader(new FileReader("D:\\test.txt"));
pw = new PrintWriter("D:\\test1.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
while ((lineContent = br.readLine()) != null) {
if(lineContent.equals("")){
sb.append("\r\n");
sb.append("\r\n");
}else{
for(int i = 0; ilineContent.length(); i++){
char charContent = lineContent.charAt(i);
sb.append(charContent);
if(i != 0 i%60 == 0){
sb.append("\r\n");
}
}
}
}
System.out.println(sb);
pw.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
br.close();
pw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
import?java.io.BufferedWriter;
import?java.io.File;
import?java.io.FileWriter;
import?java.io.IOException;
import?java.util.Scanner;
public?class?OutToTxt?{
private?static?BufferedWriter?writer;
private?static?Scanner?sc;
public?static?void?main(String[]?args)?{
File?out?=?new?File("./Out.txt");
if(!out.exists())?{
try?{
out.createNewFile();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
try?{
writer?=?new?BufferedWriter(new?FileWriter(out));
}?catch?(IOException?e)?{
e.printStackTrace();
}
sc?=?new?Scanner(System.in);
System.out.println("請(qǐng)輸入文本內(nèi)容,輸入exit結(jié)束:");
try?{
writer.write("");?//?清空文本
String?split?=?"";
while(true)?{
String?line?=?sc.nextLine();
if(line.equalsIgnoreCase("exit"))?{
break;
}
writer.append(split?+?line);
split?=?"\r\n";
}
}?catch?(IOException?e1)?{
e1.printStackTrace();
}?finally?{
if(null?!=?writer)?{
try?{
writer.flush();
writer.close();
}?catch?(IOException?e)?{
e.printStackTrace();
}
}
}
}
}
你可以運(yùn)行后輸入
Hello World!
This is my first application.
exit
寫(xiě)文件
import java.io.FileNotFoundException;
import java.io.FileOutputStream;//寫(xiě)文件
public class WriteFile {
public static void main(String[] args) throws Exception {
String temp="Hello world!\n";
FileOutputStream fos=new
FileOutputStream("D:\\GUI\\write.txt",true);//兩個(gè)參數(shù),true表示在文件末尾追加
fos.write(temp.getBytes());
fos.close();//流要及時(shí)關(guān)閉
}
}
讀文件
import java.io.FileInputStream;//讀文件
public class ReadFile {
public static void main(String[] args) throws Exception {
FileInputStream fis=new FileInputStream("D:\\GUI\\test.txt");
byte[] b=new byte[1024];
String result="";
while(true){
int num=fis.read(b);//num返回實(shí)際讀到的字節(jié)數(shù),如果文件讀完了返回-1;
if(num==-1)break;
result=result+new String(b,0,num);
}
fis.close();
System.out.println(result);
}
}
輸入無(wú)需使用字節(jié)流,直接字符流讀取即可。
private?void?input(String?fileName)?throws?IOException?{
try(BufferedReader?reader?=?new?BufferedReader(new?FileReader(fileName)))?{
String?line;
while((line=reader.readLine())?!=?null)?{
System.out.println(line);
}???
}
}
同樣輸出,只要把Input換成Output;
private?void?output(String?fileName,?String?content)?throws?IOException{
try(BufferedWriter??writer?=?new?BufferedWriter(new?FileWriter(fileName)))?{
writer.write(content);
writer.flush();
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
public class TestBaiduKnow {
public static void main(String[] args) throws IOException {
FileOutputStream fs = new FileOutputStream(new File("D:\\text.txt"));
PrintStream p = new PrintStream(fs);
p.println(100);
p.close();
}
}
//簡(jiǎn)單的一個(gè)例子,來(lái)模擬輸出
網(wǎng)頁(yè)題目:關(guān)于java導(dǎo)出txt代碼的信息
網(wǎng)頁(yè)鏈接:http://jinyejixie.com/article0/hsojoo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供做網(wǎng)站、全網(wǎng)營(yíng)銷推廣、、網(wǎng)站導(dǎo)航、企業(yè)網(wǎng)站制作、網(wǎng)站營(yíng)銷
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)