Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/reporting-services/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 不写入int值。请更正程序_Java - Fatal编程技术网

Java 不写入int值。请更正程序

Java 不写入int值。请更正程序,java,Java,请更正程序 编写一个java prg来接受RollNo,一个学生的名字,并将其存储在 Student.txt.Accept n记录并存储它 以下是生产的o/p的缺点 1.当我们将rno,即int{roll no of stud}写入文件“Student.txt”时,它会这样做 不写入int值 i/p 写入文件“student.txt”的o/p为 GeetaPinkyShaily 2) 当我们打印Student.txt中的值时,学生卷号将正确显示 使用的循环是 while((b=fin.read(

请更正程序

编写一个java prg来接受RollNo,一个学生的名字,并将其存储在 Student.txt.Accept n记录并存储它

以下是生产的o/p的缺点 1.当我们将rno,即int{roll no of stud}写入文件“Student.txt”时,它会这样做 不写入int值

i/p

写入文件“student.txt”的o/p为 GeetaPinkyShaily

2) 当我们打印Student.txt中的值时,学生卷号将正确显示 使用的循环是

while((b=fin.read())!=-1)
{
 System.out.print(b);
}
但是as
charc=(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());
  }
 }
}
它写32

即使

char ch=' ';
fout.write((int)ch);

它有32到Student.txt,您可以在这里使用DataOutputStream。FileOutputStream只能将所有内容写入字节。请参考此


在这个例子中,您可以使用RollNumber和Name。如果您将每条记录写在文件内的单独一行中也会更好,这只是为了更好地理解:)

您可以在这里使用DataOutputStream。FileOutputStream只能将所有内容写入字节。请参考此


在这个例子中,您可以使用RollNumber和Name。如果您只是为了更好地理解而将每条记录写在文件内的单独一行中也会更好:)

不要直接使用
输出流来写入文本数据。使用某种形式的
编写器
,这样您就可以编写字符串了

最简单的方法是打开一个
PrintWriter
,但这会掩盖错误。下一个级别是使用
文件编写器
,但它将始终使用系统默认编码。对于你的目的来说,它可能已经足够好了

将数字转换为文本通常是一个文化相关的过程,但为了简单起见,我暂时还是忽略了这一点。因此,使用
FileWriter
包装在
BufferedWriter
中(以便于编写新行):

BufferedWriter=newbufferedwriter(newfilewriter(“Student.txt”);
尝试
{

对于(int i=0;i不要直接使用
OutputStream
来写入文本数据。请使用某种形式的
Writer
,这样您就可以将字符串写入

最简单的方法是打开一个
PrintWriter
,但这会掩盖错误。下一步是使用
FileWriter
,但这将始终使用系统默认编码。它可能足以满足您的需要

将数字转换为文本通常是一个依赖于区域性的过程,但为了简单起见,我暂时将忽略这一点。因此,使用
FileWriter
包装在
BufferedWriter
中(以便更容易地编写新行):

BufferedWriter=newbufferedwriter(newfilewriter(“Student.txt”);
尝试
{

对于(int i=0;i如果你要求社区纠正你的计划,你只会得到反对票。相反,突出你的具体问题。在这种情况下,这是为什么

char ch=' ';
fout.write(ch);
输出32.思考一下。
System.out.print
为什么是正确的,而不是
fout.write
?fout
System.out
之间有什么区别,如果有?如果有,
write
print
之间有什么区别?请查阅文档


祝你的家庭作业好运,至少你没有假装没有。如果你要求社区纠正你的程序,你只会得到反对票。相反,突出你的具体问题。在这种情况下,这是为什么

char ch=' ';
fout.write(ch);
输出32.思考一下。
System.out.print
为什么是正确的,而不是
fout.write
?fout
System.out
之间有什么区别,如果有?如果有,
write
print
之间有什么区别?请查阅文档

祝你的家庭作业好运,至少你没有假装不是

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
}
char ch=' ';
fout.write(ch);