Java 为什么不是';我的代码不能创建新文件吗?
这是我刚刚写的一节课。以前它实际上在另一个应用程序中创建了一个文件。但不知何故,它不起作用。它没有创建新文件,我遇到以下错误:Java 为什么不是';我的代码不能创建新文件吗?,java,file,objectinputstream,Java,File,Objectinputstream,这是我刚刚写的一节课。以前它实际上在另一个应用程序中创建了一个文件。但不知何故,它不起作用。它没有创建新文件,我遇到以下错误: package hostelmanagement; /** * * @author princess */ /* * To change this template, choose Tools | Templates * and open the template in the editor. */ import java.io.Externalizab
package hostelmanagement;
/**
*
* @author princess
*/
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.Externalizable;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.swing.JOptionPane;
/**
*
* @author princess
*/
public class Student implements Serializable, Externalizable {
public static Set<Student> listOfStudents = new HashSet<Student>();
public static File oFile = new File("Student.dat");
//Data Members
private String studentID;
private String name;
private Date dateOfReg;
//Constructor
Student(String number,String name)
{
this.studentID = number;
this.name = name;
dateOfReg = new Date();
}
public String getName()
{
return name;
}
public String getStudentID()
{
return studentID;
}
public Date getDateOfReg()
{
return dateOfReg;
}
public void register() throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet<Student> sss = Student.getListOfStudents();
sss.add(this);
FileOutputStream OFileStream = new FileOutputStream(oFile);
ObjectOutputStream ObjectOFileStream = new ObjectOutputStream(OFileStream);
ObjectOFileStream.writeObject(sss);
ObjectOFileStream.close();
}
public static HashSet<Student> getListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet ss;
File iFile = new File("Student.dat");
FileInputStream IFileStream = new FileInputStream(iFile);
ObjectInputStream ObjectIFileStream = new ObjectInputStream(IFileStream);
ss = (HashSet<Student>) ObjectIFileStream.readObject();
ObjectIFileStream.close();
return (HashSet<Student>) ss;
}
public static void printListOfStudents() throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet<Student> sa = Student.getListOfStudents();
for (Student x : sa)
{System.out.println(x.toString());}
}
public static Student getStudentByID(String aNumber) throws FileNotFoundException, IOException, ClassNotFoundException
{
HashSet<Student> currentListOfStudents = Student.getListOfStudents();
Student result = null;
for (Student x : currentListOfStudents)
{
if (x.getStudentID().equalsIgnoreCase(aNumber))
{ result = x;
break;
}
}
if (result == null)
{
JOptionPane.showMessageDialog(null, "Student not found");
}
return result;
}
@Override
public String toString()
{
// include the code to retrieve assigned apartment
return "Name: " + name +" StudentID: "+ studentID + " Registered On: " + dateOfReg;
}
@Override
public boolean equals(Object another)
{
Student stud = (Student)another;
return this.name.equals(stud.name)&& this.studentID.equals(stud.studentID);
}
@Override
public int hashCode()
{
int hash = name.hashCode();
return hash;
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
package酒店管理;
/**
*
*@作者公主
*/
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
导入java.io.Externalizable;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.io.ObjectInput;
导入java.io.ObjectInputStream;
导入java.io.ObjectOutput;
导入java.io.ObjectOutputStream;
导入java.io.Serializable;
导入java.util.Date;
导入java.util.HashSet;
导入java.util.Set;
导入javax.swing.JOptionPane;
/**
*
*@作者公主
*/
公共类学生实现可序列化、可外部化{
public static Set listOfStudents=new HashSet();
公共静态文件oFile=新文件(“Student.dat”);
//数据成员
私人字符串学生ID;
私有字符串名称;
私人日期;
//建造师
学生(字符串编号、字符串名称)
{
this.studentID=数字;
this.name=名称;
dateOfReg=新日期();
}
公共字符串getName()
{
返回名称;
}
公共字符串getStudentID()
{
返回学生ID;
}
公共日期getDateOfReg()
{
REG的返回日期;
}
public void register()抛出FileNotFoundException、IOException、ClassNotFoundException
{
HashSet sss=Student.getListOfStudents();
补充(本);
FileOutputStreamofileStream=新的FileOutputStream(oFile);
ObjectOutputStream ObjectOFileStream=新的ObjectOutputStream(OFileStream);
ObjectOFileStream.writeObject(sss);
ObjectOFileStream.close();
}
public static HashSet getListOfStudents()抛出FileNotFoundException、IOException、ClassNotFoundException
{
哈希集ss;
文件iFile=新文件(“Student.dat”);
FileInputStream IFileStream=新的FileInputStream(iFile);
ObjectInputStream ObjectFileStream=新ObjectInputStream(IFileStream);
ss=(HashSet)objectfilestream.readObject();
objectfilestream.close();
返回(HashSet)ss;
}
public static void printListOfStudents()抛出FileNotFoundException、IOException、ClassNotFoundException
{
HashSet sa=Student.getListOfStudents();
学生(x:sa)
{System.out.println(x.toString());}
}
公共静态学生getStudentByID(字符串aNumber)抛出FileNotFoundException、IOException、ClassNotFoundException
{
HashSet currentListOfStudents=Student.getListOfStudents();
学生成绩=空;
对于(学生x:CurrentList of Student)
{
if(x.getStudentID().equalsIgnoreCase(一个数))
{result=x;
打破
}
}
如果(结果==null)
{
showMessageDialog(null,“未找到学生”);
}
返回结果;
}
@凌驾
公共字符串toString()
{
//包括检索指定公寓的代码
return“Name:+Name+”StudentID:“+StudentID+”注册日期:”+dateOfReg;
}
@凌驾
公共布尔等于(另一个对象)
{
学生钉=(学生)另一个;
返回this.name.equals(stud.name)和&this.studentID.equals(stud.studentID);
}
@凌驾
公共int hashCode()
{
int hash=name.hashCode();
返回散列;
}
@凌驾
public void writeExternal(ObjectOutput out)引发IOException{
抛出新的UnsupportedOperationException(“尚未支持”);
}
@凌驾
public void readExternal(ObjectInput in)引发IOException、ClassNotFoundException{
抛出新的UnsupportedOperationException(“尚未支持”);
}
}
错误:
run:
Test 1
Apr 27, 2012 10:19:30 AM hostelmanagement.HostelManagement main
SEVERE: null
java.io.FileNotFoundException: Student.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at hostelmanagement.Student.getListOfStudents(Student.java:86)
at hostelmanagement.Student.register(Student.java:74)
at hostelmanagement.HostelManagement.main(HostelManagement.java:34)
Exception in thread "main" java.io.FileNotFoundException: Student.dat (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at hostelmanagement.Student.getListOfStudents(Student.java:86)
at hostelmanagement.HostelManagement.main(HostelManagement.java:46)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)
运行:
测试1
2012年4月27日上午10:19:30招待所管理。招待所管理主楼
严重:空
java.io.FileNotFoundException:Student.dat(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:106)
在hostelmanagement.Student.getListOfStudents(Student.java:86)
在hostelmanagement.Student.register(Student.java:74)
在hostelmanagement.hostelmanagement.main(hostelmanagement.java:34)
线程“main”java.io.FileNotFoundException:Student.dat中出现异常(系统找不到指定的文件)
在java.io.FileInputStream.open(本机方法)
位于java.io.FileInputStream。(FileInputStream.java:106)
在hostelmanagement.Student.getListOfStudents(Student.java:86)
在hostelmanagement.hostelmanagement.main(hostelmanagement.java:46)
Java结果:1
生成成功(总时间:0秒)
问题出在哪里?鉴于您没有提供主逻辑,我假设您在调用
register()
(写入文件)之前调用了getlistsudents()
(读取文件)。毫不奇怪,它没有找到它
问题是您的程序试图读取一个仍然不存在的文件,而不是无法创建它。请仔细阅读stacktraces。鉴于您没有提供主逻辑,我假设您在调用
register()
(写入文件)之前正在调用getListStudents()
(读取文件)。毫不奇怪,它没有找到它
问题是您的程序试图读取一个仍然不存在的文件,而不是无法创建它。请仔细阅读stacktraces。问题可能是您在
getListOfStudents()
和register()
方法中加载的文件可能不在指定位置 问题可能是您在getListOfStudents()
和register()
方法中加载的文件可能不在指定位置 您的错误表示您正试图读取不存在的文件。它只会在您尝试写入时创建新文件
我要做的是以下几点
public static Set<Student> getListOfStudents() throws IOException, ClassNotFoundException {
File studentFile = new File("Student.dat");
FileInputStream in = null;
try {
in = new FileInputStream(studentFile);
ObjectInputStream oos = new ObjectInputStream(in);
retyurn (Set<Student>) oos.readObject();
} catch(FileNotFoundException noStudents) {
return new HashSet<Student>();
} finally {
if (in != null)
try {
in.close();
} catch(IOException ignored) {}
}
}
public static Set getListOfStudents()抛出IOException、ClassNotFoundException{
文件studentFile