什么时候在java中使用flush()?
import java.io. * ;
public class Ser {
public static void main(String args[]) {
try {
John myObj = new John("Sachin","Cricket");
System.out.println(myObj);
FileOutputStream fos = new FileOutputStream("FileName");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(myObj);
oos.flush();
oos.close();
} catch (Exception e) {
System.out.println("Expection" + e);
System.exit(0);
}
try {
John myObj2;
FileInputStream fis = new FileInputStream("FileName");
ObjectInputStream ois = new ObjectInputStream(fis);
myObj2 = (John) ois.readObject();
ois.close();
System.out.println("New Object" + myObj2);
} catch (Exception e) {
System.out.println("Expection" + e);
System.exit(0);
}
}
}
class John implements Serializable {
private String name;
private String department;
public John(String name,String department) {
this.name = name;
this.department = department;
}
public String toString() {
return "Name" + name + " " + "Department" + department;
}
}
我在上面的例子中有几个问题. >何时使用冲洗方法,为什么要使用它? 解决方法
它会刷新仍然由OutputStream缓冲的任何内容.详细说明可在JavaDoc中找到.
我不确定你的意思是“带分”,但是close方法最终会关闭资源(输入或输出),它会释放,例如文件句柄.您应该始终在finally块中调用close来清理操作系统可能具有的所有引用. InputStream is = null;
try {
is = new FileInputStream(...);
// do stuff
} catch (IOException e) {
// do stuff
} finally {
if (is != null) {
is.close();
}
}
以某种方式更正,您之前反序列化写入文件的对象.这些文件是专有的,只能被Java理解,所以你最后一个问题是好的!
你有很多选择.对于客户端应用程序,您可能希望使用XML,JSON或轻量级数据库(如SQLlite).在服务器端,您也可以查看更强大的数据库(例如MySQL). (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
