请更正程序。
Q. Write a java prg to accept RollNo, Name of a Student and store it in Student.txt.Accept n records and store it.
以下是o/p产生的缺点 1. 当我们将 rno 即 int {roll no of Stud} 写入文件“Student.txt”时,它会执行 不写int值
我/p
C:\j2sdk1.4.1_01\bin>java StudentInfo
Enter how many students
3
Student 1
Enter Roll no.:
1
Enter Name :
Geeta
Student 2
Enter Roll no.:
2
Enter Name :
Pinky
Student 3
Enter Roll no.:
3
Enter Name :
Shaily
写入文件“student.txt”的o/p是 吉塔·萍琪·谢莉
2) 当我们打印 Student.txt 中的值时,学生卷号会正确显示 使用的循环是
while((b=fin.read())!=-1)
{
System.out.print(b);
}
但如char c=(char) b;
未完成,不会打印名称
C:\j2sdk1.4.1_01\bin>java StudentInfo
Enter how many students
3
Student 1
Enter Roll no.:
1
Enter Name :
Geeta
Geeta
Student 2
Enter Roll no.:
2
Enter Name :
Pinky
Pinky
Student 3
Enter Roll no.:
3
Enter Name :
Shaily
Shaily
Contents of "Student.txt" are :
1711011011169728010511010712138310497105108121
C:\j2sdk1.4.1_01\bin>
or
反之亦然
while((b=fin.read())!=-1)
{
char ch=(char) b;
System.out.print(ch);
}
正确打印名称,但不滚动
以下是代码。请进行必要的更改。
import java.io.*;
class StudentInfo
{
int rno;
String name;
StudentInfo()
{
rno=0;
name="";
}
void getDetails() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter Roll no.: ");
rno=Integer.parseInt(br.readLine());
System.out.println("Enter Name : ");
name=br.readLine();
}
public static void main(String[] args) throws IOException
{
FileOutputStream fout=null;
FileInputStream fin;
int n;
char space=' ';
try
{
//open Student.txt
try
{
fout=new FileOutputStream("Student.txt");
}
catch(FileNotFoundException e)
{
System.out.println("Error opening the file Student.txt");
System.exit(0);
}
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter how many students");
n=Integer.parseInt(br.readLine());
StudentInfo s[]=new StudentInfo[n];
for(int i=0;i<n;i++)
{
s[i]=new StudentInfo();
System.out.println("Student "+(i+1));
s[i].getDetails();
int rollno=s[i].rno;
fout.write(rollno);
char c[]=s[i].name.toCharArray();
for(int z=0;z<c.length;z++)
fout.write(c[z]);
}
fout.close();
System.out.println("Contents of \"Student.txt\" are : ");
fin=new FileInputStream("Student.txt");
int b;
while((b=fin.read())!=-1)
{
System.out.print(b);
}
fin.close();
}
catch(Exception e)
{
System.out.println(e.getMessage());
}
}
}
我想如果我们能够区分卷号和名称,我们就可以解决这个问题。 在 o/p 文件中,我希望 o/p 为 rno 名称 rno 名称 rno 名称 IE。 1 吉塔 2 Pinky 3 谢莉
那么,如何插入呢?
如果我声明为
char ch=' ';
fout.write(ch);
它写的是 32
即使
char ch=' ';
fout.write((int)ch);
它将 32 写入 Student.txt
最佳答案
不要直接使用OutputStream
来写入文本数据。使用某种形式的 Writer
,这样您就可以写出字符串。
绝对最简单的方法是打开PrintWriter
,但这会掩盖错误。下一个级别是使用 FileWriter
,但它将始终使用系统默认编码。它可能足以满足您的目的。
将数字转换为文本通常是一个依赖于文化的过程,但为了简单起见,我暂时会忽略它。因此,使用包装在 BufferedWriter
中的 FileWriter
(以便更轻松地编写新行):
BufferedWriter writer = new BufferedWriter(new FileWriter("Student.txt"));
try
{
for(int i=0;i<n;i++)
{
s[i]=new StudentInfo();
System.out.println("Student "+(i+1));
s[i].getDetails();
writer.write(s[i].rno + ": " + s[i].name);
writer.newline();
}
}
finally
{
writer.close(); // Close output even if there's an error
}
代码的其他各个方面并不理想,但这至少应该对文件格式有所帮助......
关于java - 不写int值。请更正程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1397829/