流操作与网络类
序列化与反序列化
- ObjectInputStream 和 ObjectOutputStream
- 作用:用于存储和读取基本数据类型数据或对象的处理流。它的强大之处就是可以把Java中的对象写入到数据源中,也能把对象从数据源中还原回来。
- 要想一个java对象是可序列化的,需要满足相应的要求。
- 需要实现接口:Serializable
- 当前类提供一个全局常量:serialVersionUID
- 除了当前Person类需要实现Serializable接口之外,还必须保证其内部所有属性也必须是可序列化的。(默认情况下,基本数据类型可序列化)
- 序列化机制:
对象序列化机制允许把内存中的Java对象转换成平台无关的二进制流,从而允许把这种
二进制流持久地保存在磁盘上,或通过网络将这种二进制流传输到另一个网络节点。
当其它程序获取了这种二进制流,就可以恢复成原来的Java对象。
注:ObjectOutputStream和ObjectInputStream不能序列化static和transient修饰的成员变量
// 需要实现Serializable接口
public class Person implements Serializable{
// 序列化UID,必须要自己定义
private static final long serialVersionUID = 475463534532L;
private String name; // 官方类,本身就是个可以序列化的类
private int age;
private int id;
private Account acct; // 内部自定义类
public Person(String name, int age, int id) {
this.name = name;
this.age = age;
this.id = id;
}
public Person(String name, int age, int id, Account acct) {
this.name = name;
this.age = age;
this.id = id;
this.acct = acct;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", id=" + id +
", acct=" + acct +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public Person() {
}
}
class Account implements Serializable{
public static final long serialVersionUID = 4754534532L;
private double balance;
@Override
public String toString() {
return "Account{" +
"balance=" + balance +
'}';
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
public Account(double balance) {
this.balance = balance;
}
}
- 序列化过程:将内存中的java对象保存到磁盘中或通过网络传输出去
@Test
public void testObjectOutputStream(){
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("object.dat"));
oos.writeObject(new String("哇哈哈111"));
oos.flush();//刷新操作
oos.writeObject(new Person("哦呵呵",23,1001,new Account(5000)));
oos.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
- 反序列化:将磁盘文件中的对象还原为内存中的一个java对象
@Test
public void testObjectInputStream(){
ObjectInputStream ois = null;
try {
ois = new ObjectInputStream(new FileInputStream("object.dat"));
Object obj = ois.readObject();
String str = (String) obj;
Person p = (Person) ois.readObject();
System.out.println(str);
System.out.println(p);
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} finally {
if(ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
java.nio.file工具类
jdk 7.0 时,引入了 Path、Paths、Files三个类。Path为了简化文件操作的File类的升级版本,Paths与Files是工具类
Path,Paths类
- 实例化Path
static Path get(String first, String … more) : 用于将多个字符串串连成路径
static Path get(URI uri): 返回指定uri对应的Path路径
@Test
public void test1() {
Path path1 = Paths.get("d:\\nio\\hello.txt");//new File(String filepath)
Path path2 = Paths.get("d:\\", "nio\\hello.txt");//new File(String parent,String filename);
System.out.println(path1);
System.out.println(path2);
Path path3 = Paths.get("d:\\", "nio");
System.out.println(path3);
}
方法 | 描述 |
---|
boolean startsWith(String path) | 判断是否以 path 路径开始 |
boolean endsWith(String path) | 判断是否以 path 路径结束 |
boolean isAbsolute() | 判断是否是绝对路径 |
Path getParent() | 返回Path对象包含整个路径,不包含 Path 对象指定的文件路径 |
Path getRoot() | 返回调用 Path 对象的根路径 |
Path getFileName() | 返回与调用 Path 对象关联的文件名 |
int getNameCount() | 返回Path 根目录后面元素的数量 |
Path getName(int idx) | 返回指定索引位置 idx 的路径名称 |
Path toAbsolutePath() | 作为绝对路径返回调用 Path 对象 |
Path resolve(Path p) | 合并两个路径,返回合并后的路径对应的Path对象 |
File toFile() | 将Path转化为File类的对象 |
@Test
public void test2() {
Path path1 = Paths.get("d:\\", "nio\\nio1\\nio2\\hello.txt");
Path path2 = Paths.get("hello.txt");
System.out.println(path1); // d:\nio\nio1\nio2\hello.txt
System.out.println(path1.startsWith("d:\\nio")); // true
System.out.println(path1.endsWith("hello.txt")); // true
System.out.println(path1.isAbsolute()); // true
System.out.println(path2.isAbsolute()); // false
System.out.println(path1.getParent()); // d:\nio\nio1\nio2
System.out.println(path2.getParent()); // null
System.out.println(path1.getRoot()); // d:\
System.out.println(path2.getRoot()); // null
System.out.println(path1.getFileName()); // hello.txt
System.out.println(path2.getFileName()); // hello.txt
for (int i = 0; i < path1.getNameCount(); i++) {
System.out.print(path1.getName(i) + " "); // nio nio1 nio2 hello.txt
}
System.out.println(path1.toAbsolutePath()); // d:\nio\nio1\nio2\hello.txt
System.out.println(path2.toAbsolutePath()); // 当前工程下hello.txt的绝对路径
Path path3 = Paths.get("d:\\", "nio");
Path path4 = Paths.get("nioo\\hi.txt");
path3 = path3.resolve(path4);
System.out.println(path3); // d:\nio\nioo\hi.txt
File file = path1.toFile();//Path--->File的转换// File toFile(): 将Path转化为File类的对象
Path newPath = file.toPath();//File--->Path的转换
}
Files类使用
方法 | 描述 |
---|
Path copy(Path src, Path dest, CopyOption … how) | 文件的复制 |
Path createDirectory(Path path, FileAttribute<?> … attr) | 创建一个目录 |
Path createFile(Path path, FileAttribute<?> … arr) | 创建一个文件 |
void delete(Path path) | 删除一个文件/目录,如果不存在,执行报错 |
void deleteIfExists(Path path) | Path对应的文件/目录如果存在,执行删除.如果不存在,正常执行结束 |
Path move(Path src, Path dest, CopyOption…how) | 将 src 移动到 dest 位置 |
long size(Path path) | 返回 path 指定文件的大小 |
boolean exists(Path path, LinkOption … opts) | 判断文件是否存在 |
boolean isDirectory(Path path, LinkOption … opts) | 判断是否是目录 |
boolean isRegularFile(Path path, LinkOption … opts) | 判断是否是文件 |
boolean isHidden(Path path) | 判断是否是隐藏文件 |
boolean isReadable(Path path) | 判断文件是否可读 |
boolean isWritable(Path path) | 判断文件是否可写 |
boolean notExists(Path path, LinkOption … opts) | 判断文件是否不存在 |
InputStream newInputStream(Path path, OpenOption…how) | 获取 InputStream 对象 |
OutputStream newOutputStream(Path path, OpenOption…how) | 获取 OutputStream 对象 |
SeekableByteChannel newByteChannel(Path path, OpenOption…how) | 获取与指定文件的连接,how 指定打开方式。 |
DirectoryStream
newDirectoryStream(Path path)
| 打开 path 指定的目录 |
创建流
- 文件权限
StandardOpenOption.READ:表示对应的Channel是可读的。
StandardOpenOption.WRITE:表示对应的Channel是可写的。
StandardOpenOption.CREATE:如果要写出的文件不存在,则创建。如果存在,忽略
StandardOpenOption.CREATE_NEW:如果要写出的文件不存在,则创建。如果存在,抛异常
@Test
public void test3() throws IOException{
Path path1 = Paths.get("d:\\", "hello.txt");
InputStream inputStream = Files.newInputStream(path1, StandardOpenOption.READ);
OutputStream outputStream = Files.newOutputStream(path1, StandardOpenOption.WRITE,StandardOpenOption.CREATE);
SeekableByteChannel channel = Files.newByteChannel(path1, StandardOpenOption.READ,StandardOpenOption.WRITE,StandardOpenOption.CREATE);
Path path2 = Paths.get("e:\\lib");
DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path2);
Iterator<Path> iterator = directoryStream.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
网络类
方法 | 描述方式 |
---|
public static InetAddress getByName(String host) | 根据填入获取Inet类 |
public static InetAddress getLocalHost() | 获取本地ip |
public String getHostName() | 获取域名 |
public String getHostAddress() | 获取IP地址 |
TCP实例
public class TCPTest3 {
/*
这里涉及到的异常,应该使用try-catch-finally处理
*/
@Test
public void client() throws IOException {
//1.创建Socket对象,指明服务器端的ip和端口号
Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
//2.获取一个输出流,用于输出数据
OutputStream os = socket.getOutputStream();
//3.读取需要发送的文件
FileInputStream fis = new FileInputStream(new File("test.txt"));
//4.进行文件发送
byte[] buffer = new byte[1024];
int len;
while((len = fis.read(buffer)) != -1){
os.write(buffer,0,len);
}
//关闭数据的输出
socket.shutdownOutput();
//5.接收来自于服务器端的数据,并显示到控制台上
InputStream is = socket.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] bufferr = new byte[20];
int len1;
while((len1 = is.read(buffer)) != -1){
baos.write(buffer,0,len1);
}
System.out.println(baos.toString());
//6.关闭资源
fis.close();
os.close();
socket.close();
baos.close();
}
/*
这里涉及到的异常,应该使用try-catch-finally处理
*/
@Test
public void server() throws IOException {
//1.创建服务器端的ServerSocket,指明自己的端口号
ServerSocket ss = new ServerSocket(9090);
//2.调用accept()表示接收来自于客户端的socket
Socket socket = ss.accept();
//3.获取输入流
InputStream is = socket.getInputStream();
//4.接收数据输出到本地文件
FileOutputStream fos = new FileOutputStream(new File("text2.txt"));
//5.读取输入流中的数据
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("文件传输完成");
//6.服务器端给予客户端反馈
OutputStream os = socket.getOutputStream();
os.write("文件已接收".getBytes());
//7.关闭资源
fos.close();
is.close();
socket.close();
ss.close();
os.close();
}
}
UDP实例
public class UDPTest {
//发送端
@Test
public void sender() throws IOException {
DatagramSocket socket = new DatagramSocket();
String str = "UDP方式发送数据";
byte[] data = str.getBytes();
InetAddress inet = InetAddress.getLocalHost();
DatagramPacket packet = new DatagramPacket(data,0,data.length,inet,9090);
socket.send(packet);
socket.close();
}
//接收端
@Test
public void receiver() throws IOException {
DatagramSocket socket = new DatagramSocket(9090);
byte[] buffer = new byte[100];
DatagramPacket packet = new DatagramPacket(buffer,0,buffer.length);
socket.receive(packet);
System.out.println(new String(packet.getData(),0,packet.getLength()));
socket.close();
}
}
URL实例
- 可以在本地安装一个tomcat做服务器,模拟从服务器下载文件
public class URLTest1 {
public static void main(String[] args) {
HttpURLConnection urlConnection = null;
InputStream is = null;
FileOutputStream fos = null;
try {
URL url = new URL("http://localhost:8080/examples/test.txt");
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
is = urlConnection.getInputStream();
fos = new FileOutputStream("test2.txt");
byte[] buffer = new byte[1024];
int len;
while((len = is.read(buffer)) != -1){
fos.write(buffer,0,len);
}
System.out.println("下载完成");
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭资源
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(urlConnection != null){
urlConnection.disconnect();
}
}
}
}