Java:需要使用ByteArrayOutputStream将数据写入文件,不知该如何写下去了,求解
importjava.io.ByteArrayOutputStream;importjava.io.File;importjava.io.FileOutputStream...
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
User obj = new User();
obj.setName("Timml");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("E://output"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Put data in your baos
baos.writeTo(fos);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
}
}
} 展开
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test {
public static void main(String[] args) {
User obj = new User();
obj.setName("Timml");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("E://output"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Put data in your baos
baos.writeTo(fos);
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
}
}
} 展开
4个回答
展开全部
package com.mkyong.core;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class Test {
public static void main(String[] args) {
User obj = new User();
obj.setName("Timml2");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(new File("output"));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
// Put data in your baos
ObjectOutputStream objectOutputStream = new ObjectOutputStream(baos);
objectOutputStream.writeObject(obj);
objectOutputStream.flush();
objectOutputStream.close();
baos.writeTo(fos);
baos.flush();
baos.close();
ObjectInputStream objectinputStream = new ObjectInputStream(new FileInputStream(new File("output")));
User user = (User)objectinputStream.readObject();
System.out.println(user);
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}
class User implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User [name=" + name + ", age=" + age + "]";
}
}
展开全部
String filePath = "D:test.txt";
//创建文件
File file = new File(filePath);
PrintWriter pw = null;
try{
if(!file.exists()){
file.createNewFile();
}
pw = new PrintWriter(new FileWriter(file));
pw.write(sb.toString());
}catch(Exception e){
System.out.println("新建文件出错");
//e.printStackTrace();
}finally{
pw.close();
}
这个只适用于文本文档,不能写word,excel,pdf
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
首先将数据流化,然后调用io包的file创建文件或用已声明的文件,再写入文件转化过来嘛
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-15
展开全部
你找API看看ByteArrayOutputStream操作的对象是什么,然后再调用相应的io进行操作就行了啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询