黑马程序员技术交流社区
标题:
File输出流的问题
[打印本页]
作者:
王双宝
时间:
2012-7-13 17:11
标题:
File输出流的问题
public class Test {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args){
try {
File f=new File("abc.txt");
FileOutputStream fos=new FileOutputStream(f);
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
int i=0;
while(true){
try{
Point p=(Point)in.readObject();
System.out.println(p);
}
catch(EOFException e){
break;
}
Point p=(Point)in.readObject();
System.out.println(p);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Point implements Serializable{
private int x;
private int y;
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "x="+x+",y="+y;
}
}
当程序第一次运行时,abc.txt写入了两个对象。可再次运行,打印出的还是两个对象。怎么写才是添加到文件中的?
如:
第一次运行结果:
x=3,y=4
x=2,y=3
第二次运行结果:
x=3,y=4
x=2,y=3
x=3,y=4
x=2,y=3
作者:
游兴钟
时间:
2012-7-13 17:37
public class Test {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args){
try {
File f=new File("abc.txt");
FileOutputStream fos=new FileOutputStream(f,true);//改成这个就可以了
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
int i=0;
while(true){
try{
Point p=(Point)in.readObject();
System.out.println(p);
}
catch(EOFException e){
break;
}
Point p=(Point)in.readObject();
System.out.println(p);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Point implements Serializable{
private int x;
private int y;
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "x="+x+",y="+y;
}
}
作者:
黑马-王言龙
时间:
2012-7-13 17:37
本帖最后由 黑马-王言龙 于 2012-7-13 17:38 编辑
FileOutputStream fos=new FileOutputStream(f,
true
);
作者:
黑马刘涛
时间:
2012-7-13 17:43
public static void main(String[] args){
try {
File f=new File("D:\\abc.txt");
FileOutputStream fos=new FileOutputStream(f,true); // 如果第二个参数为 true,则将字节写入文件末尾处,而不是写入文件开始处。
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
int i=0;
while(true){
try{
Point p=(Point)in.readObject();
System.out.println(p);
}
catch(EOFException e){
break;
}
Point p=(Point)in.readObject();
System.out.println(p);
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
复制代码
如果是FileOutputStream(File file)构造方法将会在文件开头写,覆盖原来写入的数据。改过代码后你可以看看abc.txt文件里多了些东西
作者:
王双宝
时间:
2012-7-14 09:13
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.SocketException;
import java.net.UnknownHostException;
public class Test {
/**
* @param args
* @throws UnknownHostException
* @throws SocketException
*/
public static void main(String[] args){
try {
File f=new File("abc.txt");
if(!f.exists()){
f.createNewFile();
}
FileOutputStream fos=new FileOutputStream(f,true);//是可以到文件尾
ObjectOutputStream out=new ObjectOutputStream(fos);
out.writeObject(new Point(3,4));//怎么改成添加在文件尾?
out.writeObject(new Point(2,3));//怎么改成添加在文件尾?
out.flush();
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ObjectInputStream in=new ObjectInputStream(new FileInputStream(new File("abc.txt")));
int i=0;
while(true){
try{
Point p=(Point)in.readObject(); //这里读不出来写入的所有对象,还是第一次写入的内容,后面新写的内容读出不来,为什么?
System.out.println(p);
}catch(Exception e){
System.out.println(e.toString());//此处抛出了java.io.StreamCorruptedException
break;
}
}
in.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Point implements Serializable{
private int x;
private int y;
public Point(){
}
public Point(int x,int y){
this.x=x;
this.y=y;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "x="+x+",y="+y;
}
}
运行结果:
x=3,y=4
x=2,y=3
java.io.StreamCorruptedException: invalid type code: AC
求解
作者:
樊佰轩
时间:
2012-7-14 11:47
FileOutputStream fos=new FileOutputStream(f,true);
//改成这个就可以了
//ture代表你可以在已存在的文件后面续写该文件内容。
//如果改为false,那么就会开始的时候一样,每次运行都会创建一个新的文件替代已存在的文件。
//默认情况下是false。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2