第五课:流与其他
Posted Vanau
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第五课:流与其他相关的知识,希望对你有一定的参考价值。
首先参考自:http://blog.csdn.net/hguisu/article/details/7418161
一.文件输入输出字节流
/*
*文件名:fileInputStreamTest.java
*功能:读取文件并显示到控制台
*/
package IOTest;
import java.io.*;
public class fileInputStreamTest
public static void main(String[] args) throws IOException
FileInputStream fileInputStream=new FileInputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt");
byte[] buf=new byte[1024];
int length=0;
while ((length=fileInputStream.read(buf))>0)
System.out.println(new String(buf,0,length));
fileInputStream.close();
/*
*文件名:fileOutputStreamTest
*功能:读取文件并将文件写入另一个文件中(复制)
*/
package IOTest;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class fileOutputStreamTest
public static void main(String[] args) throws IOException
FileInputStream fileInputStream=new FileInputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt");
FileOutputStream fileOutputStream=new FileOutputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\w.txt");
byte[] buf=new byte[1024];
int length=fileInputStream.read(buf,0,buf.length);
for (byte b : buf)
fileOutputStream.write(b);
fileInputStream.close();
fileOutputStream.close();
二.输入输出字符流
/*
*文件名:bufferedReaderTest.java
*功能:读取文件到控制台显示
*/
package IOTest;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
public class bufferedReaderTest
public static void main(String[] args) throws IOException
BufferedReader bufferedReader=new BufferedReader(new FileReader("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
String line=null;
while ((line=bufferedReader.readLine())!=null)
System.out.println(line);
/*
*文件名:bufferedWriterTest.java
*功能:读取文件并写入到另一个文件(复制)
*/
package IOTest;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class bufferedWriterTest
public static void main(String[] args) throws IOException
BufferedReader bufferedReader=new BufferedReader(new FileReader("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
BufferedWriter bufferedWriter=new BufferedWriter(new FileWriter("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\w.txt"));
String line=null;
while ((line=bufferedReader.readLine())!=null)
bufferedWriter.write(line);
bufferedWriter.newLine();
bufferedWriter.close();
bufferedReader.close();
三.对象流
/*
*文件名:User.java
*功能:对象类信息
*/
package IOTest;
import java.io.*;
public class User implements Serializable
String userName;
String password;
public User(String userName,String password)
this.userName=userName;
this.password=password;
@Override
public String toString()
return "[用户名:"+this.userName+"----密码:"+this.password+"]";
/*
*文件名:UserTest.java
*功能:写对象和读对象测试
*/
package IOTest;
import java.io.*;
public class UserTest
public static void main(String[] args) throws IOException, ClassNotFoundException
User user1=new User("Alice", "1234");
writeObj(user1);
User user2=(User)readObj();
System.out.println("读取对象的信息:"+user2);
//写对象到文件(序列化)
public static void writeObj(User user) throws IOException
ObjectOutputStream objOut=new ObjectOutputStream(new FileOutputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
objOut.writeObject(user);
objOut.close();
//读取文件中的对象(反序列化)
public static Object readObj() throws IOException, ClassNotFoundException
ObjectInputStream objIn=new ObjectInputStream(new FileInputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
return objIn.readObject();
/*
*文件名:UsersTest.java
*功能:写对象数组和读对象数组
*/
package IOTest;
import java.io.*;
public class UsersTest
public static void main(String[] args) throws IOException, ClassNotFoundException
User[] users=new User[3];
users[0]=new User("Alice", "1234");
users[1]=new User("Bob", "12345");
users[2]=new User("Carl", "123456");
writeObj(users);
User[] user2=readObj2();
for (User user : user2)
System.out.println(user);
/* User user1=new User("Alice", "1234");
writeObj(user1);
User user2=(User)readObj();
System.out.println("读取对象的信息:"+user1);*/
//写对象到文件(序列化)
public static void writeObj(User[] user) throws IOException
ObjectOutputStream objOut=new ObjectOutputStream(new FileOutputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
objOut.writeObject(user);
objOut.close();
//读取文件中的对象(反序列化)
/* public static Object readObj() throws IOException, ClassNotFoundException
ObjectInputStream objIn=new ObjectInputStream(new FileInputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
return objIn.readObject();
*/
public static User[] readObj2() throws IOException, ClassNotFoundException
ObjectInputStream objIn=new ObjectInputStream(new FileInputStream("C:\\\\Users\\\\f\\\\Desktop\\\\WorkingSpace\\\\r.txt"));
User[] newusers=(User[])objIn.readObject();
return newusers;
得到结果:
以上是关于第五课:流与其他的主要内容,如果未能解决你的问题,请参考以下文章