IO流(2)
来源:互联网 发布:ios网络电视 编辑:程序博客网 时间:2024/06/05 09:05
输出结果:我有一只小毛驴File f=new File("D://8.txt"); //已经被写入“我有一只小毛驴”
InputStream is=new FileInputStream(f);
BufferedInputStream br=new BufferedInputStream(is);
Reader r=new InputStreamReader(br);
char ch[]=new char[1024];
int len=-1;
StringBuffer sb=new StringBuffer();
while((len=r.read(ch))!=-1){
sb.append(new String(ch,0,50));
}
is.close();
r.close();
br.close();
System.out.println(sb);
}
public static void main(String[] args) throws IOException {
InputStreamReader2();
}
⑤PrintStream/PrintWriterpublic static void Out() throws IOException{
File f2=new File("D://9.txt");
OutputStream os=new FileOutputStream(f2,true);
OutputStreamWriter osw=new OutputStreamWriter(os);
BufferedWriter bw=new BufferedWriter(osw);
String s3="我希望w自己x能赶t上移动互联u网时代";
bw.write(s3);
bw.close();
osw.close();
os.close();
}
public static void main(String[] args) throws IOException {
Out();
}
PrintStream和PrintWriter差别不大,后者实现了前者的所有功能,不同的是,后者的自动刷新需要println等方法,否则不会自动刷新。public static void print() throws IOException{
OutputStream os=new FileOutputStream("D://op.txt");
BufferedOutputStream bos=new BufferedOutputStream(os);
PrintStream ps=new PrintStream(bos);
ps.print(9.098);
ps.print(true);
ps.print("我呵呵");
ps.print(87);
ps.close();
bos.close();
os.close();
}
public static void main(String[] args) throws IOException {
print();
}
要记住对象写入的顺序,因为读取的时候要按照顺序来,否则会乱码。public static void Output(){
try {
OutputStream os=new FileOutputStream("D://yui.tmp");
ObjectOutputStream oos=new ObjectOutputStream(os);
Dog d1=new Dog("小白",7);
Dog d2=new Dog("小黑",5);
oos.writeObject(d1);
oos.writeBoolean(true);
oos.writeObject(d2);
oos.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Output();
}
反序列化的时候,顺序要按照写入的顺序来。而且,注意可能会出现类型不匹配。public static void input() throws IOException, ClassNotFoundException{
InputStream is=new FileInputStream("D://yui.tmp");
ObjectInputStream ois=new ObjectInputStream(is);
Object obj=ois.readObject();
Dog dog1=(Dog)obj;
Boolean bl=ois.readBoolean();
Dog dog2=(Dog)ois.readObject();
System.out.println(bl);
System.out.println(dog1);
System.out.println(dog2);
ois.close();
is.close();
}
public static void main(String[] args) {
try {
input();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
读取操作和对象流类似,这里就不列出来了。public static void Dataout(){
try {
OutputStream os=new FileOutputStream("D://io.tmp");
DataOutputStream dos=new DataOutputStream(os);
dos.writeInt(45);
dos.writeUTF("按顺序读取");
dos.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Dataout();
}
- IO流2
- Java io流(2)
- io流 2
- IO流2
- IO输入输出流-2
- IO流1-2
- IO流笔记2
- IO流2
- IO流(2)
- IO流2
- IO流(2)
- IO流(2)
- JAVA IO流(2)
- IO流(2)
- Java IO流(2)
- (2)IO流
- IO流(2)
- IO流2
- IO流(1)
- [leetcode-20]Valid Parentheses(java)
- 图像处理------直方图均衡化
- <th></th><caption></caption>标签
- 【大渣】KMP算法
- IO流(2)
- Html5学习之Form
- 桥梁结构的ANSYS参数化分析
- php 正则表达式匹配 斜杠问题
- dom4j中xml解析的一些常用方法
- Hibernate 增删改查
- 使用 Cocos2d-x 和粒子编辑器实现“天天爱消除”场景特效
- 反射
- find_text_dongle.hdev相关例程学习