java如何实现对同一个text文本边写边读

java如何实现对同一个text文本边写边读,第1张

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("输入学生的人数:")

int Studentpeople=s.nextInt()

String []StudentScore=new String[Studentpeople]

String []StudentName=new String[Studentpeople]

System.out.println("请输入学生的侍瞎姓名和总分")

for(int i=0i<Studentpeoplei++)

{

StudentName[i]=s.next()

StudentScore[i]=s.next()

System.out.println("\n")

}

int []IntStudentScore=new int[Studentpeople]

for(int n=0n<Studentpeoplen++)

{

IntStudentScore[n]=Integer.parseInt(StudentScore[n])

}

int StudentScoreMin=IntStudentScore[0]

int StudentScoreMax=IntStudentScore[0]

for(int m=1m<Studentpeoplem++)

{

int AllScore=IntStudentScore[0]

AllScore+=IntStudentScore[m]

//求最低分

if(StudentScoreMin>IntStudentScore[m])

StudentScoreMin=IntStudentScore[m]

else

StudentScoreMin=StudentScoreMin

}

//求最高分

for(int p=0p<慧贺Studentpeoplep++)

{

if(StudentScoreMax<IntStudentScore[p])

StudentScoreMax=IntStudentScore[p]

else

StudentScoreMax=StudentScoreMax

}

try{

Swriter=new FileWriter("Student.txt")

Swriter.write("学生成绩表单\n")

Swriter.write("姓名前谈派:\t\t总分:\n")

for(int j=0j<Studentpeoplej++)

{

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){}

}

}

}

你可以参考着个程序 我自己写的

读取文件有三种方式,r, w, a,读,覆盖写, 追加写, new FileReader(file)以读的方式打开了文件,两个马上咐颂拆就以new FileWriter(file)方式衡枣覆盖写文件,文件自然是空白的,之后你readLine读到的樱首是null,即String line是null,然后你要writer.write(line) 即writer.write(null)自然报空指针;

建议,如果边读边写,考虑RandomAccessFile类


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/12265646.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-24
下一篇 2023-05-24

发表评论

登录后才能评论

评论列表(0条)

保存