File类的使用
文件可认为是相关记录或放在一起的数据的集合。
在JAVA中,使用java.io.File类对文件进行操作。
File类的相关操作
// File file = new File("D:\\study\\java\\javaTest2\\src\\io\\file\\iotest.txt");
// File file = new File("D:\\study\\java","javaTest2\\src\\io\\file\\iotest.txt");
File f = new File("D:\\study\\java");
File file = new File(f,"javaTest2\\src\\io\\file\\iotest.txt");
System.out.println("是否是文件:"+file.isFile());
System.out.println("是否是目录:"+file.isDirectory());
File f2 = new File("D:\\study\\java\\javaTest2\\src\\io2");
if (!f2.exists()){
f2.mkdir();
}
File f3 = new File("D:\\study\\java\\javaTest2\\src\\io2\\test.txt");
try {
f3.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
字节流
字节输入流 InputStream
FileInputStream
从文件系统中的某个文件中获得输入字节。
用于读取诸如图像数据之类的原始字节流。
如果返回值为-1,则表示已经达到文件末尾。
public class FileInputDemo {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("test.txt");
int n = fis.read();
while(n!=-1){
System.out.println((char)n);
n=fis.read();
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
字节输出流 OutputStream
FileOutputStream
public class FileOutputDemo {
public static void main(String[] args) {
FileOutputStream fos;
try {
fos = new FileOutputStream("test.txt");
fos.write(50);
fos.write('a');
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
文件拷贝demo:
public class FileCopy {
public static void main(String[] args) {
// 文件拷贝
try {
FileInputStream fis = new FileInputStream("D:\\study\\java\\javaTest2\\src\\io\\byteIo\\demo.txt");
FileOutputStream fos = new FileOutputStream("D:\\study\\java\\javaTest2\\src\\io\\byteIo\\demo2.txt");
int n = 0;
byte[] b = new byte[1024];
while ((n = fis.read(b))!=-1){
fos.write(b,0,n);
}
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
缓冲流
缓冲输入流 BufferedInputStream
缓冲输出流 BufferedOutputStream
public class BufferDemo {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("demo.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos);
FileInputStream fis = new FileInputStream("demo.txt");
BufferedInputStream bis = new BufferedInputStream(fis);
bos.write(50);
bos.write('a');
bos.flush();//强制清空
System.out.println(bis.read());
System.out.println((char)bis.read());
bis.close();
bos.close();
fis.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}
字符流
字符输入流 Reader
字符输出流 Writer
字节字符输入输出流demo:
public class ReaderDemo {
public static void main(String[] args) {
try {
FileInputStream fis = new FileInputStream("part1.md");
InputStreamReader isr = new InputStreamReader(fis);
FileOutputStream fos = new FileOutputStream("part01.md");
OutputStreamWriter osw = new OutputStreamWriter(fos);
int n = 0;
char[] cbuf = new char[1024];
// while ((n = isr.read())!=-1){
// System.out.print((char)n);
// }
// while((n = isr.read(cbuf))!=-1){
// String s = new String(cbuf,0,n);
// System.out.println(s);
// }
// 输出
while((n = isr.read(cbuf))!=-1){
// osw.write(cbuf,0,n);
// osw.flush();
String s = new String(cbuf,0,n);
osw.write(s);
}
fis.close();
fos.close();
osw.close();
isr.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
}
}
}
对象的序列化与反序列化
对象序列化的步骤
- 创建一个类,继承Serializable接口。
- 创建对象
- 将对象写入文件
- 从文件读取对象信息
对象输入流 ObjectInputStream
对象输出流 ObjectOutputStream
序列化demo。
Goods.java
import java.io.Serializable;
public class Goods implements Serializable {
private String goodsId;
private String goodsName;
private double price;
public Goods(String goodsId, String goodsName, double price) {
this.goodsId = goodsId;
this.goodsName = goodsName;
this.price = price;
}
public String getGoodsId() {
return goodsId;
}
public void setGoodsId(String goodsId) {
this.goodsId = goodsId;
}
public String getGoodsName() {
return goodsName;
}
public void setGoodsName(String goodsName) {
this.goodsName = goodsName;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "商品信息[编号:"+goodsId
+",名称:"+goodsName
+",价格:"+price+"]";
}
}
测试类 GoodsTest.java
import java.io.*;
public class GoodsTest {
public static void main(String[] args) {
Goods goods1 = new Goods("1001","电脑",4500);
try {
FileOutputStream fos = new FileOutputStream("test.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream("test.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
oos.writeObject(goods1);
oos.writeBoolean(true);
oos.flush();
Goods goods = null;
try {
goods = (Goods) ois.readObject();
System.out.println(goods);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
}