讀取文件有三種方式,r, w, a,讀,覆蓋寫, 追加寫, new FileReader(file)以讀的方式打開了文件,兩個馬上就以new FileWriter(file)方式覆蓋寫文件,文件自然是空白的,之后你readLine讀到的是null,即String line是null,然后你要writer.write(line); 即writer.write(null); 自然報空指針;
創(chuàng)新互聯(lián)公司服務(wù)項目包括烈山網(wǎng)站建設(shè)、烈山網(wǎng)站制作、烈山網(wǎng)頁制作以及烈山網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,烈山網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到烈山省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
建議,如果邊讀邊寫,考慮RandomAccessFile類
import java.io.*;
import java.lang.*;
import java.util.Scanner;
import java.io.*;
import java.lang.*;
public class Sy2Student
{
public static void main(String []args)
{
Writer Swriter=null;
Scanner s=new Scanner(System.in);
System.out.print("輸入學(xué)生的人數(shù):");
int Studentpeople=s.nextInt();
String []StudentScore=new String[Studentpeople];
String []StudentName=new String[Studentpeople];
System.out.println("請輸入學(xué)生的姓名和總分");
for(int i=0;iStudentpeople;i++)
{
StudentName[i]=s.next();
StudentScore[i]=s.next();
System.out.println("\n");
}
int []IntStudentScore=new int[Studentpeople];
for(int n=0;nStudentpeople;n++)
{
IntStudentScore[n]=Integer.parseInt(StudentScore[n]);
}
int StudentScoreMin=IntStudentScore[0];
int StudentScoreMax=IntStudentScore[0];
for(int m=1;mStudentpeople;m++)
{
int AllScore=IntStudentScore[0];
AllScore+=IntStudentScore[m];
//求最低分
if(StudentScoreMinIntStudentScore[m])
StudentScoreMin=IntStudentScore[m];
else
StudentScoreMin=StudentScoreMin;
}
//求最高分
for(int p=0;pStudentpeople;p++)
{
if(StudentScoreMaxIntStudentScore[p])
StudentScoreMax=IntStudentScore[p];
else
StudentScoreMax=StudentScoreMax;
}
try{
Swriter=new FileWriter("Student.txt");
Swriter.write("學(xué)生成績表單\n");
Swriter.write("姓名:\t\t總分:\n");
for(int j=0;jStudentpeople;j++)
{
Swriter.write(StudentName[j]+"\t\t");
Swriter.write(StudentScore[j]+"\n");
}
Swriter.write("最高分:"+StudentScoreMax+"\n");
Swriter.write("最低分:"+StudentScoreMin+"\n");
}catch(IOException e){
e.printStackTrace();
}finally{
if(Swriter!=null)
try{
Swriter.close();
}catch(IOException e){}
}
Reader RStudent=null;
try{
RStudent=new FileReader("Student.txt");
char []buffer=new char[1024];
int offset;
while((offset=RStudent.read(buffer))0)
System.out.println(new String(buffer,0,offset));
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
if(RStudent!=null)
try{
RStudent.close();
}catch(IOException e){}
}
}
}
你可以參考著個程序 我自己寫的
另外寫一個文件,把有用的、需要改動的信息寫入,最后刪除原文件。
本文標(biāo)題:java邊讀邊寫代碼 java寫代碼技巧
文章轉(zhuǎn)載:http://muchs.cn/article36/dosphpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供關(guān)鍵詞優(yōu)化、建站公司、服務(wù)器托管、定制開發(fā)、電子商務(wù)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容