WriteOutputStream
2016-07-07 11:08
323 查看
ObjectOutputStream oos = null;
try {
oos = new ObjectOutputStream(new FileOutputStream("d:\\file.txt"));
Students student = new Students(50, "dsfhgj", 20);
Students student2 = new Students(123, "赵三", 25);
oos.writeObject(student);
oos.writeObject(student2);
ObjectInputStream objectReader = new ObjectInputStream(
new FileInputStream("d:\\file.txt"));
for (int i = 0; i < 3; i++) {
System.out.println(objectReader.readObject());
}
} catch (Exception e) {
} finally {
oos.close();
}
File file = new File("d:\\", "file");
System.out.println("文件名" + file.getName());
System.out.println("路径" + file.getParent());
System.out.println("绝对路径" + file.getAbsolutePath());
System.out.println("判断文件是否存在" + file.exists());
System.out.println(file.isDirectory() ? "目录" : "文件");
System.out.println(file.isFile() ? "普通" : "命名管道");
if (file.canRead()) {// 是否能读
System.out.println("可读文件");
} else {
System.out.println("非可读文件");
}
if (file.canWrite()) {
System.out.println("可写文件");
} else {
System.out.println("非可写文件");
}
System.out.println(file.lastModified());// 文件最后修改时间()
File file2 = new File("d:\\a.txt");
file2.createNewFile();
File file3 = new File("d:\\pr文件");
showDirs(file3);
}
/**
* 创建文件
*
* @param file
* @throws IOException
*/
public static void create(File file) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
}
/**
* 删除文件
*
* @param file
* @throws IOException
*/
public static void delete(File file) throws IOException {
if (file.exists()) {
file.delete();
}
}
/**
* 显示文件路径
*
* @param file
* @throws IOException
*/
public static void showDirs(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file2 : files) {
System.out.println(file2.getName());
}
}
try {
oos = new ObjectOutputStream(new FileOutputStream("d:\\file.txt"));
Students student = new Students(50, "dsfhgj", 20);
Students student2 = new Students(123, "赵三", 25);
oos.writeObject(student);
oos.writeObject(student2);
ObjectInputStream objectReader = new ObjectInputStream(
new FileInputStream("d:\\file.txt"));
for (int i = 0; i < 3; i++) {
System.out.println(objectReader.readObject());
}
} catch (Exception e) {
} finally {
oos.close();
}
File file = new File("d:\\", "file");
System.out.println("文件名" + file.getName());
System.out.println("路径" + file.getParent());
System.out.println("绝对路径" + file.getAbsolutePath());
System.out.println("判断文件是否存在" + file.exists());
System.out.println(file.isDirectory() ? "目录" : "文件");
System.out.println(file.isFile() ? "普通" : "命名管道");
if (file.canRead()) {// 是否能读
System.out.println("可读文件");
} else {
System.out.println("非可读文件");
}
if (file.canWrite()) {
System.out.println("可写文件");
} else {
System.out.println("非可写文件");
}
System.out.println(file.lastModified());// 文件最后修改时间()
File file2 = new File("d:\\a.txt");
file2.createNewFile();
File file3 = new File("d:\\pr文件");
showDirs(file3);
}
/**
* 创建文件
*
* @param file
* @throws IOException
*/
public static void create(File file) throws IOException {
if (!file.exists()) {
file.createNewFile();
}
}
/**
* 删除文件
*
* @param file
* @throws IOException
*/
public static void delete(File file) throws IOException {
if (file.exists()) {
file.delete();
}
}
/**
* 显示文件路径
*
* @param file
* @throws IOException
*/
public static void showDirs(File file) throws IOException {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File file2 : files) {
System.out.println(file2.getName());
}
}
相关文章推荐
- wkhtmtopdf--高分辨率转HTML成PDF(一)
- wkhtmtopdf--高分辨率转HTML成PDF(二)
- java IO总结
- 黑马程序员_I/O流总结
- java IO学习笔记
- IO流
- Java IO 基础总结(一)
- Java 输入输出流教程(一)(原作:Java IO Tutorial by Jakob Jenkov)
- IO练习
- JAVA - IO流 - 整理
- 黑马程序员_java IO流及文件操作详解
- java I/O (二)——对象序列化
- java io新类及常用输入输出方法
- response响应那些事之PrintWriter,OutputStream;write(),print[ln]()
- 输入字节流(FileInputStream)和输出字节流(FileInputStream)的用法
- Java_io体系之概念与整体结构——02
- Java_io体系之InputStream、OutputStream简介、走进源码——03
- Java_io体系之ByteArrayInputStream、ByteArrayOutputStream简介、走进源码及示例——04
- Java_io体系之File、FileInputStream、FileOutputStream简介、走进源码及示例——05
- Java_io体系之PipedInputStream、PipedOutputStream简介、走进源码及示例——06