更新时间:2022-12-29 11:06:23 来源:极悦 浏览912次
Java文件处理是什么?极悦小编来为大家进行详细介绍。
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
File file1 = new File("./src/main/resources/test");
File file2 = new File("./src/main/resources/test/score.txt");
System.out.println(file1.isDirectory());
System.out.println(file2.isFile());
System.out.println(file1.isAbsolute());
if (! file1.exists()) {
file1.mkdirs();
}
if (! file2.exists()) {
try {
file2.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
字节输入流InputStream
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
/**
* 从文件系统中的某个文件中获得输入字节
* 用于读取诸如图像数据之类的原始字节流
*/
public class FileInputStreamDemo {
public static void main(String[] args) {
FileInputStream fis = null;
try {
fis = new FileInputStream("./src/main/resources/test/score.txt");
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (fis != null) {
// readDemo(fis);
readByteDemo(fis);
}
}
/**
* read(byte[] b)
* read(byte[] b, int off, int len)
* @param fis
*/
public static void readByteDemo(FileInputStream fis) {
byte[] b = new byte[100];
try {
fis.read(b, 0, 5);
System.out.println(new String(b));
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* read()
* @param fis
*/
public static void readDemo(FileInputStream fis) {
try {
int n = 0;
while ((n = fis.read()) != -1) {
System.out.print((char) n );
}
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字节输出流OutputStream
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileOutputStreamDemo {
public static void main(String[] args) {
String name = "./src/main/resources/test/score1.txt";
FileOutputStream fos;
FileInputStream fis;
try {
fos = new FileOutputStream(name, true);
fis = new FileInputStream(name);
fos.write(50);
fos.write('a');
System.out.println(fis.read());
System.out.println((char) fis.read());
fos.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyFileDemo {
public static void main(String[] args) {
String path = "./src/main/resources/test/";
String name = "111.jpeg";
try {
FileInputStream fis = new FileInputStream(path + name);
FileOutputStream fos = new FileOutputStream(path + "copy" + name);
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();
}
}
}
import java.io.*;
/**
* 缓冲输入流 BufferedInputStream
* 缓冲输出流 BufferedOutputStream
*/
public class BufferedDemo {
public static void main(String[] args) {
String name = "./src/main/resources/test/score1.txt";
try {
FileOutputStream fos = new FileOutputStream(name);
BufferedOutputStream bos = new BufferedOutputStream(fos);
FileInputStream fis = new FileInputStream(name);
BufferedInputStream bis = new BufferedInputStream(fis);
long startTime = System.currentTimeMillis();
bos.write(50);
bos.write('a');
bos.flush(); // 缓冲区未写满,调用该方法强制清空缓冲区并写入文件
System.out.println(bis.read());
System.out.println((char) bis.read());
long endTime = System.currentTimeMillis();
System.out.println(endTime - startTime);
fos.close();
bos.close(); // 也会强制清空缓冲区并写入文件
fis.close();
bis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
字符输入流 Reader
字符输出流 Writer
InputStreamReader
OutputStreamWriter
import java.io.*;
public class ReaderDemo {
public static void main(String[] args) {
String name = "./src/main/resources/test/score.txt";
String name1 = "./src/main/resources/test/score1.txt";
try {
FileInputStream fis = new FileInputStream(name);
InputStreamReader isr = new InputStreamReader(fis, "GBK");
BufferedReader br = new BufferedReader(isr);
FileOutputStream fos = new FileOutputStream(name1);
OutputStreamWriter osw = new OutputStreamWriter(fos, "GBK");
BufferedWriter bw = new BufferedWriter(osw);
int n = 0;
char[] cbuf = new char[10];
/* 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.print(s);
}*/
while ((n = br.read(cbuf)) != -1) {
// String s = new String(cbuf, 0, n);
// osw.write(s);
bw.write(cbuf, 0, n);
bw.flush();
}
fis.close();
fos.close();
isr.close();
osw.close();
br.close();
bw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
步骤:
(1)创建一个类,继承Serializable接口;
(2)创建对象;
(3)将对象写入文件;
(4)从文件读取对象信息
对象输入流ObjectInputStream
对象输出流ObjectOutputStream
import java.io.*;
public class GoodsTest {
public static void main(String[] args) {
String name = "./src/main/resources/test/score.txt";
Goods goods1 = new Goods("gd001", "电脑", 3000);
try {
FileOutputStream fos = new FileOutputStream(name);
ObjectOutputStream oos = new ObjectOutputStream(fos);
FileInputStream fis = new FileInputStream(name);
ObjectInputStream ois = new ObjectInputStream(fis);
// 将Goods对象写入文件
oos.writeObject(goods1);
oos.writeBoolean(true);
oos.flush();
// 读取对象
Goods goods = (Goods) ois.readObject();
System.out.println(goods);
System.out.println(ois.readBoolean());
fos.close();
oos.close();
fis.close();
ois.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
}
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 "Goods{" +
"goodsId='" + goodsId + '\'' +
", goodsName='" + goodsName + '\'' +
", price=" + price +
'}';
}
}
0基础 0学费 15天面授
Java就业班有基础 直达就业
业余时间 高薪转行
Java在职加薪班工作1~3年,加薪神器
工作3~5年,晋升架构
提交申请后,顾问老师会电话与您沟通安排学习